cronMigrationManager.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  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. const { stepId } = jobData;
  337. const step = this.migrationSteps.find(s => s.id === stepId);
  338. if (!step) {
  339. throw new Error(`Migration step ${stepId} not found`);
  340. }
  341. // Create steps for this migration
  342. const steps = this.createMigrationSteps(step);
  343. for (let i = 0; i < steps.length; i++) {
  344. const stepData = steps[i];
  345. // Save step status
  346. cronJobStorage.saveJobStep(jobId, i, {
  347. stepName: stepData.name,
  348. status: 'running',
  349. progress: 0
  350. });
  351. // Execute step
  352. await this.executeMigrationStep(jobId, i, stepData);
  353. // Mark step as completed
  354. cronJobStorage.saveJobStep(jobId, i, {
  355. status: 'completed',
  356. progress: 100,
  357. completedAt: new Date()
  358. });
  359. // Update overall progress
  360. const progress = Math.round(((i + 1) / steps.length) * 100);
  361. cronJobStorage.saveJobStatus(jobId, { progress });
  362. }
  363. }
  364. /**
  365. * Create migration steps for a job
  366. */
  367. createMigrationSteps(step) {
  368. const steps = [];
  369. switch (step.id) {
  370. case 'board-background-color':
  371. steps.push(
  372. { name: 'Initialize board colors', duration: 1000 },
  373. { name: 'Update board documents', duration: 2000 },
  374. { name: 'Finalize changes', duration: 500 }
  375. );
  376. break;
  377. case 'add-cardcounterlist-allowed':
  378. steps.push(
  379. { name: 'Add card counter permissions', duration: 800 },
  380. { name: 'Update existing boards', duration: 1500 },
  381. { name: 'Verify permissions', duration: 700 }
  382. );
  383. break;
  384. case 'migrate-attachments-collectionFS-to-ostrioFiles':
  385. steps.push(
  386. { name: 'Scan CollectionFS attachments', duration: 2000 },
  387. { name: 'Create Meteor-Files records', duration: 3000 },
  388. { name: 'Migrate file data', duration: 5000 },
  389. { name: 'Update references', duration: 2000 },
  390. { name: 'Cleanup old data', duration: 1000 }
  391. );
  392. break;
  393. default:
  394. steps.push(
  395. { name: `Execute ${step.name}`, duration: 2000 },
  396. { name: 'Verify changes', duration: 1000 }
  397. );
  398. }
  399. return steps;
  400. }
  401. /**
  402. * Execute a migration step
  403. */
  404. async executeMigrationStep(jobId, stepIndex, stepData) {
  405. const { name, duration } = stepData;
  406. // Simulate step execution with progress updates
  407. const progressSteps = 10;
  408. for (let i = 0; i <= progressSteps; i++) {
  409. const progress = Math.round((i / progressSteps) * 100);
  410. // Update step progress
  411. cronJobStorage.saveJobStep(jobId, stepIndex, {
  412. progress,
  413. currentAction: `Executing: ${name} (${progress}%)`
  414. });
  415. // Simulate work
  416. await new Promise(resolve => setTimeout(resolve, duration / progressSteps));
  417. }
  418. }
  419. /**
  420. * Execute a board operation job
  421. */
  422. async executeBoardOperationJob(jobId, jobData) {
  423. const { operationType, operationData } = jobData;
  424. // Use existing board operation logic
  425. await this.executeBoardOperation(jobId, operationType, operationData);
  426. }
  427. /**
  428. * Execute a board migration job
  429. */
  430. async executeBoardMigrationJob(jobId, jobData) {
  431. const { boardId, boardTitle, migrationType } = jobData;
  432. try {
  433. // Starting board migration
  434. // Create migration steps for this board
  435. const steps = this.createBoardMigrationSteps(boardId, migrationType);
  436. for (let i = 0; i < steps.length; i++) {
  437. const stepData = steps[i];
  438. // Save step status
  439. cronJobStorage.saveJobStep(jobId, i, {
  440. stepName: stepData.name,
  441. status: 'running',
  442. progress: 0,
  443. boardId: boardId
  444. });
  445. // Execute step
  446. await this.executeBoardMigrationStep(jobId, i, stepData, boardId);
  447. // Mark step as completed
  448. cronJobStorage.saveJobStep(jobId, i, {
  449. status: 'completed',
  450. progress: 100,
  451. completedAt: new Date()
  452. });
  453. // Update overall progress
  454. const progress = Math.round(((i + 1) / steps.length) * 100);
  455. cronJobStorage.saveJobStatus(jobId, { progress });
  456. }
  457. // Mark board as migrated
  458. this.markBoardAsMigrated(boardId, migrationType);
  459. // Completed board migration
  460. } catch (error) {
  461. console.error(`Board migration failed for ${boardId}:`, error);
  462. throw error;
  463. }
  464. }
  465. /**
  466. * Create migration steps for a board
  467. */
  468. createBoardMigrationSteps(boardId, migrationType) {
  469. const steps = [];
  470. if (migrationType === 'full_board_migration') {
  471. steps.push(
  472. { name: 'Check board structure', duration: 500, type: 'validation' },
  473. { name: 'Migrate lists to swimlanes', duration: 2000, type: 'lists' },
  474. { name: 'Migrate attachments', duration: 3000, type: 'attachments' },
  475. { name: 'Update board metadata', duration: 1000, type: 'metadata' },
  476. { name: 'Verify migration', duration: 1000, type: 'verification' }
  477. );
  478. } else {
  479. // Default migration steps
  480. steps.push(
  481. { name: 'Initialize board migration', duration: 1000, type: 'init' },
  482. { name: 'Execute migration', duration: 2000, type: 'migration' },
  483. { name: 'Finalize changes', duration: 1000, type: 'finalize' }
  484. );
  485. }
  486. return steps;
  487. }
  488. /**
  489. * Execute a board migration step
  490. */
  491. async executeBoardMigrationStep(jobId, stepIndex, stepData, boardId) {
  492. const { name, duration, type } = stepData;
  493. // Simulate step execution with progress updates
  494. const progressSteps = 10;
  495. for (let i = 0; i <= progressSteps; i++) {
  496. const progress = Math.round((i / progressSteps) * 100);
  497. // Update step progress
  498. cronJobStorage.saveJobStep(jobId, stepIndex, {
  499. progress,
  500. currentAction: `Executing: ${name} (${progress}%)`
  501. });
  502. // Simulate work based on step type
  503. await this.simulateBoardMigrationWork(type, duration / progressSteps);
  504. }
  505. }
  506. /**
  507. * Simulate board migration work
  508. */
  509. async simulateBoardMigrationWork(stepType, duration) {
  510. // Simulate different types of migration work
  511. switch (stepType) {
  512. case 'validation':
  513. // Quick validation
  514. await new Promise(resolve => setTimeout(resolve, duration * 0.5));
  515. break;
  516. case 'lists':
  517. // List migration work
  518. await new Promise(resolve => setTimeout(resolve, duration));
  519. break;
  520. case 'attachments':
  521. // Attachment migration work
  522. await new Promise(resolve => setTimeout(resolve, duration * 1.2));
  523. break;
  524. case 'metadata':
  525. // Metadata update work
  526. await new Promise(resolve => setTimeout(resolve, duration * 0.8));
  527. break;
  528. case 'verification':
  529. // Verification work
  530. await new Promise(resolve => setTimeout(resolve, duration * 0.6));
  531. break;
  532. default:
  533. // Default work
  534. await new Promise(resolve => setTimeout(resolve, duration));
  535. }
  536. }
  537. /**
  538. * Mark a board as migrated
  539. */
  540. markBoardAsMigrated(boardId, migrationType) {
  541. try {
  542. // Update board with migration markers
  543. const updateQuery = {
  544. 'migrationMarkers.fullMigrationCompleted': true,
  545. 'migrationMarkers.lastMigration': new Date(),
  546. 'migrationMarkers.migrationType': migrationType
  547. };
  548. // Update the board document
  549. if (typeof Boards !== 'undefined') {
  550. Boards.update(boardId, { $set: updateQuery });
  551. }
  552. console.log(`Marked board ${boardId} as migrated`);
  553. } catch (error) {
  554. console.error(`Error marking board ${boardId} as migrated:`, error);
  555. }
  556. }
  557. /**
  558. * Create a cron job for a migration step
  559. */
  560. createCronJob(step) {
  561. SyncedCron.add({
  562. name: step.cronName,
  563. schedule: (parser) => parser.text(step.schedule),
  564. job: () => {
  565. this.runMigrationStep(step);
  566. },
  567. });
  568. }
  569. /**
  570. * Run a migration step
  571. */
  572. async runMigrationStep(step) {
  573. try {
  574. // Starting migration step
  575. cronMigrationCurrentStep.set(step.name);
  576. cronMigrationStatus.set(`Running: ${step.description}`);
  577. cronIsMigrating.set(true);
  578. // Simulate migration progress
  579. const progressSteps = 10;
  580. for (let i = 0; i <= progressSteps; i++) {
  581. step.progress = (i / progressSteps) * 100;
  582. this.updateProgress();
  583. // Simulate work
  584. await new Promise(resolve => setTimeout(resolve, 100));
  585. }
  586. // Mark as completed
  587. step.completed = true;
  588. step.progress = 100;
  589. step.status = 'completed';
  590. // Completed migration step
  591. // Update progress
  592. this.updateProgress();
  593. } catch (error) {
  594. console.error(`Migration ${step.name} failed:`, error);
  595. step.status = 'error';
  596. cronMigrationStatus.set(`Migration failed: ${error.message}`);
  597. }
  598. }
  599. /**
  600. * Start all migrations using job queue
  601. */
  602. async startAllMigrations() {
  603. if (this.isRunning) {
  604. return;
  605. }
  606. this.isRunning = true;
  607. cronIsMigrating.set(true);
  608. cronMigrationStatus.set('Adding migrations to job queue...');
  609. this.startTime = Date.now();
  610. try {
  611. // Add all migration steps to the job queue
  612. for (let i = 0; i < this.migrationSteps.length; i++) {
  613. const step = this.migrationSteps[i];
  614. if (step.completed) {
  615. continue; // Skip already completed steps
  616. }
  617. // Add to job queue
  618. const jobId = `migration_${step.id}_${Date.now()}`;
  619. cronJobStorage.addToQueue(jobId, 'migration', step.weight, {
  620. stepId: step.id,
  621. stepName: step.name,
  622. stepDescription: step.description
  623. });
  624. // Save initial job status
  625. cronJobStorage.saveJobStatus(jobId, {
  626. jobType: 'migration',
  627. status: 'pending',
  628. progress: 0,
  629. stepId: step.id,
  630. stepName: step.name,
  631. stepDescription: step.description
  632. });
  633. }
  634. cronMigrationStatus.set('Migrations added to queue. Processing will begin shortly...');
  635. // Start monitoring progress
  636. this.monitorMigrationProgress();
  637. } catch (error) {
  638. console.error('Failed to start migrations:', error);
  639. cronMigrationStatus.set(`Failed to start migrations: ${error.message}`);
  640. cronIsMigrating.set(false);
  641. this.isRunning = false;
  642. }
  643. }
  644. /**
  645. * Monitor migration progress
  646. */
  647. monitorMigrationProgress() {
  648. const monitorInterval = Meteor.setInterval(() => {
  649. const stats = cronJobStorage.getQueueStats();
  650. const incompleteJobs = cronJobStorage.getIncompleteJobs();
  651. // Update progress
  652. const totalJobs = stats.total;
  653. const completedJobs = stats.completed;
  654. const progress = totalJobs > 0 ? Math.round((completedJobs / totalJobs) * 100) : 0;
  655. cronMigrationProgress.set(progress);
  656. // Update status
  657. if (stats.running > 0) {
  658. const runningJob = incompleteJobs.find(job => job.status === 'running');
  659. if (runningJob) {
  660. cronMigrationCurrentStep.set(runningJob.stepName || 'Processing migration...');
  661. cronMigrationStatus.set(`Running: ${runningJob.stepName || 'Migration in progress'}`);
  662. }
  663. } else if (stats.pending > 0) {
  664. cronMigrationStatus.set(`${stats.pending} migrations pending in queue`);
  665. cronMigrationCurrentStep.set('Waiting for available resources...');
  666. } else if (stats.completed === totalJobs && totalJobs > 0) {
  667. // All migrations completed
  668. cronMigrationStatus.set('All migrations completed successfully!');
  669. cronMigrationProgress.set(100);
  670. cronMigrationCurrentStep.set('');
  671. // Clear status after delay
  672. setTimeout(() => {
  673. cronIsMigrating.set(false);
  674. cronMigrationStatus.set('');
  675. cronMigrationProgress.set(0);
  676. }, 3000);
  677. Meteor.clearInterval(monitorInterval);
  678. }
  679. }, 2000); // Check every 2 seconds
  680. }
  681. /**
  682. * Start a specific cron job
  683. */
  684. async startCronJob(cronName) {
  685. // Change schedule to run once
  686. const job = SyncedCron.jobs.find(j => j.name === cronName);
  687. if (job) {
  688. job.schedule = 'once';
  689. SyncedCron.start();
  690. }
  691. }
  692. /**
  693. * Wait for a cron job to complete
  694. */
  695. async waitForCronJobCompletion(step) {
  696. return new Promise((resolve) => {
  697. const checkInterval = setInterval(() => {
  698. if (step.completed || step.status === 'error') {
  699. clearInterval(checkInterval);
  700. resolve();
  701. }
  702. }, 1000);
  703. });
  704. }
  705. /**
  706. * Stop a specific cron job
  707. */
  708. stopCronJob(cronName) {
  709. SyncedCron.remove(cronName);
  710. const step = this.migrationSteps.find(s => s.cronName === cronName);
  711. if (step) {
  712. step.status = 'stopped';
  713. }
  714. this.updateCronJobsList();
  715. }
  716. /**
  717. * Pause a specific cron job
  718. */
  719. pauseCronJob(cronName) {
  720. SyncedCron.pause(cronName);
  721. const step = this.migrationSteps.find(s => s.cronName === cronName);
  722. if (step) {
  723. step.status = 'paused';
  724. }
  725. this.updateCronJobsList();
  726. }
  727. /**
  728. * Resume a specific cron job
  729. */
  730. resumeCronJob(cronName) {
  731. SyncedCron.resume(cronName);
  732. const step = this.migrationSteps.find(s => s.cronName === cronName);
  733. if (step) {
  734. step.status = 'running';
  735. }
  736. this.updateCronJobsList();
  737. }
  738. /**
  739. * Remove a cron job
  740. */
  741. removeCronJob(cronName) {
  742. SyncedCron.remove(cronName);
  743. this.migrationSteps = this.migrationSteps.filter(s => s.cronName !== cronName);
  744. this.updateCronJobsList();
  745. }
  746. /**
  747. * Add a new cron job
  748. */
  749. addCronJob(jobData) {
  750. const step = {
  751. id: jobData.id || `custom_${Date.now()}`,
  752. name: jobData.name,
  753. description: jobData.description,
  754. weight: jobData.weight || 1,
  755. completed: false,
  756. progress: 0,
  757. cronName: jobData.cronName || `custom_${Date.now()}`,
  758. schedule: jobData.schedule || 'every 1 minute',
  759. status: 'stopped'
  760. };
  761. this.migrationSteps.push(step);
  762. this.createCronJob(step);
  763. this.updateCronJobsList();
  764. }
  765. /**
  766. * Update progress variables
  767. */
  768. updateProgress() {
  769. const totalWeight = this.migrationSteps.reduce((total, step) => total + step.weight, 0);
  770. const completedWeight = this.migrationSteps.reduce((total, step) => {
  771. return total + (step.completed ? step.weight : step.progress * step.weight / 100);
  772. }, 0);
  773. const progress = Math.round((completedWeight / totalWeight) * 100);
  774. cronMigrationProgress.set(progress);
  775. cronMigrationSteps.set([...this.migrationSteps]);
  776. }
  777. /**
  778. * Update cron jobs list
  779. */
  780. updateCronJobsList() {
  781. // Check if SyncedCron is available and has jobs
  782. if (!SyncedCron || !SyncedCron.jobs || !Array.isArray(SyncedCron.jobs)) {
  783. // SyncedCron not available or no jobs yet
  784. cronJobs.set([]);
  785. return;
  786. }
  787. const jobs = SyncedCron.jobs.map(job => {
  788. const step = this.migrationSteps.find(s => s.cronName === job.name);
  789. return {
  790. name: job.name,
  791. schedule: job.schedule,
  792. status: step ? step.status : 'unknown',
  793. lastRun: job.lastRun,
  794. nextRun: job.nextRun,
  795. running: job.running
  796. };
  797. });
  798. cronJobs.set(jobs);
  799. }
  800. /**
  801. * Get all cron jobs
  802. */
  803. getAllCronJobs() {
  804. return cronJobs.get();
  805. }
  806. /**
  807. * Get migration steps
  808. */
  809. getMigrationSteps() {
  810. return this.migrationSteps;
  811. }
  812. /**
  813. * Start a long-running operation for a specific board
  814. */
  815. startBoardOperation(boardId, operationType, operationData) {
  816. const operationId = `${boardId}_${operationType}_${Date.now()}`;
  817. // Add to job queue
  818. cronJobStorage.addToQueue(operationId, 'board_operation', 3, {
  819. boardId,
  820. operationType,
  821. operationData
  822. });
  823. // Save initial job status
  824. cronJobStorage.saveJobStatus(operationId, {
  825. jobType: 'board_operation',
  826. status: 'pending',
  827. progress: 0,
  828. boardId,
  829. operationType,
  830. operationData,
  831. createdAt: new Date()
  832. });
  833. // Update board operations map for backward compatibility
  834. const operation = {
  835. id: operationId,
  836. boardId: boardId,
  837. type: operationType,
  838. data: operationData,
  839. status: 'pending',
  840. progress: 0,
  841. startTime: new Date(),
  842. endTime: null,
  843. error: null
  844. };
  845. const operations = boardOperations.get();
  846. operations.set(operationId, operation);
  847. boardOperations.set(operations);
  848. return operationId;
  849. }
  850. /**
  851. * Execute a board operation
  852. */
  853. async executeBoardOperation(operationId, operationType, operationData) {
  854. const operations = boardOperations.get();
  855. const operation = operations.get(operationId);
  856. if (!operation) {
  857. console.error(`Operation ${operationId} not found`);
  858. return;
  859. }
  860. try {
  861. console.log(`Starting board operation: ${operationType} for board ${operation.boardId}`);
  862. // Update operation status
  863. operation.status = 'running';
  864. operation.progress = 0;
  865. this.updateBoardOperation(operationId, operation);
  866. // Execute the specific operation
  867. switch (operationType) {
  868. case 'copy_board':
  869. await this.copyBoard(operationId, operationData);
  870. break;
  871. case 'move_board':
  872. await this.moveBoard(operationId, operationData);
  873. break;
  874. case 'copy_swimlane':
  875. await this.copySwimlane(operationId, operationData);
  876. break;
  877. case 'move_swimlane':
  878. await this.moveSwimlane(operationId, operationData);
  879. break;
  880. case 'copy_list':
  881. await this.copyList(operationId, operationData);
  882. break;
  883. case 'move_list':
  884. await this.moveList(operationId, operationData);
  885. break;
  886. case 'copy_card':
  887. await this.copyCard(operationId, operationData);
  888. break;
  889. case 'move_card':
  890. await this.moveCard(operationId, operationData);
  891. break;
  892. case 'copy_checklist':
  893. await this.copyChecklist(operationId, operationData);
  894. break;
  895. case 'move_checklist':
  896. await this.moveChecklist(operationId, operationData);
  897. break;
  898. default:
  899. throw new Error(`Unknown operation type: ${operationType}`);
  900. }
  901. // Mark as completed
  902. operation.status = 'completed';
  903. operation.progress = 100;
  904. operation.endTime = new Date();
  905. this.updateBoardOperation(operationId, operation);
  906. console.log(`Completed board operation: ${operationType} for board ${operation.boardId}`);
  907. } catch (error) {
  908. console.error(`Board operation ${operationType} failed:`, error);
  909. operation.status = 'error';
  910. operation.error = error.message;
  911. operation.endTime = new Date();
  912. this.updateBoardOperation(operationId, operation);
  913. }
  914. }
  915. /**
  916. * Update board operation progress
  917. */
  918. updateBoardOperation(operationId, operation) {
  919. const operations = boardOperations.get();
  920. operations.set(operationId, operation);
  921. boardOperations.set(operations);
  922. // Update progress map
  923. const progressMap = boardOperationProgress.get();
  924. progressMap.set(operationId, {
  925. progress: operation.progress,
  926. status: operation.status,
  927. error: operation.error
  928. });
  929. boardOperationProgress.set(progressMap);
  930. }
  931. /**
  932. * Copy board operation
  933. */
  934. async copyBoard(operationId, data) {
  935. const { sourceBoardId, targetBoardId, copyOptions } = data;
  936. const operation = boardOperations.get().get(operationId);
  937. // Simulate copy progress
  938. const steps = ['copying_swimlanes', 'copying_lists', 'copying_cards', 'copying_attachments', 'finalizing'];
  939. for (let i = 0; i < steps.length; i++) {
  940. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  941. this.updateBoardOperation(operationId, operation);
  942. // Simulate work
  943. await new Promise(resolve => setTimeout(resolve, 1000));
  944. }
  945. }
  946. /**
  947. * Move board operation
  948. */
  949. async moveBoard(operationId, data) {
  950. const { sourceBoardId, targetBoardId, moveOptions } = data;
  951. const operation = boardOperations.get().get(operationId);
  952. // Simulate move progress
  953. const steps = ['preparing_move', 'moving_swimlanes', 'moving_lists', 'moving_cards', 'updating_references', 'finalizing'];
  954. for (let i = 0; i < steps.length; i++) {
  955. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  956. this.updateBoardOperation(operationId, operation);
  957. // Simulate work
  958. await new Promise(resolve => setTimeout(resolve, 800));
  959. }
  960. }
  961. /**
  962. * Copy swimlane operation
  963. */
  964. async copySwimlane(operationId, data) {
  965. const { sourceSwimlaneId, targetBoardId, copyOptions } = data;
  966. const operation = boardOperations.get().get(operationId);
  967. // Simulate copy progress
  968. const steps = ['copying_swimlane', 'copying_lists', 'copying_cards', 'finalizing'];
  969. for (let i = 0; i < steps.length; i++) {
  970. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  971. this.updateBoardOperation(operationId, operation);
  972. // Simulate work
  973. await new Promise(resolve => setTimeout(resolve, 500));
  974. }
  975. }
  976. /**
  977. * Move swimlane operation
  978. */
  979. async moveSwimlane(operationId, data) {
  980. const { sourceSwimlaneId, targetBoardId, moveOptions } = data;
  981. const operation = boardOperations.get().get(operationId);
  982. // Simulate move progress
  983. const steps = ['preparing_move', 'moving_swimlane', 'updating_references', 'finalizing'];
  984. for (let i = 0; i < steps.length; i++) {
  985. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  986. this.updateBoardOperation(operationId, operation);
  987. // Simulate work
  988. await new Promise(resolve => setTimeout(resolve, 400));
  989. }
  990. }
  991. /**
  992. * Copy list operation
  993. */
  994. async copyList(operationId, data) {
  995. const { sourceListId, targetBoardId, copyOptions } = data;
  996. const operation = boardOperations.get().get(operationId);
  997. // Simulate copy progress
  998. const steps = ['copying_list', 'copying_cards', 'copying_attachments', 'finalizing'];
  999. for (let i = 0; i < steps.length; i++) {
  1000. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  1001. this.updateBoardOperation(operationId, operation);
  1002. // Simulate work
  1003. await new Promise(resolve => setTimeout(resolve, 300));
  1004. }
  1005. }
  1006. /**
  1007. * Move list operation
  1008. */
  1009. async moveList(operationId, data) {
  1010. const { sourceListId, targetBoardId, moveOptions } = data;
  1011. const operation = boardOperations.get().get(operationId);
  1012. // Simulate move progress
  1013. const steps = ['preparing_move', 'moving_list', 'updating_references', 'finalizing'];
  1014. for (let i = 0; i < steps.length; i++) {
  1015. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  1016. this.updateBoardOperation(operationId, operation);
  1017. // Simulate work
  1018. await new Promise(resolve => setTimeout(resolve, 200));
  1019. }
  1020. }
  1021. /**
  1022. * Copy card operation
  1023. */
  1024. async copyCard(operationId, data) {
  1025. const { sourceCardId, targetListId, copyOptions } = data;
  1026. const operation = boardOperations.get().get(operationId);
  1027. // Simulate copy progress
  1028. const steps = ['copying_card', 'copying_attachments', 'copying_checklists', 'finalizing'];
  1029. for (let i = 0; i < steps.length; i++) {
  1030. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  1031. this.updateBoardOperation(operationId, operation);
  1032. // Simulate work
  1033. await new Promise(resolve => setTimeout(resolve, 150));
  1034. }
  1035. }
  1036. /**
  1037. * Move card operation
  1038. */
  1039. async moveCard(operationId, data) {
  1040. const { sourceCardId, targetListId, moveOptions } = data;
  1041. const operation = boardOperations.get().get(operationId);
  1042. // Simulate move progress
  1043. const steps = ['preparing_move', 'moving_card', 'updating_references', 'finalizing'];
  1044. for (let i = 0; i < steps.length; i++) {
  1045. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  1046. this.updateBoardOperation(operationId, operation);
  1047. // Simulate work
  1048. await new Promise(resolve => setTimeout(resolve, 100));
  1049. }
  1050. }
  1051. /**
  1052. * Copy checklist operation
  1053. */
  1054. async copyChecklist(operationId, data) {
  1055. const { sourceChecklistId, targetCardId, copyOptions } = data;
  1056. const operation = boardOperations.get().get(operationId);
  1057. // Simulate copy progress
  1058. const steps = ['copying_checklist', 'copying_items', 'finalizing'];
  1059. for (let i = 0; i < steps.length; i++) {
  1060. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  1061. this.updateBoardOperation(operationId, operation);
  1062. // Simulate work
  1063. await new Promise(resolve => setTimeout(resolve, 100));
  1064. }
  1065. }
  1066. /**
  1067. * Move checklist operation
  1068. */
  1069. async moveChecklist(operationId, data) {
  1070. const { sourceChecklistId, targetCardId, moveOptions } = data;
  1071. const operation = boardOperations.get().get(operationId);
  1072. // Simulate move progress
  1073. const steps = ['preparing_move', 'moving_checklist', 'finalizing'];
  1074. for (let i = 0; i < steps.length; i++) {
  1075. operation.progress = Math.round(((i + 1) / steps.length) * 100);
  1076. this.updateBoardOperation(operationId, operation);
  1077. // Simulate work
  1078. await new Promise(resolve => setTimeout(resolve, 50));
  1079. }
  1080. }
  1081. /**
  1082. * Get board operations for a specific board
  1083. */
  1084. getBoardOperations(boardId) {
  1085. const operations = boardOperations.get();
  1086. const boardOps = [];
  1087. for (const [operationId, operation] of operations) {
  1088. if (operation.boardId === boardId) {
  1089. boardOps.push(operation);
  1090. }
  1091. }
  1092. return boardOps.sort((a, b) => b.startTime - a.startTime);
  1093. }
  1094. /**
  1095. * Get all board operations with pagination
  1096. */
  1097. getAllBoardOperations(page = 1, limit = 20, searchTerm = '') {
  1098. const operations = boardOperations.get();
  1099. const allOps = Array.from(operations.values());
  1100. // Filter by search term if provided
  1101. let filteredOps = allOps;
  1102. if (searchTerm) {
  1103. filteredOps = allOps.filter(op =>
  1104. op.boardId.toLowerCase().includes(searchTerm.toLowerCase()) ||
  1105. op.type.toLowerCase().includes(searchTerm.toLowerCase())
  1106. );
  1107. }
  1108. // Sort by start time (newest first)
  1109. filteredOps.sort((a, b) => b.startTime - a.startTime);
  1110. // Paginate
  1111. const startIndex = (page - 1) * limit;
  1112. const endIndex = startIndex + limit;
  1113. const paginatedOps = filteredOps.slice(startIndex, endIndex);
  1114. return {
  1115. operations: paginatedOps,
  1116. total: filteredOps.length,
  1117. page: page,
  1118. limit: limit,
  1119. totalPages: Math.ceil(filteredOps.length / limit)
  1120. };
  1121. }
  1122. /**
  1123. * Get board operation statistics
  1124. */
  1125. getBoardOperationStats() {
  1126. const operations = boardOperations.get();
  1127. const stats = {
  1128. total: operations.size,
  1129. running: 0,
  1130. completed: 0,
  1131. error: 0,
  1132. byType: {}
  1133. };
  1134. for (const [operationId, operation] of operations) {
  1135. stats[operation.status]++;
  1136. if (!stats.byType[operation.type]) {
  1137. stats.byType[operation.type] = 0;
  1138. }
  1139. stats.byType[operation.type]++;
  1140. }
  1141. return stats;
  1142. }
  1143. }
  1144. // Export singleton instance
  1145. export const cronMigrationManager = new CronMigrationManager();
  1146. // Initialize cron jobs on server start
  1147. Meteor.startup(() => {
  1148. cronMigrationManager.initializeCronJobs();
  1149. });
  1150. // Meteor methods for client-server communication
  1151. Meteor.methods({
  1152. 'cron.startAllMigrations'() {
  1153. if (!this.userId) {
  1154. throw new Meteor.Error('not-authorized');
  1155. }
  1156. return cronMigrationManager.startAllMigrations();
  1157. },
  1158. 'cron.startJob'(cronName) {
  1159. if (!this.userId) {
  1160. throw new Meteor.Error('not-authorized');
  1161. }
  1162. return cronMigrationManager.startCronJob(cronName);
  1163. },
  1164. 'cron.stopJob'(cronName) {
  1165. if (!this.userId) {
  1166. throw new Meteor.Error('not-authorized');
  1167. }
  1168. return cronMigrationManager.stopCronJob(cronName);
  1169. },
  1170. 'cron.pauseJob'(cronName) {
  1171. if (!this.userId) {
  1172. throw new Meteor.Error('not-authorized');
  1173. }
  1174. return cronMigrationManager.pauseCronJob(cronName);
  1175. },
  1176. 'cron.resumeJob'(cronName) {
  1177. if (!this.userId) {
  1178. throw new Meteor.Error('not-authorized');
  1179. }
  1180. return cronMigrationManager.resumeCronJob(cronName);
  1181. },
  1182. 'cron.removeJob'(cronName) {
  1183. if (!this.userId) {
  1184. throw new Meteor.Error('not-authorized');
  1185. }
  1186. return cronMigrationManager.removeCronJob(cronName);
  1187. },
  1188. 'cron.addJob'(jobData) {
  1189. if (!this.userId) {
  1190. throw new Meteor.Error('not-authorized');
  1191. }
  1192. return cronMigrationManager.addCronJob(jobData);
  1193. },
  1194. 'cron.getJobs'() {
  1195. return cronMigrationManager.getAllCronJobs();
  1196. },
  1197. 'cron.getMigrationProgress'() {
  1198. return {
  1199. progress: cronMigrationProgress.get(),
  1200. status: cronMigrationStatus.get(),
  1201. currentStep: cronMigrationCurrentStep.get(),
  1202. steps: cronMigrationSteps.get(),
  1203. isMigrating: cronIsMigrating.get()
  1204. };
  1205. },
  1206. 'cron.startBoardOperation'(boardId, operationType, operationData) {
  1207. if (!this.userId) {
  1208. throw new Meteor.Error('not-authorized');
  1209. }
  1210. return cronMigrationManager.startBoardOperation(boardId, operationType, operationData);
  1211. },
  1212. 'cron.getBoardOperations'(boardId) {
  1213. if (!this.userId) {
  1214. throw new Meteor.Error('not-authorized');
  1215. }
  1216. return cronMigrationManager.getBoardOperations(boardId);
  1217. },
  1218. 'cron.getAllBoardOperations'(page, limit, searchTerm) {
  1219. if (!this.userId) {
  1220. throw new Meteor.Error('not-authorized');
  1221. }
  1222. return cronMigrationManager.getAllBoardOperations(page, limit, searchTerm);
  1223. },
  1224. 'cron.getBoardOperationStats'() {
  1225. if (!this.userId) {
  1226. throw new Meteor.Error('not-authorized');
  1227. }
  1228. return cronMigrationManager.getBoardOperationStats();
  1229. },
  1230. 'cron.getJobDetails'(jobId) {
  1231. if (!this.userId) {
  1232. throw new Meteor.Error('not-authorized');
  1233. }
  1234. return cronJobStorage.getJobDetails(jobId);
  1235. },
  1236. 'cron.getQueueStats'() {
  1237. if (!this.userId) {
  1238. throw new Meteor.Error('not-authorized');
  1239. }
  1240. return cronJobStorage.getQueueStats();
  1241. },
  1242. 'cron.getSystemResources'() {
  1243. if (!this.userId) {
  1244. throw new Meteor.Error('not-authorized');
  1245. }
  1246. return cronJobStorage.getSystemResources();
  1247. },
  1248. 'cron.pauseJob'(jobId) {
  1249. if (!this.userId) {
  1250. throw new Meteor.Error('not-authorized');
  1251. }
  1252. cronJobStorage.updateQueueStatus(jobId, 'paused');
  1253. cronJobStorage.saveJobStatus(jobId, { status: 'paused' });
  1254. return { success: true };
  1255. },
  1256. 'cron.resumeJob'(jobId) {
  1257. if (!this.userId) {
  1258. throw new Meteor.Error('not-authorized');
  1259. }
  1260. cronJobStorage.updateQueueStatus(jobId, 'pending');
  1261. cronJobStorage.saveJobStatus(jobId, { status: 'pending' });
  1262. return { success: true };
  1263. },
  1264. 'cron.stopJob'(jobId) {
  1265. if (!this.userId) {
  1266. throw new Meteor.Error('not-authorized');
  1267. }
  1268. cronJobStorage.updateQueueStatus(jobId, 'stopped');
  1269. cronJobStorage.saveJobStatus(jobId, {
  1270. status: 'stopped',
  1271. stoppedAt: new Date()
  1272. });
  1273. return { success: true };
  1274. },
  1275. 'cron.cleanupOldJobs'(daysOld) {
  1276. if (!this.userId) {
  1277. throw new Meteor.Error('not-authorized');
  1278. }
  1279. return cronJobStorage.cleanupOldJobs(daysOld);
  1280. },
  1281. 'cron.getBoardMigrationStats'() {
  1282. if (!this.userId) {
  1283. throw new Meteor.Error('not-authorized');
  1284. }
  1285. // Import the board migration detector
  1286. const { boardMigrationDetector } = require('./boardMigrationDetector');
  1287. return boardMigrationDetector.getMigrationStats();
  1288. },
  1289. 'cron.forceBoardMigrationScan'() {
  1290. if (!this.userId) {
  1291. throw new Meteor.Error('not-authorized');
  1292. }
  1293. // Import the board migration detector
  1294. const { boardMigrationDetector } = require('./boardMigrationDetector');
  1295. return boardMigrationDetector.forceScan();
  1296. }
  1297. });