cronMigrationManager.js 43 KB

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