s3db.js 11.0.1 → 11.0.3

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.
@@ -163,7 +163,6 @@ export class SchedulerPlugin extends Plugin {
163
163
  };
164
164
 
165
165
  this.database = null;
166
- this.lockResource = null;
167
166
  this.jobs = new Map();
168
167
  this.activeJobs = new Map();
169
168
  this.timers = new Map();
@@ -218,9 +217,7 @@ export class SchedulerPlugin extends Plugin {
218
217
  }
219
218
 
220
219
  async onInstall() {
221
-
222
- // Create lock resource for distributed locking
223
- await this._createLockResource();
220
+ // Locks are now managed by PluginStorage with TTL - no Resource needed
224
221
 
225
222
  // Create job execution history resource
226
223
  if (this.config.persistJobs) {
@@ -258,27 +255,6 @@ export class SchedulerPlugin extends Plugin {
258
255
  this.emit('initialized', { jobs: this.jobs.size });
259
256
  }
260
257
 
261
- async _createLockResource() {
262
- const [ok, err, lockResource] = await tryFn(() =>
263
- this.database.createResource({
264
- name: 'plg_scheduler_job_locks',
265
- attributes: {
266
- id: 'string|required',
267
- jobName: 'string|required',
268
- lockedAt: 'number|required',
269
- instanceId: 'string|optional'
270
- },
271
- behavior: 'body-only',
272
- timestamps: false
273
- })
274
- );
275
-
276
- if (!ok && !this.database.resources.plg_scheduler_job_locks) {
277
- throw new Error(`Failed to create lock resource: ${err?.message}`);
278
- }
279
-
280
- this.lockResource = ok ? lockResource : this.database.resources.plg_scheduler_job_locks;
281
- }
282
258
 
283
259
  async _createJobHistoryResource() {
284
260
  const [ok] = await tryFn(() => this.database.createResource({
@@ -416,19 +392,17 @@ export class SchedulerPlugin extends Plugin {
416
392
  // Mark as active immediately (will be updated with executionId later)
417
393
  this.activeJobs.set(jobName, 'acquiring-lock');
418
394
 
419
- // Acquire distributed lock to prevent concurrent execution across instances
420
- const lockId = `lock-${jobName}`;
421
- const [lockAcquired, lockErr] = await tryFn(() =>
422
- this.lockResource.insert({
423
- id: lockId,
424
- jobName,
425
- lockedAt: Date.now(),
426
- instanceId: process.pid ? String(process.pid) : 'unknown'
427
- })
428
- );
395
+ // Acquire distributed lock with TTL to prevent concurrent execution across instances
396
+ const storage = this.getStorage();
397
+ const lockKey = `job-${jobName}`;
398
+ const lock = await storage.acquireLock(lockKey, {
399
+ ttl: Math.ceil(job.timeout / 1000) + 60, // Job timeout + 60 seconds buffer
400
+ timeout: 0, // Don't wait if locked
401
+ workerId: process.pid ? String(process.pid) : 'unknown'
402
+ });
429
403
 
430
404
  // If lock couldn't be acquired, another instance is executing this job
431
- if (!lockAcquired) {
405
+ if (!lock) {
432
406
  if (this.config.verbose) {
433
407
  console.log(`[SchedulerPlugin] Job '${jobName}' already running on another instance`);
434
408
  }
@@ -577,7 +551,7 @@ export class SchedulerPlugin extends Plugin {
577
551
  }
578
552
  } finally {
579
553
  // Always release the distributed lock
580
- await tryFn(() => this.lockResource.delete(lockId));
554
+ await tryFn(() => storage.releaseLock(lockKey));
581
555
  }
582
556
  }
583
557