@lokalise/background-jobs-common
Advanced tools
Comparing version 5.2.0 to 6.0.0
@@ -6,2 +6,2 @@ import type { TransactionObservabilityManager } from '@lokalise/node-core'; | ||
export { createTask } from './periodic-jobs/periodicJobUtils'; | ||
export type { PeriodicJobDependencies, BackgroundJobConfiguration, LockConfiguration, JobExecutionContext, } from './periodic-jobs/periodicJobTypes'; | ||
export type { PeriodicJobDependencies, BackgroundJobConfiguration, LockConfiguration, JobExecutionContext, Schedule, } from './periodic-jobs/periodicJobTypes'; |
@@ -9,4 +9,6 @@ "use strict"; | ||
const toad_scheduler_1 = require("toad-scheduler"); | ||
const toad_scheduler_2 = require("toad-scheduler"); | ||
const periodicJobUtils_1 = require("./periodicJobUtils"); | ||
const DEFAULT_EXCLUSIVE_LOCK_SUFFIX = 'EXCLUSIVE'; | ||
const DEFAULT_LOCK_TIMEOUT = 120000; | ||
class AbstractPeriodicJob { | ||
@@ -29,3 +31,5 @@ jobId; | ||
exclusiveLockSuffix: options.singleConsumerMode?.exclusiveLockSuffix ?? 'EXCLUSIVE', | ||
lockTimeout: options.intervalInMs * 2, | ||
lockTimeout: options.schedule.intervalInMs | ||
? options.schedule.intervalInMs * 2 | ||
: DEFAULT_LOCK_TIMEOUT, | ||
...options.singleConsumerMode, | ||
@@ -42,9 +46,20 @@ }, | ||
const task = (0, periodicJobUtils_1.createTask)(this.logger, this); | ||
this.scheduler.addSimpleIntervalJob(new toad_scheduler_1.SimpleIntervalJob({ | ||
milliseconds: this.options.intervalInMs, | ||
runImmediately: true, | ||
}, task, { | ||
id: this.jobId, | ||
preventOverrun: true, | ||
})); | ||
if (this.options.schedule.intervalInMs) { | ||
this.scheduler.addSimpleIntervalJob(new toad_scheduler_2.SimpleIntervalJob({ | ||
milliseconds: this.options.schedule.intervalInMs, | ||
runImmediately: true, | ||
}, task, { | ||
id: this.jobId, | ||
preventOverrun: true, | ||
})); | ||
return; | ||
} | ||
if (this.options.schedule.cron) { | ||
this.scheduler.addCronJob(new toad_scheduler_1.CronJob(this.options.schedule.cron, task, { | ||
id: this.jobId, | ||
preventOverrun: true, | ||
})); | ||
return; | ||
} | ||
throw new Error('Invalid config, please specify intervalInMs or cron parameters'); | ||
} | ||
@@ -104,3 +119,5 @@ async dispose() { | ||
if (this.singleConsumerLock) { | ||
await this.updateMutex(this.singleConsumerLock, this.options.singleConsumerMode.lockTimeoutAfterSuccess ?? this.options.intervalInMs, this.options.singleConsumerMode.exclusiveLockSuffix, | ||
await this.updateMutex(this.singleConsumerLock, this.options.singleConsumerMode.lockTimeoutAfterSuccess ?? | ||
this.options.schedule.intervalInMs ?? | ||
DEFAULT_LOCK_TIMEOUT, this.options.singleConsumerMode.exclusiveLockSuffix, | ||
// it is fine to lose lock at this point | ||
@@ -107,0 +124,0 @@ () => { }, 0); |
@@ -5,2 +5,21 @@ import type { CommonLogger, ErrorReporter, TransactionObservabilityManager } from '@lokalise/node-core'; | ||
import type { RequestContext } from '../background-job-processor'; | ||
export type Schedule = { | ||
/** | ||
* The interval in milliseconds at which the job should run. | ||
*/ | ||
intervalInMs: number; | ||
cron?: never; | ||
} | { | ||
intervalInMs?: never; | ||
cron?: { | ||
/** | ||
* Cron expression with 5 mandatory and 1 optional positions (optional second, then minute, hour, day of month, month, day of week) | ||
*/ | ||
cronExpression: string; | ||
/** | ||
* If not specified, local timezone will be used | ||
*/ | ||
timezone?: string; | ||
}; | ||
}; | ||
export type BackgroundJobConfiguration = { | ||
@@ -11,7 +30,4 @@ /** | ||
jobId: string; | ||
schedule: Schedule; | ||
/** | ||
* The interval in milliseconds at which the job should run. | ||
*/ | ||
intervalInMs: number; | ||
/** | ||
* Allows to run the job exclusively in a single instance of the application. | ||
@@ -18,0 +34,0 @@ * The first consumer that acquires the lock will be the only one to run the job until it stops refreshing the lock. |
{ | ||
"name": "@lokalise/background-jobs-common", | ||
"version": "5.2.0", | ||
"version": "6.0.0", | ||
"files": ["dist", "LICENSE.md", "README.md"], | ||
@@ -5,0 +5,0 @@ "author": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59985
1241