cronMigrationManager.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  1. /**
  2. * Cron Migration Manager
  3. * Manages database migrations as cron jobs using percolate:synced-cron
  4. */
  5. import { Meteor } from 'meteor/meteor';
  6. import { SyncedCron } from 'meteor/percolate:synced-cron';
  7. import { ReactiveVar } from 'meteor/reactive-var';
  8. import { cronJobStorage } from './cronJobStorage';
  9. // Server-side reactive variables for cron migration progress
  10. export const cronMigrationProgress = new ReactiveVar(0);
  11. export const cronMigrationStatus = new ReactiveVar('');
  12. export const cronMigrationCurrentStep = new ReactiveVar('');
  13. export const cronMigrationSteps = new ReactiveVar([]);
  14. export const cronIsMigrating = new ReactiveVar(false);
  15. export const cronJobs = new ReactiveVar([]);
  16. // Board-specific operation tracking
  17. export const boardOperations = new ReactiveVar(new Map());
  18. export const boardOperationProgress = new ReactiveVar(new Map());
  19. class CronMigrationManager {
  20. constructor() {
  21. this.migrationSteps = this.initializeMigrationSteps();
  22. this.currentStepIndex = 0;
  23. this.startTime = null;
  24. this.isRunning = false;
  25. this.jobProcessor = null;
  26. this.processingInterval = null;
  27. }
  28. /**
  29. * Initialize migration steps as cron jobs
  30. */
  31. initializeMigrationSteps() {
  32. return [
  33. {
  34. id: 'board-background-color',
  35. name: 'Board Background Colors',
  36. description: 'Setting up board background colors',
  37. weight: 1,
  38. completed: false,
  39. progress: 0,
  40. cronName: 'migration_board_background_color',
  41. schedule: 'every 1 minute', // Will be changed to 'once' when triggered
  42. status: 'stopped'
  43. },
  44. {
  45. id: 'add-cardcounterlist-allowed',
  46. name: 'Card Counter List Settings',
  47. description: 'Adding card counter list permissions',
  48. weight: 1,
  49. completed: false,
  50. progress: 0,
  51. cronName: 'migration_card_counter_list',
  52. schedule: 'every 1 minute',
  53. status: 'stopped'
  54. },
  55. {
  56. id: 'add-boardmemberlist-allowed',
  57. name: 'Board Member List Settings',
  58. description: 'Adding board member list permissions',
  59. weight: 1,
  60. completed: false,
  61. progress: 0,
  62. cronName: 'migration_board_member_list',
  63. schedule: 'every 1 minute',
  64. status: 'stopped'
  65. },
  66. {
  67. id: 'lowercase-board-permission',
  68. name: 'Board Permission Standardization',
  69. description: 'Converting board permissions to lowercase',
  70. weight: 1,
  71. completed: false,
  72. progress: 0,
  73. cronName: 'migration_lowercase_permission',
  74. schedule: 'every 1 minute',
  75. status: 'stopped'
  76. },
  77. {
  78. id: 'change-attachments-type-for-non-images',
  79. name: 'Attachment Type Standardization',
  80. description: 'Updating attachment types for non-images',
  81. weight: 2,
  82. completed: false,
  83. progress: 0,
  84. cronName: 'migration_attachment_types',
  85. schedule: 'every 1 minute',
  86. status: 'stopped'
  87. },
  88. {
  89. id: 'card-covers',
  90. name: 'Card Covers System',
  91. description: 'Setting up card cover functionality',
  92. weight: 2,
  93. completed: false,
  94. progress: 0,
  95. cronName: 'migration_card_covers',
  96. schedule: 'every 1 minute',
  97. status: 'stopped'
  98. },
  99. {
  100. id: 'use-css-class-for-boards-colors',
  101. name: 'Board Color CSS Classes',
  102. description: 'Converting board colors to CSS classes',
  103. weight: 2,
  104. completed: false,
  105. progress: 0,
  106. cronName: 'migration_board_color_css',
  107. schedule: 'every 1 minute',
  108. status: 'stopped'
  109. },
  110. {
  111. id: 'denormalize-star-number-per-board',
  112. name: 'Board Star Counts',
  113. description: 'Calculating star counts per board',
  114. weight: 3,
  115. completed: false,
  116. progress: 0,
  117. cronName: 'migration_star_numbers',
  118. schedule: 'every 1 minute',
  119. status: 'stopped'
  120. },
  121. {
  122. id: 'add-member-isactive-field',
  123. name: 'Member Activity Status',
  124. description: 'Adding member activity tracking',
  125. weight: 2,
  126. completed: false,
  127. progress: 0,
  128. cronName: 'migration_member_activity',
  129. schedule: 'every 1 minute',
  130. status: 'stopped'
  131. },
  132. {
  133. id: 'add-sort-checklists',
  134. name: 'Checklist Sorting',
  135. description: 'Adding sort order to checklists',
  136. weight: 2,
  137. completed: false,
  138. progress: 0,
  139. cronName: 'migration_sort_checklists',
  140. schedule: 'every 1 minute',
  141. status: 'stopped'
  142. },
  143. {
  144. id: 'add-swimlanes',
  145. name: 'Swimlanes System',
  146. description: 'Setting up swimlanes functionality',
  147. weight: 4,
  148. completed: false,
  149. progress: 0,
  150. cronName: 'migration_swimlanes',
  151. schedule: 'every 1 minute',
  152. status: 'stopped'
  153. },
  154. {
  155. id: 'add-views',
  156. name: 'Board Views',
  157. description: 'Adding board view options',
  158. weight: 2,
  159. completed: false,
  160. progress: 0,
  161. cronName: 'migration_views',
  162. schedule: 'every 1 minute',
  163. status: 'stopped'
  164. },
  165. {
  166. id: 'add-checklist-items',
  167. name: 'Checklist Items',
  168. description: 'Setting up checklist items system',
  169. weight: 3,
  170. completed: false,
  171. progress: 0,
  172. cronName: 'migration_checklist_items',
  173. schedule: 'every 1 minute',
  174. status: 'stopped'
  175. },
  176. {
  177. id: 'add-card-types',
  178. name: 'Card Types',
  179. description: 'Adding card type functionality',
  180. weight: 2,
  181. completed: false,
  182. progress: 0,
  183. cronName: 'migration_card_types',
  184. schedule: 'every 1 minute',
  185. status: 'stopped'
  186. },
  187. {
  188. id: 'add-custom-fields-to-cards',
  189. name: 'Custom Fields',
  190. description: 'Adding custom fields to cards',
  191. weight: 3,
  192. completed: false,
  193. progress: 0,
  194. cronName: 'migration_custom_fields',
  195. schedule: 'every 1 minute',
  196. status: 'stopped'
  197. },
  198. {
  199. id: 'migrate-attachments-collectionFS-to-ostrioFiles',
  200. name: 'Migrate Attachments to Meteor-Files',
  201. description: 'Migrating attachments from CollectionFS to Meteor-Files',
  202. weight: 8,
  203. completed: false,
  204. progress: 0,
  205. cronName: 'migration_attachments_collectionfs',
  206. schedule: 'every 1 minute',
  207. status: 'stopped'
  208. },
  209. {
  210. id: 'migrate-avatars-collectionFS-to-ostrioFiles',
  211. name: 'Migrate Avatars to Meteor-Files',
  212. description: 'Migrating avatars from CollectionFS to Meteor-Files',
  213. weight: 6,
  214. completed: false,
  215. progress: 0,
  216. cronName: 'migration_avatars_collectionfs',
  217. schedule: 'every 1 minute',
  218. status: 'stopped'
  219. },
  220. {
  221. id: 'migrate-lists-to-per-swimlane',
  222. name: 'Migrate Lists to Per-Swimlane',
  223. description: 'Migrating lists to per-swimlane structure',
  224. weight: 5,
  225. completed: false,
  226. progress: 0,
  227. cronName: 'migration_lists_per_swimlane',
  228. schedule: 'every 1 minute',
  229. status: 'stopped'
  230. },
  231. ];
  232. }
  233. /**
  234. * Initialize all migration cron jobs
  235. */
  236. initializeCronJobs() {
  237. this.migrationSteps.forEach(step => {
  238. this.createCronJob(step);
  239. });
  240. // Start job processor
  241. this.startJobProcessor();
  242. // Update cron jobs list after a short delay to allow SyncedCron to initialize
  243. Meteor.setTimeout(() => {
  244. this.updateCronJobsList();
  245. }, 1000);
  246. }
  247. /**
  248. * Start the job processor for CPU-aware job execution
  249. */
  250. startJobProcessor() {
  251. if (this.processingInterval) {
  252. return; // Already running
  253. }
  254. this.processingInterval = Meteor.setInterval(() => {
  255. this.processJobQueue();
  256. }, 5000); // Check every 5 seconds
  257. // Cron job processor started with CPU throttling
  258. }
  259. /**
  260. * Stop the job processor
  261. */
  262. stopJobProcessor() {
  263. if (this.processingInterval) {
  264. Meteor.clearInterval(this.processingInterval);
  265. this.processingInterval = null;
  266. }
  267. }
  268. /**
  269. * Process the job queue with CPU throttling
  270. */
  271. async processJobQueue() {
  272. const canStart = cronJobStorage.canStartNewJob();
  273. if (!canStart.canStart) {
  274. // Suppress "Cannot start new job: Maximum concurrent jobs reached" message
  275. // console.log(`Cannot start new job: ${canStart.reason}`);
  276. return;
  277. }
  278. const nextJob = cronJobStorage.getNextJob();
  279. if (!nextJob) {
  280. return; // No jobs in queue
  281. }
  282. // Start the job
  283. await this.executeJob(nextJob);
  284. }
  285. /**
  286. * Execute a job from the queue
  287. */
  288. async executeJob(queueJob) {
  289. const { jobId, jobType, jobData } = queueJob;
  290. try {
  291. // Update queue status to running
  292. cronJobStorage.updateQueueStatus(jobId, 'running', { startedAt: new Date() });
  293. // Save job status
  294. cronJobStorage.saveJobStatus(jobId, {
  295. jobType,
  296. status: 'running',
  297. progress: 0,
  298. startedAt: new Date(),
  299. ...jobData
  300. });
  301. // Execute based on job type
  302. if (jobType === 'migration') {
  303. await this.executeMigrationJob(jobId, jobData);
  304. } else if (jobType === 'board_operation') {
  305. await this.executeBoardOperationJob(jobId, jobData);
  306. } else if (jobType === 'board_migration') {
  307. await this.executeBoardMigrationJob(jobId, jobData);
  308. } else {
  309. throw new Error(`Unknown job type: ${jobType}`);
  310. }
  311. // Mark as completed
  312. cronJobStorage.updateQueueStatus(jobId, 'completed', { completedAt: new Date() });
  313. cronJobStorage.saveJobStatus(jobId, {
  314. status: 'completed',
  315. progress: 100,
  316. completedAt: new Date()
  317. });
  318. } catch (error) {
  319. console.error(`Job ${jobId} failed:`, error);
  320. // Mark as failed
  321. cronJobStorage.updateQueueStatus(jobId, 'failed', {
  322. failedAt: new Date(),
  323. error: error.message
  324. });
  325. cronJobStorage.saveJobStatus(jobId, {
  326. status: 'failed',
  327. error: error.message,
  328. failedAt: new Date()
  329. });
  330. }
  331. }
  332. /**
  333. * Execute a migration job
  334. */
  335. async executeMigrationJob(jobId, jobData) {
  336. if (!jobData) {
  337. throw new Error('Job data is required for migration execution');
  338. }
  339. const { stepId } = jobData;
  340. if (!stepId) {
  341. throw new Error('Step ID is required in job data');
  342. }
  343. const step = this.migrationSteps.find(s => s.id === stepId);
  344. if (!step) {
  345. throw new Error(`Migration step ${stepId} not found`);
  346. }
  347. // Create steps for this migration
  348. const steps = this.createMigrationSteps(step);
  349. for (let i = 0; i < steps.length; i++) {
  350. const stepData = steps[i];
  351. // Save step status
  352. cronJobStorage.saveJobStep(jobId, i, {
  353. stepName: stepData.name,
  354. status: 'running',
  355. progress: 0
  356. });
  357. // Execute step
  358. await this.executeMigrationStep(jobId, i, stepData, stepId);
  359. // Mark step as completed
  360. cronJobStorage.saveJobStep(jobId, i, {
  361. status: 'completed',
  362. progress: 100,
  363. completedAt: new Date()
  364. });
  365. // Update overall progress
  366. const progress = Math.round(((i + 1) / steps.length) * 100);
  367. cronJobStorage.saveJobStatus(jobId, { progress });
  368. }
  369. }
  370. /**
  371. * Create migration steps for a job
  372. */
  373. createMigrationSteps(step) {
  374. const steps = [];
  375. switch (step.id) {
  376. case 'board-background-color':
  377. steps.push(
  378. { name: 'Initialize board colors', duration: 1000 },
  379. { name: 'Update board documents', duration: 2000 },
  380. { name: 'Finalize changes', duration: 500 }
  381. );
  382. break;
  383. case 'add-cardcounterlist-allowed':
  384. steps.push(
  385. { name: 'Add card counter permissions', duration: 800 },
  386. { name: 'Update existing boards', duration: 1500 },
  387. { name: 'Verify permissions', duration: 700 }
  388. );
  389. break;
  390. case 'migrate-attachments-collectionFS-to-ostrioFiles':
  391. steps.push(
  392. { name: 'Scan CollectionFS attachments', duration: 2000 },
  393. { name: 'Create Meteor-Files records', duration: 3000 },
  394. { name: 'Migrate file data', duration: 5000 },
  395. { name: 'Update references', duration: 2000 },
  396. { name: 'Cleanup old data', duration: 1000 }
  397. );
  398. break;
  399. default:
  400. steps.push(
  401. { name: `Execute ${step.name}`, duration: 2000 },
  402. { name: 'Verify changes', duration: 1000 }
  403. );
  404. }
  405. return steps;
  406. }
  407. /**
  408. * Execute a migration step
  409. */
  410. async executeMigrationStep(jobId, stepIndex, stepData, stepId) {
  411. const { name, duration } = stepData;
  412. // Simulate step execution with progress updates for other migrations
  413. const progressSteps = 10;
  414. for (let i = 0; i <= progressSteps; i++) {
  415. const progress = Math.round((i / progressSteps) * 100);
  416. // Update step progress
  417. cronJobStorage.saveJobStep(jobId, stepIndex, {
  418. progress,
  419. currentAction: `Executing: ${name} (${progress}%)`
  420. });
  421. // Simulate work
  422. await new Promise(resolve => setTimeout(resolve, duration / progressSteps));
  423. }
  424. }
  425. }
  426. /**
  427. * Execute a board operation job
  428. */
  429. async executeBoardOperationJob(jobId, jobData) {
  430. const { operationType, operationData } = jobData;
  431. // Use existing board operation logic
  432. await this.executeBoardOperation(jobId, operationType, operationData);
  433. }
  434. /**
  435. * Execute a board migration job
  436. */
  437. async executeBoardMigrationJob(jobId, jobData) {
  438. const { boardId, boardTitle, migrationType } = jobData;
  439. try {
  440. // Starting board migration
  441. // Create migration steps for this board
  442. const steps = this.createBoardMigrationSteps(boardId, migrationType);
  443. for (let i = 0; i < steps.length; i++) {
  444. const stepData = steps[i];
  445. // Save step status
  446. cronJobStorage.saveJobStep(jobId, i, {
  447. stepName: stepData.name,
  448. status: 'running',
  449. progress: 0,
  450. boardId: boardId
  451. });
  452. // Execute step
  453. await this.executeBoardMigrationStep(jobId, i, stepData, boardId);
  454. // Mark step as completed
  455. cronJobStorage.saveJobStep(jobId, i, {
  456. status: 'completed',
  457. progress: 100,
  458. completedAt: new Date()
  459. });
  460. // Update overall progress
  461. const progress = Math.round(((i + 1) / steps.length) * 100);
  462. cronJobStorage.saveJobStatus(jobId, { progress });
  463. }
  464. // Mark board as migrated
  465. this.markBoardAsMigrated(boardId, migrationType);
  466. // Completed board migration
  467. } catch (error) {
  468. console.error(`Board migration failed for ${boardId}:`, error);
  469. throw error;
  470. }
  471. }
  472. /**
  473. * Create migration steps for a board
  474. */
  475. createBoardMigrationSteps(boardId, migrationType) {
  476. const steps = [];
  477. if (migrationType === 'full_board_migration') {
  478. steps.push(
  479. { name: 'Check board structure', duration: 500, type: 'validation' },
  480. { name: 'Migrate lists to swimlanes', duration: 2000, type: 'lists' },
  481. { name: 'Migrate attachments', duration: 3000, type: 'attachments' },
  482. { name: 'Update board metadata', duration: 1000, type: 'metadata' },
  483. { name: 'Verify migration', duration: 1000, type: 'verification' }
  484. );
  485. } else {
  486. // Default migration steps
  487. steps.push(
  488. { name: 'Initialize board migration', duration: 1000, type: 'init' },
  489. { name: 'Execute migration', duration: 2000, type: 'migration' },
  490. { name: 'Finalize changes', duration: 1000, type: 'finalize' }
  491. );
  492. }
  493. return steps;
  494. }
  495. /**
  496. * Execute a board migration step
  497. */
  498. async executeBoardMigrationStep(jobId, stepIndex, stepData, boardId) {
  499. const { name, duration, type } = stepData;
  500. // Simulate step execution with progress updates
  501. const progressSteps = 10;
  502. for (let i = 0; i <= progressSteps; i++) {
  503. const progress = Math.round((i / progressSteps) * 100);
  504. // Update step progress
  505. cronJobStorage.saveJobStep(jobId, stepIndex, {
  506. progress,
  507. currentAction: `Executing: ${name} (${progress}%)`
  508. });
  509. // Simulate work based on step type
  510. await this.simulateBoardMigrationWork(type, duration / progressSteps);
  511. }
  512. }
  513. /**
  514. * Simulate board migration work
  515. */
  516. async simulateBoardMigrationWork(stepType, duration) {
  517. // Simulate different types of migration work
  518. switch (stepType) {
  519. case 'validation':
  520. // Quick validation
  521. await new Promise(resolve => setTimeout(resolve, duration * 0.5));
  522. break;
  523. case 'lists':
  524. // List migration work
  525. await new Promise(resolve => setTimeout(resolve, duration));
  526. break;
  527. case 'attachments':
  528. // Attachment migration work
  529. await new Promise(resolve => setTimeout(resolve, duration * 1.2));
  530. break;
  531. case 'metadata':
  532. // Metadata update work
  533. await new Promise(resolve => setTimeout(resolve, duration * 0.8));
  534. break;
  535. case 'verification':
  536. // Verification work
  537. await new Promise(resolve => setTimeout(resolve, duration * 0.6));
  538. break;
  539. default:
  540. // Default work
  541. await new Promise(resolve => setTimeout(resolve, duration));
  542. }
  543. }
  544. /**
  545. * Mark a board as migrated
  546. */
  547. markBoardAsMigrated(boardId, migrationType) {
  548. try {
  549. // Update board with migration markers
  550. const updateQuery = {
  551. 'migrationMarkers.fullMigrationCompleted': true,
  552. 'migrationMarkers.lastMigration': new Date(),
  553. 'migrationMarkers.migrationType': migrationType
  554. };
  555. // Update the board document
  556. if (typeof Boards !== 'undefined') {
  557. Boards.update(boardId, { $set: updateQuery });
  558. }
  559. console.log(`Marked board ${boardId} as migrated`);
  560. } catch (error) {
  561. console.error(`Error marking board ${boardId} as migrated:`, error);
  562. }
  563. }
  564. /**
  565. * Create a cron job for a migration step
  566. */
  567. createCronJob(step) {
  568. SyncedCron.add({
  569. name: step.cronName,
  570. schedule: (parser) => parser.text(step.schedule),
  571. job: () => {
  572. this.runMigrationStep(step);
  573. },
  574. });
  575. }
  576. /**
  577. * Run a migration step
  578. */
  579. async runMigrationStep(step) {
  580. try {
  581. // Starting migration step
  582. cronMigrationCurrentStep.set(step.name);
  583. cronMigrationStatus.set(`Running: ${step.description}`);
  584. cronIsMigrating.set(true);
  585. // Simulate migration progress
  586. const progressSteps = 10;
  587. for (let i = 0; i <= progressSteps; i++) {
  588. step.progress = (i / progressSteps) * 100;
  589. this.updateProgress();
  590. // Simulate work
  591. await new Promise(resolve => setTimeout(resolve, 100));
  592. }
  593. // Mark as completed
  594. step.completed = true;
  595. step.progress = 100;
  596. step.status = 'completed';
  597. // Completed migration step
  598. // Update progress
  599. this.updateProgress();
  600. } catch (error) {
  601. console.error(`Migration ${step.name} failed:`, error);
  602. step.status = 'error';
  603. cronMigrationStatus.set(`Migration failed: ${error.message}`);
  604. }
  605. }
  606. /**
  607. * Start all migrations using job queue
  608. */
  609. async startAllMigrations() {
  610. if (this.isRunning) {
  611. return;
  612. }
  613. this.isRunning = true;
  614. cronIsMigrating.set(true);
  615. cronMigrationStatus.set('Adding migrations to job queue...');
  616. this.startTime = Date.now();
  617. try {
  618. // Add all migration steps to the job queue
  619. for (let i = 0; i < this.migrationSteps.length; i++) {
  620. const step = this.migrationSteps[i];
  621. if (step.completed) {
  622. continue; // Skip already completed steps
  623. }
  624. // Add to job queue
  625. const jobId = `migration_${step.id}_${Date.now()}`;
  626. cronJobStorage.addToQueue(jobId, 'migration', step.weight, {
  627. stepId: step.id,
  628. stepName: step.name,
  629. stepDescription: step.description
  630. });
  631. // Save initial job status
  632. cronJobStorage.saveJobStatus(jobId, {
  633. jobType: 'migration',
  634. status: 'pending',
  635. progress: 0,
  636. stepId: step.id,
  637. stepName: step.name,
  638. stepDescription: step.description
  639. });
  640. }
  641. cronMigrationStatus.set('Migrations added to queue. Processing will begin shortly...');
  642. // Start monitoring progress
  643. this.monitorMigrationProgress();
  644. } catch (error) {
  645. console.error('Failed to start migrations:', error);
  646. cronMigrationStatus.set(`Failed to start migrations: ${error.message}`);
  647. cronIsMigrating.set(false);
  648. this.isRunning = false;
  649. }
  650. }
  651. /**
  652. * Monitor migration progress
  653. */
  654. monitorMigrationProgress() {
  655. const monitorInterval = Meteor.setInterval(() => {
  656. const stats = cronJobStorage.getQueueStats();
  657. const incompleteJobs = cronJobStorage.getIncompleteJobs();
  658. // Update progress
  659. const totalJobs = stats.total;
  660. const completedJobs = stats.completed;
  661. const progress = totalJobs > 0 ? Math.round((completedJobs / totalJobs) * 100) : 0;
  662. cronMigrationProgress.set(progress);
  663. // Update status
  664. if (stats.running > 0) {
  665. const runningJob = incompleteJobs.find(job => job.status === 'running');
  666. if (runningJob) {
  667. cronMigrationCurrentStep.set(runningJob.stepName || 'Processing migration...');
  668. cronMigrationStatus.set(`Running: ${runningJob.stepName || 'Migration in progress'}`);
  669. }
  670. } else if (stats.pending > 0) {
  671. cronMigrationStatus.set(`${stats.pending} migrations pending in queue`);
  672. cronMigrationCurrentStep.set('Waiting for available resources...');
  673. } else if (stats.completed === totalJobs && totalJobs > 0) {
  674. // All migrations completed
  675. cronMigrationStatus.set('All migrations completed successfully!');
  676. cronMigrationProgress.set(100);
  677. cronMigrationCurrentStep.set('');
  678. // Clear status after delay
  679. setTimeout(() => {
  680. cronIsMigrating.set(false);
  681. cronMigrationStatus.set('');
  682. cronMigrationProgress.set(0);
  683. }, 3000);
  684. Meteor.clearInterval(monitorInterval);
  685. }
  686. }, 2000); // Check every 2 seconds
  687. }
  688. /**
  689. * Start a specific cron job
  690. */
  691. async startCronJob(cronName) {
  692. // Change schedule to run once
  693. const job = SyncedCron.jobs.find(j => j.name === cronName);
  694. if (job) {
  695. job.schedule = 'once';
  696. SyncedCron.start();
  697. }
  698. }
  699. /**
  700. * Wait for a cron job to complete
  701. */
  702. async waitForCronJobCompletion(step) {
  703. return new Promise((resolve) => {
  704. const checkInterval = setInterval(() => {
  705. if (step.completed || step.status === 'error') {
  706. clearInterval(checkInterval);
  707. resolve();
  708. }
  709. }, 1000);
  710. });
  711. }
  712. /**
  713. * Stop a specific cron job
  714. */
  715. stopCronJob(cronName) {
  716. SyncedCron.remove(cronName);
  717. const step = this.migrationSteps.find(s => s.cronName === cronName);
  718. if (step) {
  719. step.status = 'stopped';
  720. }
  721. this.updateCronJobsList();
  722. }
  723. /**
  724. * Pause a specific cron job
  725. */
  726. pauseCronJob(cronName) {
  727. SyncedCron.pause(cronName);
  728. const step = this.migrationSteps.find(s => s.cronName === cronName);
  729. if (step) {
  730. step.status = 'paused';
  731. }
  732. this.updateCronJobsList();
  733. }
  734. /**
  735. * Resume a specific cron job
  736. */
  737. resumeCronJob(cronName) {
  738. SyncedCron.resume(cronName);
  739. const step = this.migrationSteps.find(s => s.cronName === cronName);
  740. if (step) {
  741. step.status = 'running';
  742. }
  743. this.updateCronJobsList();
  744. }
  745. /**
  746. * Remove a cron job
  747. */
  748. removeCronJob(cronName) {
  749. SyncedCron.remove(cronName);
  750. this.migrationSteps = this.migrationSteps.filter(s => s.cronName !== cronName);
  751. this.updateCronJobsList();
  752. }
  753. /**
  754. * Add a new cron job
  755. */
  756. addCronJob(jobData) {
  757. const step = {
  758. id: jobData.id || `custom_${Date.now()}`,
  759. name: jobData.name,
  760. description: jobData.description,
  761. weight: jobData.weight || 1,
  762. completed: false,
  763. progress: 0,
  764. cronName: jobData.cronName || `custom_${Date.now()}`,
  765. schedule: jobData.schedule || 'every 1 minute',
  766. status: 'stopped'
  767. };
  768. this.migrationSteps.push(step);
  769. this.createCronJob(step);
  770. this.updateCronJobsList();
  771. }
  772. /**
  773. * Update progress variables
  774. */
  775. updateProgress() {
  776. const totalWeight = this.migrationSteps.reduce((total, step) => total + step.weight, 0);
  777. const completedWeight = this.migrationSteps.reduce((total, step) => {
  778. return total + (step.completed ? step.weight : step.progress * step.weight / 100);
  779. }, 0);
  780. const progress = Math.round((completedWeight / totalWeight) * 100);
  781. cronMigrationProgress.set(progress);
  782. cronMigrationSteps.set([...this.migrationSteps]);
  783. }
  784. /**
  785. * Update cron jobs list
  786. */
  787. updateCronJobsList() {
  788. // Check if SyncedCron is available and has jobs
  789. if (!SyncedCron || !SyncedCron.jobs || !Array.isArray(SyncedCron.jobs)) {
  790. // SyncedCron not available or no jobs yet
  791. cronJobs.set([]);
  792. return;
  793. }
  794. const jobs = SyncedCron.jobs.map(job => {
  795. const step = this.migrationSteps.find(s => s.cronName === job.name);
  796. return {
  797. name: job.name,
  798. schedule: job.schedule,
  799. status: step ? step.status : 'unknown',
  800. lastRun: job.lastRun,
  801. nextRun: job.nextRun,
  802. running: job.running
  803. };
  804. });
  805. cronJobs.set(jobs);
  806. }
  807. /**
  808. * Get all cron jobs
  809. */
  810. getAllCronJobs() {
  811. return cronJobs.get();
  812. }
  813. /**
  814. * Get migration steps
  815. */
  816. getMigrationSteps() {
  817. return this.migrationSteps;
  818. }
  819. /**
  820. * Start a long-running operation for a specific board
  821. */
  822. startBoardOperation(boardId, operationType, operationData) {
  823. const operationId = `${boardId}_${operationType}_${Date.now()}`;
  824. // Add to job queue
  825. cronJobStorage.addToQueue(operationId, 'board_operation', 3, {
  826. boardId,
  827. operationType,
  828. operationData
  829. });
  830. // Save initial job status
  831. cronJobStorage.saveJobStatus(operationId, {
  832. jobType: 'board_operation',
  833. status: 'pending',
  834. progress: 0,
  835. boardId,
  836. operationType,
  837. operationData,
  838. createdAt: new Date()
  839. });
  840. // Update board operations map for backward compatibility
  841. const operation = {
  842. id: operationId,
  843. boardId: boardId,
  844. type: operationType,
  845. data: operationData,
  846. status: 'pending',
  847. progress: 0,
  848. startTime: new Date(),
  849. endTime: null,
  850. error: null
  851. };
  852. const operations = boardOperations.get();
  853. operations.set(operationId, operation);
  854. boardOperations.set(operations);
  855. return operationId;
  856. }
  857. /**
  858. * Execute a board operation
  859. */
  860. async executeBoardOperation(operationId, operationType, operationData) {
  861. const operations = boardOperations.get();
  862. const operation = operations.get(operationId);
  863. if (!operation) {
  864. console.error(`Operation ${operationId} not found`);
  865. return;
  866. }
  867. try {
  868. console.log(`Starting board operation: ${operationType} for board ${operation.boardId}`);
  869. // Update operation status
  870. operation.status = 'running';
  871. operation.progress = 0;
  872. this.updateBoardOperation(operationId, operation);
  873. // Execute the specific operation
  874. switch (operationType) {
  875. case 'copy_board':
  876. await this.copyBoard(operationId, operationData);
  877. break;
  878. case 'move_board':
  879. await this.moveBoard(operationId, operationData);
  880. break;
  881. case 'copy_swimlane':
  882. await this.copySwimlane(operationId, operationData);
  883. break;
  884. case 'move_swimlane':
  885. await this.moveSwimlane(operationId, operationData);
  886. break;
  887. case 'copy_list':
  888. await this.copyList(operationId, operationData);
  889. break;
  890. case 'move_list':
  891. await this.moveList(operationId, operationData);
  892. break;
  893. case 'copy_card':
  894. await this.copyCard(operationId, operationData);
  895. break;
  896. case 'move_card':
  897. await this.moveCard(operationId, operationData);
  898. break;
  899. case 'copy_checklist':
  900. await this.copyChecklist(operationId, operationData);
  901. break;
  902. case 'move_checklist':
  903. await this.moveChecklist(operationId, operationData);
  904. break;
  905. default:
  906. throw new Error(`Unknown operation type: ${operationType}`);
  907. }
  908. // Mark as completed
  909. operation.status = 'completed';
  910. operation.progress = 100;
  911. operation.endTime = new Date();
  912. this.updateBoardOperation(operationId, operation);
  913. console.log(`Completed board operation: ${operationType} for board ${operation.boardId}`);
  914. } catch (error) {
  915. console.error(`Board operation ${operationType} failed:`, error);
  916. operation.status = 'error';
  917. operation.error = error.message;
  918. operation.endTime = new Date();
  919. this.updateBoardOperation(operationId, operation);
  920. }
  921. }
  922. /**
  923. * Update board operation progress
  924. */
  925. updateBoardOperation(operationId, operation) {
  926. const operations = boardOperations.get();
  927. operations.set(operationId, operation);
  928. boardOperations.set(operations);
  929. // Update progress map
  930. const progressMap = boardOperationProgress.get();
  931. progressMap.set(operationId, {
  932. progress: operation.progress,
  933. status: operation.status,
  934. error: operation.error
  935. });
  936. boardOperationProgress.set(progressMap);
  937. }
  938. /**
  939. * Copy board operation
  940. */
  941. async copyBoard(operationId, data) {
  942. const { sourceBoardId, targetBoardId, copyOptions } = data;
  943. const operation = boardOperations.get().get(operationId);
  944. // Simulate copy progress
  945. const steps = ['copying_swimlanes', 'copying_lists', 'copying_cards', 'copying_attachments', 'finalizing'];
  946. for (let i = 0; i < steps.length; i++) {
  947. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  948. this.updateBoardOperation(operationId, operation);
  949. // Simulate work
  950. await new Promise(resolve => setTimeout(resolve, 1000));
  951. }
  952. }
  953. /**
  954. * Move board operation
  955. */
  956. async moveBoard(operationId, data) {
  957. const { sourceBoardId, targetBoardId, moveOptions } = data;
  958. const operation = boardOperations.get().get(operationId);
  959. // Simulate move progress
  960. const steps = ['preparing_move', 'moving_swimlanes', 'moving_lists', 'moving_cards', 'updating_references', 'finalizing'];
  961. for (let i = 0; i < steps.length; i++) {
  962. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  963. this.updateBoardOperation(operationId, operation);
  964. // Simulate work
  965. await new Promise(resolve => setTimeout(resolve, 800));
  966. }
  967. }
  968. /**
  969. * Copy swimlane operation
  970. */
  971. async copySwimlane(operationId, data) {
  972. const { sourceSwimlaneId, targetBoardId, copyOptions } = data;
  973. const operation = boardOperations.get().get(operationId);
  974. // Simulate copy progress
  975. const steps = ['copying_swimlane', 'copying_lists', 'copying_cards', 'finalizing'];
  976. for (let i = 0; i < steps.length; i++) {
  977. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  978. this.updateBoardOperation(operationId, operation);
  979. // Simulate work
  980. await new Promise(resolve => setTimeout(resolve, 500));
  981. }
  982. }
  983. /**
  984. * Move swimlane operation
  985. */
  986. async moveSwimlane(operationId, data) {
  987. const { sourceSwimlaneId, targetBoardId, moveOptions } = data;
  988. const operation = boardOperations.get().get(operationId);
  989. // Simulate move progress
  990. const steps = ['preparing_move', 'moving_swimlane', 'updating_references', 'finalizing'];
  991. for (let i = 0; i < steps.length; i++) {
  992. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  993. this.updateBoardOperation(operationId, operation);
  994. // Simulate work
  995. await new Promise(resolve => setTimeout(resolve, 400));
  996. }
  997. }
  998. /**
  999. * Copy list operation
  1000. */
  1001. async copyList(operationId, data) {
  1002. const { sourceListId, targetBoardId, copyOptions } = data;
  1003. const operation = boardOperations.get().get(operationId);
  1004. // Simulate copy progress
  1005. const steps = ['copying_list', 'copying_cards', 'copying_attachments', 'finalizing'];
  1006. for (let i = 0; i < steps.length; i++) {
  1007. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  1008. this.updateBoardOperation(operationId, operation);
  1009. // Simulate work
  1010. await new Promise(resolve => setTimeout(resolve, 300));
  1011. }
  1012. }
  1013. /**
  1014. * Move list operation
  1015. */
  1016. async moveList(operationId, data) {
  1017. const { sourceListId, targetBoardId, moveOptions } = data;
  1018. const operation = boardOperations.get().get(operationId);
  1019. // Simulate move progress
  1020. const steps = ['preparing_move', 'moving_list', 'updating_references', 'finalizing'];
  1021. for (let i = 0; i < steps.length; i++) {
  1022. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  1023. this.updateBoardOperation(operationId, operation);
  1024. // Simulate work
  1025. await new Promise(resolve => setTimeout(resolve, 200));
  1026. }
  1027. }
  1028. /**
  1029. * Copy card operation
  1030. */
  1031. async copyCard(operationId, data) {
  1032. const { sourceCardId, targetListId, copyOptions } = data;
  1033. const operation = boardOperations.get().get(operationId);
  1034. // Simulate copy progress
  1035. const steps = ['copying_card', 'copying_attachments', 'copying_checklists', 'finalizing'];
  1036. for (let i = 0; i < steps.length; i++) {
  1037. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  1038. this.updateBoardOperation(operationId, operation);
  1039. // Simulate work
  1040. await new Promise(resolve => setTimeout(resolve, 150));
  1041. }
  1042. }
  1043. /**
  1044. * Move card operation
  1045. */
  1046. async moveCard(operationId, data) {
  1047. const { sourceCardId, targetListId, moveOptions } = data;
  1048. const operation = boardOperations.get().get(operationId);
  1049. // Simulate move progress
  1050. const steps = ['preparing_move', 'moving_card', 'updating_references', 'finalizing'];
  1051. for (let i = 0; i < steps.length; i++) {
  1052. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  1053. this.updateBoardOperation(operationId, operation);
  1054. // Simulate work
  1055. await new Promise(resolve => setTimeout(resolve, 100));
  1056. }
  1057. }
  1058. /**
  1059. * Copy checklist operation
  1060. */
  1061. async copyChecklist(operationId, data) {
  1062. const { sourceChecklistId, targetCardId, copyOptions } = data;
  1063. const operation = boardOperations.get().get(operationId);
  1064. // Simulate copy progress
  1065. const steps = ['copying_checklist', 'copying_items', 'finalizing'];
  1066. for (let i = 0; i < steps.length; i++) {
  1067. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  1068. this.updateBoardOperation(operationId, operation);
  1069. // Simulate work
  1070. await new Promise(resolve => setTimeout(resolve, 100));
  1071. }
  1072. }
  1073. /**
  1074. * Move checklist operation
  1075. */
  1076. async moveChecklist(operationId, data) {
  1077. const { sourceChecklistId, targetCardId, moveOptions } = data;
  1078. const operation = boardOperations.get().get(operationId);
  1079. // Simulate move progress
  1080. const steps = ['preparing_move', 'moving_checklist', 'finalizing'];
  1081. for (let i = 0; i < steps.length; i++) {
  1082. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  1083. this.updateBoardOperation(operationId, operation);
  1084. // Simulate work
  1085. await new Promise(resolve => setTimeout(resolve, 50));
  1086. }
  1087. }
  1088. /**
  1089. * Get board operations for a specific board
  1090. */
  1091. getBoardOperations(boardId) {
  1092. const operations = boardOperations.get();
  1093. const boardOps = [];
  1094. for (const [operationId, operation] of operations) {
  1095. if (operation.boardId === boardId) {
  1096. boardOps.push(operation);
  1097. }
  1098. }
  1099. return boardOps.sort((a, b) => b.startTime - a.startTime);
  1100. }
  1101. /**
  1102. * Get all board operations with pagination
  1103. */
  1104. getAllBoardOperations(page = 1, limit = 20, searchTerm = '') {
  1105. const operations = boardOperations.get();
  1106. const allOps = Array.from(operations.values());
  1107. // Filter by search term if provided
  1108. let filteredOps = allOps;
  1109. if (searchTerm) {
  1110. filteredOps = allOps.filter(op =>
  1111. op.boardId.toLowerCase().includes(searchTerm.toLowerCase()) ||
  1112. op.type.toLowerCase().includes(searchTerm.toLowerCase())
  1113. );
  1114. }
  1115. // Sort by start time (newest first)
  1116. filteredOps.sort((a, b) => b.startTime - a.startTime);
  1117. // Paginate
  1118. const startIndex = (page - 1) * limit;
  1119. const endIndex = startIndex + limit;
  1120. const paginatedOps = filteredOps.slice(startIndex, endIndex);
  1121. return {
  1122. operations: paginatedOps,
  1123. total: filteredOps.length,
  1124. page: page,
  1125. limit: limit,
  1126. totalPages: Math.ceil(filteredOps.length / limit)
  1127. };
  1128. }
  1129. /**
  1130. * Get board operation statistics
  1131. */
  1132. getBoardOperationStats() {
  1133. const operations = boardOperations.get();
  1134. const stats = {
  1135. total: operations.size,
  1136. running: 0,
  1137. completed: 0,
  1138. error: 0,
  1139. byType: {}
  1140. };
  1141. for (const [operationId, operation] of operations) {
  1142. stats[operation.status]++;
  1143. if (!stats.byType[operation.type]) {
  1144. stats.byType[operation.type] = 0;
  1145. }
  1146. stats.byType[operation.type]++;
  1147. }
  1148. return stats;
  1149. }
  1150. }
  1151. // Export singleton instance
  1152. export const cronMigrationManager = new CronMigrationManager();
  1153. // Initialize cron jobs on server start
  1154. Meteor.startup(() => {
  1155. cronMigrationManager.initializeCronJobs();
  1156. });
  1157. // Meteor methods for client-server communication
  1158. Meteor.methods({
  1159. 'cron.startAllMigrations'() {
  1160. if (!this.userId) {
  1161. throw new Meteor.Error('not-authorized');
  1162. }
  1163. return cronMigrationManager.startAllMigrations();
  1164. },
  1165. 'cron.startJob'(cronName) {
  1166. if (!this.userId) {
  1167. throw new Meteor.Error('not-authorized');
  1168. }
  1169. return cronMigrationManager.startCronJob(cronName);
  1170. },
  1171. 'cron.stopJob'(cronName) {
  1172. if (!this.userId) {
  1173. throw new Meteor.Error('not-authorized');
  1174. }
  1175. return cronMigrationManager.stopCronJob(cronName);
  1176. },
  1177. 'cron.pauseJob'(cronName) {
  1178. if (!this.userId) {
  1179. throw new Meteor.Error('not-authorized');
  1180. }
  1181. return cronMigrationManager.pauseCronJob(cronName);
  1182. },
  1183. 'cron.resumeJob'(cronName) {
  1184. if (!this.userId) {
  1185. throw new Meteor.Error('not-authorized');
  1186. }
  1187. return cronMigrationManager.resumeCronJob(cronName);
  1188. },
  1189. 'cron.removeJob'(cronName) {
  1190. if (!this.userId) {
  1191. throw new Meteor.Error('not-authorized');
  1192. }
  1193. return cronMigrationManager.removeCronJob(cronName);
  1194. },
  1195. 'cron.addJob'(jobData) {
  1196. if (!this.userId) {
  1197. throw new Meteor.Error('not-authorized');
  1198. }
  1199. return cronMigrationManager.addCronJob(jobData);
  1200. },
  1201. 'cron.getJobs'() {
  1202. return cronMigrationManager.getAllCronJobs();
  1203. },
  1204. 'cron.getMigrationProgress'() {
  1205. return {
  1206. progress: cronMigrationProgress.get(),
  1207. status: cronMigrationStatus.get(),
  1208. currentStep: cronMigrationCurrentStep.get(),
  1209. steps: cronMigrationSteps.get(),
  1210. isMigrating: cronIsMigrating.get()
  1211. };
  1212. },
  1213. 'cron.startBoardOperation'(boardId, operationType, operationData) {
  1214. if (!this.userId) {
  1215. throw new Meteor.Error('not-authorized');
  1216. }
  1217. return cronMigrationManager.startBoardOperation(boardId, operationType, operationData);
  1218. },
  1219. 'cron.getBoardOperations'(boardId) {
  1220. if (!this.userId) {
  1221. throw new Meteor.Error('not-authorized');
  1222. }
  1223. return cronMigrationManager.getBoardOperations(boardId);
  1224. },
  1225. 'cron.getAllBoardOperations'(page, limit, searchTerm) {
  1226. if (!this.userId) {
  1227. throw new Meteor.Error('not-authorized');
  1228. }
  1229. return cronMigrationManager.getAllBoardOperations(page, limit, searchTerm);
  1230. },
  1231. 'cron.getBoardOperationStats'() {
  1232. if (!this.userId) {
  1233. throw new Meteor.Error('not-authorized');
  1234. }
  1235. return cronMigrationManager.getBoardOperationStats();
  1236. },
  1237. 'cron.getJobDetails'(jobId) {
  1238. if (!this.userId) {
  1239. throw new Meteor.Error('not-authorized');
  1240. }
  1241. return cronJobStorage.getJobDetails(jobId);
  1242. },
  1243. 'cron.getQueueStats'() {
  1244. if (!this.userId) {
  1245. throw new Meteor.Error('not-authorized');
  1246. }
  1247. return cronJobStorage.getQueueStats();
  1248. },
  1249. 'cron.getSystemResources'() {
  1250. if (!this.userId) {
  1251. throw new Meteor.Error('not-authorized');
  1252. }
  1253. return cronJobStorage.getSystemResources();
  1254. },
  1255. 'cron.pauseJob'(jobId) {
  1256. if (!this.userId) {
  1257. throw new Meteor.Error('not-authorized');
  1258. }
  1259. cronJobStorage.updateQueueStatus(jobId, 'paused');
  1260. cronJobStorage.saveJobStatus(jobId, { status: 'paused' });
  1261. return { success: true };
  1262. },
  1263. 'cron.resumeJob'(jobId) {
  1264. if (!this.userId) {
  1265. throw new Meteor.Error('not-authorized');
  1266. }
  1267. cronJobStorage.updateQueueStatus(jobId, 'pending');
  1268. cronJobStorage.saveJobStatus(jobId, { status: 'pending' });
  1269. return { success: true };
  1270. },
  1271. 'cron.stopJob'(jobId) {
  1272. if (!this.userId) {
  1273. throw new Meteor.Error('not-authorized');
  1274. }
  1275. cronJobStorage.updateQueueStatus(jobId, 'stopped');
  1276. cronJobStorage.saveJobStatus(jobId, {
  1277. status: 'stopped',
  1278. stoppedAt: new Date()
  1279. });
  1280. return { success: true };
  1281. },
  1282. 'cron.cleanupOldJobs'(daysOld) {
  1283. if (!this.userId) {
  1284. throw new Meteor.Error('not-authorized');
  1285. }
  1286. return cronJobStorage.cleanupOldJobs(daysOld);
  1287. },
  1288. 'cron.getBoardMigrationStats'() {
  1289. if (!this.userId) {
  1290. throw new Meteor.Error('not-authorized');
  1291. }
  1292. // Import the board migration detector
  1293. const { boardMigrationDetector } = require('./boardMigrationDetector');
  1294. return boardMigrationDetector.getMigrationStats();
  1295. },
  1296. 'cron.forceBoardMigrationScan'() {
  1297. if (!this.userId) {
  1298. throw new Meteor.Error('not-authorized');
  1299. }
  1300. // Import the board migration detector
  1301. const { boardMigrationDetector } = require('./boardMigrationDetector');
  1302. return boardMigrationDetector.forceScan();
  1303. },
  1304. });