jm2 0.1.12 → 0.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jm2",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
4
4
  "description": "Job Manager 2 - A simple yet powerful job scheduler combining cron and at functionality",
5
5
  "type": "module",
6
6
  "main": "src/cli/index.js",
@@ -193,6 +193,34 @@ export class Scheduler {
193
193
  return nextRun;
194
194
  }
195
195
 
196
+ /**
197
+ * Find periodic jobs that are overdue (nextRun is in the past)
198
+ * This handles system sleep/wake scenarios where jobs should have run while asleep
199
+ * @param {Date} now - Current time
200
+ * @returns {Array} Array of overdue job IDs
201
+ */
202
+ findOverduePeriodicJobs(now) {
203
+ const overdueJobs = [];
204
+
205
+ for (const [id, job] of this.jobs) {
206
+ if (
207
+ job.status === JobStatus.ACTIVE &&
208
+ job.type === JobType.CRON &&
209
+ job.cron &&
210
+ job.nextRun
211
+ ) {
212
+ const timeSinceNextRun = now.getTime() - job.nextRun.getTime();
213
+ const isOverdue = timeSinceNextRun > this.checkIntervalMs * 2;
214
+
215
+ if (isOverdue) {
216
+ overdueJobs.push(id);
217
+ }
218
+ }
219
+ }
220
+
221
+ return overdueJobs;
222
+ }
223
+
196
224
  /**
197
225
  * Recalculate next run times for periodic jobs that have drifted into the past
198
226
  * This handles system sleep/wake scenarios where nextRun becomes stale
@@ -321,8 +349,26 @@ export class Scheduler {
321
349
 
322
350
  const nowDate = new Date();
323
351
 
324
- // Recalculate next run for periodic jobs that have drifted into the past
325
- // This handles system sleep/wake scenarios
352
+ // Check for overdue jobs BEFORE recalculating next run times
353
+ // This ensures jobs that should have run while system was asleep get executed
354
+ const overdueJobIds = this.findOverduePeriodicJobs(nowDate);
355
+ if (overdueJobIds.length > 0) {
356
+ this.logger.info(`Found ${overdueJobIds.length} overdue job(s) to execute after system wake`);
357
+ for (const jobId of overdueJobIds) {
358
+ const job = this.jobs.get(jobId);
359
+ if (job) {
360
+ this.logger.info(`Executing overdue job ${jobId} (${job.name || 'unnamed'})`);
361
+ // Execute immediately without waiting for the normal due jobs check
362
+ this.executeJob(job);
363
+ // Update next run time from now for the next scheduled occurrence
364
+ const nextRun = this.calculateNextRun(job, nowDate);
365
+ this.updateJobNextRun(jobId, nextRun);
366
+ }
367
+ }
368
+ }
369
+
370
+ // Recalculate next run for any remaining periodic jobs that have drifted into the past
371
+ // This handles edge cases and ensures nextRun times are in the future
326
372
  this.recalculateStalePeriodicJobs(nowDate);
327
373
 
328
374
  const dueJobs = this.getDueJobs();