@@ -10,6 +10,7 @@ import { env } from 'src/configurations/env';
1010import {
1111 MOODLE_SYNC_JOB_NAME ,
1212 MOODLE_SYNC_CONFIG_KEY ,
13+ MOODLE_SYNC_ENABLED_CONFIG_KEY ,
1314 MOODLE_SYNC_INTERVAL_DEFAULTS ,
1415 MOODLE_SYNC_MIN_INTERVAL_MINUTES ,
1516 minutesToCron ,
@@ -20,6 +21,7 @@ export class MoodleSyncScheduler implements OnModuleInit {
2021 private readonly logger = new Logger ( MoodleSyncScheduler . name ) ;
2122 private currentIntervalMinutes : number ;
2223 private currentCronExpression : string ;
24+ private isEnabled : boolean ;
2325
2426 constructor (
2527 @InjectQueue ( QueueName . MOODLE_SYNC ) private readonly syncQueue : Queue ,
@@ -29,18 +31,20 @@ export class MoodleSyncScheduler implements OnModuleInit {
2931
3032 async onModuleInit ( ) {
3133 const interval = await this . resolveInterval ( ) ;
34+ const enabled = await this . resolveEnabled ( ) ;
3235 this . currentIntervalMinutes = interval ;
3336 this . currentCronExpression = minutesToCron ( interval ) ;
37+ this . isEnabled = enabled ;
3438
3539 const job = CronJob . from ( {
3640 cronTime : this . currentCronExpression ,
3741 onTick : ( ) => this . handleScheduledSync ( ) ,
38- start : true ,
42+ start : enabled ,
3943 } ) ;
4044
4145 this . schedulerRegistry . addCronJob ( MOODLE_SYNC_JOB_NAME , job ) ;
4246 this . logger . log (
43- `Sync scheduler initialized: every ${ interval } min (${ this . currentCronExpression } )` ,
47+ `Sync scheduler initialized: every ${ interval } min (${ this . currentCronExpression } ), enabled= ${ enabled } ` ,
4448 ) ;
4549 }
4650
@@ -56,7 +60,7 @@ export class MoodleSyncScheduler implements OnModuleInit {
5660 const job = CronJob . from ( {
5761 cronTime : cronExpression ,
5862 onTick : ( ) => this . handleScheduledSync ( ) ,
59- start : true ,
63+ start : this . isEnabled ,
6064 } ) ;
6165
6266 this . schedulerRegistry . addCronJob ( MOODLE_SYNC_JOB_NAME , job ) ;
@@ -89,17 +93,49 @@ export class MoodleSyncScheduler implements OnModuleInit {
8993 intervalMinutes : number ;
9094 cronExpression : string ;
9195 nextExecution : string | null ;
96+ enabled : boolean ;
9297 } {
9398 const job = this . schedulerRegistry . getCronJob ( MOODLE_SYNC_JOB_NAME ) ;
9499 const nextDate = job . nextDate ( ) ;
95100
96101 return {
97102 intervalMinutes : this . currentIntervalMinutes ,
98103 cronExpression : this . currentCronExpression ,
99- nextExecution : nextDate ?. toISO ( ) ?? null ,
104+ nextExecution : this . isEnabled ? ( nextDate ?. toISO ( ) ?? null ) : null ,
105+ enabled : this . isEnabled ,
100106 } ;
101107 }
102108
109+ async setEnabled ( enabled : boolean ) : Promise < void > {
110+ const job = this . schedulerRegistry . getCronJob ( MOODLE_SYNC_JOB_NAME ) ;
111+
112+ if ( enabled && ! this . isEnabled ) {
113+ job . start ( ) ;
114+ this . logger . log ( 'Sync cron job enabled' ) ;
115+ } else if ( ! enabled && this . isEnabled ) {
116+ await job . stop ( ) ;
117+ this . logger . log ( 'Sync cron job disabled' ) ;
118+ }
119+
120+ const fork = this . em . fork ( ) ;
121+ const config = await fork . findOne ( SystemConfig , {
122+ key : MOODLE_SYNC_ENABLED_CONFIG_KEY ,
123+ } ) ;
124+
125+ if ( config ) {
126+ config . value = String ( enabled ) ;
127+ } else {
128+ const newConfig = new SystemConfig ( ) ;
129+ newConfig . key = MOODLE_SYNC_ENABLED_CONFIG_KEY ;
130+ newConfig . value = String ( enabled ) ;
131+ newConfig . description = 'Whether the Moodle sync cron job is enabled' ;
132+ fork . persist ( newConfig ) ;
133+ }
134+ await fork . flush ( ) ;
135+
136+ this . isEnabled = enabled ;
137+ }
138+
103139 private async handleScheduledSync ( ) {
104140 try {
105141 await this . syncQueue . add (
@@ -164,4 +200,21 @@ export class MoodleSyncScheduler implements OnModuleInit {
164200 ) ;
165201 return defaultInterval ;
166202 }
203+
204+ private async resolveEnabled ( ) : Promise < boolean > {
205+ try {
206+ const fork = this . em . fork ( ) ;
207+ const config = await fork . findOne ( SystemConfig , {
208+ key : MOODLE_SYNC_ENABLED_CONFIG_KEY ,
209+ } ) ;
210+ if ( config ?. value ) {
211+ return config . value === 'true' ;
212+ }
213+ } catch {
214+ this . logger . warn (
215+ 'Could not read sync enabled state from database, defaulting to enabled' ,
216+ ) ;
217+ }
218+ return true ;
219+ }
167220}
0 commit comments