@@ -9,18 +9,13 @@ const rabbit = require('../lib/rabbitfactory');
99const config = require ( '../lib/config' ) ;
1010const constants = require ( '../lib/constants' ) ;
1111const Job = require ( '../models/job' ) ;
12- // Don't remove. Loading this causes logger to start
13- const logger = require ( '../lib/logwriter' ) ; // eslint-disable-line no-unused-vars
14- // Don't remove. Loading this causes status to start
15- const status = require ( '../lib/mqstatus' ) ; // eslint-disable-line no-unused-vars
16- // Don't remove. Loading this causes jobwatcher to start
17- const jobWatcher = require ( '../lib/k8s/jobwatcher' ) ; // eslint-disable-line no-unused-vars
18- // Don't remove. Loading this causes eventwatcher to start
19- const eventWatcher = require ( '../lib/k8s/eventwatcher' ) ; // eslint-disable-line no-unused-vars
20- // Don't remove. Loading this causes the zombieRuns to start
21- const zombieRuns = require ( '../lib/k8s/zombieruns' ) ; // eslint-disable-line no-unused-vars
22- // Don't remove. Loading this causes the cleanupJobs to start
23- const jobCleanup = require ( '../lib/k8s/jobcleanup' ) ; // eslint-disable-line no-unused-vars
12+
13+ const logwriter = require ( '../lib/logwriter' ) ;
14+ const status = require ( '../lib/mqstatus' ) ;
15+ const jobWatcher = require ( '../lib/k8s/jobwatcher' ) ;
16+ const eventWatcher = require ( '../lib/k8s/eventwatcher' ) ;
17+ const zombieRuns = require ( '../lib/k8s/zombieruns' ) ;
18+ const jobCleanup = require ( '../lib/k8s/jobcleanup' ) ;
2419
2520const iostatus = require ( '../lib/iostatus' ) ;
2621
@@ -30,6 +25,14 @@ const _loadedJobs = {};
3025
3126( async ( ) => {
3227 try {
28+ await rabbit . initialize ( ) ;
29+ debug ( 'starting services' ) ;
30+ status . start ( ) ;
31+ logwriter . start ( ) ;
32+ eventWatcher . start ( ) ;
33+ jobWatcher . start ( ) ;
34+ zombieRuns . start ( ) ;
35+ jobCleanup . start ( ) ;
3336 const jobs = await Job . loadAllJobs ( ) ;
3437 debug ( 'loading jobs' ) ;
3538 jobs . forEach ( ( job ) => {
@@ -43,6 +46,56 @@ const _loadedJobs = {};
4346
4447 console . info ( 'Scheduler started' ) ;
4548 debug ( 'Scheduler started connected to %s' , config . db ) ;
49+
50+ // Used so scheduler is notified of changes and can add/remove/change jobs
51+ rabbit . subscribe ( constants . QUEUES . SCHEDULER , async ( message ) => {
52+ async function updateJob ( jobId ) {
53+ deleteJob ( jobId , true ) ;
54+
55+ const dbJob = await Job . findById ( new ObjectId ( jobId ) ) ;
56+ _loadedJobs [ jobId ] = dbJob ;
57+ dbJob . startCron ( ) ;
58+ debug ( 'updated jobId: %s' , jobId ) ;
59+ iostatus . sendJobChange ( jobId ) ;
60+ }
61+
62+ function deleteJob ( jobId , partOfUpdate ) {
63+ if ( _loadedJobs [ jobId ] ) {
64+ // Get it
65+ const loadedJob = _loadedJobs [ jobId ] ;
66+ // Remove it from list
67+ delete _loadedJobs [ jobId ] ;
68+ // Stop it
69+ loadedJob . stopCron ( ) ;
70+ debug ( 'removed jobId: %s' , jobId ) ;
71+ }
72+
73+ if ( ! partOfUpdate ) {
74+ iostatus . sendJobChange ( jobId ) ;
75+ }
76+ }
77+
78+ switch ( message . action ) {
79+ case constants . SCHEDULERACTION . NEW :
80+ // New job has been added. Make sure to handle case where
81+ // message is old and job has already been loaded in scheduler
82+ // if this is the case we ignore the message
83+ await updateJob ( message . jobId ) ;
84+ break ;
85+
86+ case constants . SCHEDULERACTION . DELETED :
87+ // Job has been deleted
88+ deleteJob ( message . jobId , false ) ;
89+ break ;
90+
91+ case constants . SCHEDULERACTION . UPDATED :
92+ // Job has changed
93+ await updateJob ( message . jobId ) ;
94+ break ;
95+ }
96+
97+ return true ;
98+ } ) ;
4699 }
47100 catch ( err ) {
48101 console . error ( 'Error loading jobs' , err ) ;
@@ -51,52 +104,3 @@ const _loadedJobs = {};
51104} ) ( ) ;
52105
53106
54- // Used so scheduler is notified of changes and can add/remove/change jobs
55- rabbit . subscribe ( constants . QUEUES . SCHEDULER , async ( message ) => {
56- async function updateJob ( jobId ) {
57- deleteJob ( jobId , true ) ;
58-
59- const dbJob = await Job . findById ( new ObjectId ( jobId ) ) ;
60- _loadedJobs [ jobId ] = dbJob ;
61- dbJob . startCron ( ) ;
62- debug ( 'updated jobId: %s' , jobId ) ;
63- iostatus . sendJobChange ( jobId ) ;
64- }
65-
66- function deleteJob ( jobId , partOfUpdate ) {
67- if ( _loadedJobs [ jobId ] ) {
68- // Get it
69- const loadedJob = _loadedJobs [ jobId ] ;
70- // Remove it from list
71- delete _loadedJobs [ jobId ] ;
72- // Stop it
73- loadedJob . stopCron ( ) ;
74- debug ( 'removed jobId: %s' , jobId ) ;
75- }
76-
77- if ( ! partOfUpdate ) {
78- iostatus . sendJobChange ( jobId ) ;
79- }
80- }
81-
82- switch ( message . action ) {
83- case constants . SCHEDULERACTION . NEW :
84- // New job has been added. Make sure to handle case where
85- // message is old and job has already been loaded in scheduler
86- // if this is the case we ignore the message
87- await updateJob ( message . jobId ) ;
88- break ;
89-
90- case constants . SCHEDULERACTION . DELETED :
91- // Job has been deleted
92- deleteJob ( message . jobId , false ) ;
93- break ;
94-
95- case constants . SCHEDULERACTION . UPDATED :
96- // Job has changed
97- await updateJob ( message . jobId ) ;
98- break ;
99- }
100-
101- return true ;
102- } ) ;
0 commit comments