@baasix/baasix 0.1.53 → 0.1.56
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/README.md +3 -1
- package/dist/README.md +3 -1
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +1 -0
- package/dist/app.js.map +1 -1
- package/dist/services/ItemsService.d.ts.map +1 -1
- package/dist/services/ItemsService.js +12 -1
- package/dist/services/ItemsService.js.map +1 -1
- package/dist/services/TasksService.d.ts +100 -16
- package/dist/services/TasksService.d.ts.map +1 -1
- package/dist/services/TasksService.js +462 -96
- package/dist/services/TasksService.js.map +1 -1
- package/dist/utils/systemschema.d.ts +172 -2
- package/dist/utils/systemschema.d.ts.map +1 -1
- package/dist/utils/systemschema.js +23 -1
- package/dist/utils/systemschema.js.map +1 -1
- package/package.json +2 -2
|
@@ -2,38 +2,69 @@ import type { BackgroundTask } from '../types/index.js';
|
|
|
2
2
|
declare class TasksService {
|
|
3
3
|
private cache;
|
|
4
4
|
private cacheKey;
|
|
5
|
-
private taskRunningKey;
|
|
6
5
|
private refreshInterval;
|
|
7
6
|
private refreshIntervalId;
|
|
8
7
|
private initialized;
|
|
8
|
+
private shuttingDown;
|
|
9
|
+
private concurrency;
|
|
10
|
+
private runningCount;
|
|
11
|
+
private stallTimeout;
|
|
9
12
|
private redisClient;
|
|
10
13
|
private useTaskRedis;
|
|
11
14
|
private instanceId;
|
|
12
15
|
private lockRenewalInterval;
|
|
16
|
+
private hasInstanceLock;
|
|
17
|
+
private static readonly LOCK_KEY;
|
|
13
18
|
private static readonly LOCK_TTL_SECONDS;
|
|
14
19
|
private static readonly LOCK_RENEWAL_INTERVAL;
|
|
15
20
|
init(): Promise<void>;
|
|
16
21
|
ensureInitialized(): Promise<void>;
|
|
17
22
|
refreshCache(): Promise<void>;
|
|
18
23
|
getNotStartedTasks(): Promise<BackgroundTask[]>;
|
|
19
|
-
setTaskRunning(isRunning: boolean): Promise<void>;
|
|
20
|
-
isTaskRunning(): Promise<boolean>;
|
|
21
24
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
25
|
+
* Atomically claim a task for processing.
|
|
26
|
+
* Uses UPDATE ... WHERE task_status = 'Not started' to prevent duplicate processing.
|
|
27
|
+
* If another worker already claimed the task, returns null (0 rows updated).
|
|
24
28
|
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
29
|
+
* @param taskId - The task ID to claim
|
|
30
|
+
* @returns The claimed task record, or null if already claimed/not found
|
|
31
|
+
*/
|
|
32
|
+
claimTask(taskId: string | number): Promise<BackgroundTask | null>;
|
|
33
|
+
/**
|
|
34
|
+
* Recover tasks stuck in "Running" state beyond the stall timeout.
|
|
35
|
+
* - If retry_count < max_retries: resets to "Not started" for automatic retry
|
|
36
|
+
* - Otherwise: marks as "Error" with stall information
|
|
27
37
|
*
|
|
28
|
-
*
|
|
29
|
-
|
|
38
|
+
* Called during initialization and each periodic cache refresh.
|
|
39
|
+
*/
|
|
40
|
+
recoverStalledTasks(): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* @deprecated Use tryAcquireLock()/releaseLock() for atomic task coordination.
|
|
43
|
+
* setTaskRunning(true) now delegates to tryAcquireLock() internally.
|
|
44
|
+
*/
|
|
45
|
+
setTaskRunning(isRunning: boolean): Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Check if task processing is at capacity.
|
|
48
|
+
* Returns true when running task count >= configured TASK_CONCURRENCY.
|
|
49
|
+
* With default concurrency of 1, returns true if any task is running.
|
|
50
|
+
*/
|
|
51
|
+
isTaskRunning(): Promise<boolean>;
|
|
52
|
+
/**
|
|
53
|
+
* Get the number of currently running tasks.
|
|
54
|
+
*/
|
|
55
|
+
getRunningCount(): number;
|
|
56
|
+
/**
|
|
57
|
+
* Try to acquire a task processing slot.
|
|
58
|
+
* Respects TASK_CONCURRENCY — allows up to N concurrent tasks per instance.
|
|
59
|
+
* In multi-instance mode, only one instance can hold the processing lock at a time.
|
|
30
60
|
*
|
|
31
|
-
* @param lockTimeout -
|
|
32
|
-
* @returns True if
|
|
61
|
+
* @param lockTimeout - Redis lock TTL in seconds (multi-instance only, default: 60s)
|
|
62
|
+
* @returns True if slot acquired, false if at capacity or lock held by another instance
|
|
33
63
|
*/
|
|
34
64
|
tryAcquireLock(lockTimeout?: number): Promise<boolean>;
|
|
35
65
|
/**
|
|
36
|
-
* Start automatic lock renewal to prevent expiry during long-running tasks
|
|
66
|
+
* Start automatic lock renewal to prevent expiry during long-running tasks.
|
|
67
|
+
* Uses Lua script for atomic check-and-renew (prevents race between GET and EXPIRE).
|
|
37
68
|
*/
|
|
38
69
|
private startLockRenewal;
|
|
39
70
|
/**
|
|
@@ -41,16 +72,69 @@ declare class TasksService {
|
|
|
41
72
|
*/
|
|
42
73
|
private stopLockRenewal;
|
|
43
74
|
/**
|
|
44
|
-
* Release
|
|
45
|
-
*
|
|
46
|
-
* @returns True if
|
|
75
|
+
* Release a task processing slot.
|
|
76
|
+
* When all slots are released, the instance lock (Redis) is also released.
|
|
77
|
+
* @returns True if slot released, false otherwise
|
|
47
78
|
*/
|
|
48
79
|
releaseLock(): Promise<boolean>;
|
|
80
|
+
/**
|
|
81
|
+
* In-memory set of job locks held by this instance (single-instance fallback).
|
|
82
|
+
*/
|
|
83
|
+
private heldJobLocks;
|
|
84
|
+
/**
|
|
85
|
+
* Acquire a named distributed lock for a scheduled job.
|
|
86
|
+
* Prevents the same job from running on multiple instances simultaneously.
|
|
87
|
+
*
|
|
88
|
+
* - With Redis (`TASK_REDIS_ENABLED=true`): uses `SET NX EX` for cross-instance locking
|
|
89
|
+
* - Without Redis: uses in-memory set (prevents re-entry within same process)
|
|
90
|
+
*
|
|
91
|
+
* @param jobName - Unique job identifier (e.g., "attendance-cron", "cleanup-job")
|
|
92
|
+
* @param ttlSeconds - Lock TTL in seconds. Should be >= your job's max execution time.
|
|
93
|
+
* Lock auto-expires after this, so a crashed instance won't block forever.
|
|
94
|
+
* Default: 300 (5 minutes)
|
|
95
|
+
* @returns `true` if lock acquired, `false` if already held (by this or another instance)
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* schedule.scheduleJob(everyFifteenMinutes, async () => {
|
|
100
|
+
* const locked = await tasksService.acquireJobLock("attendance-cron", 600);
|
|
101
|
+
* if (!locked) return; // another instance is running this job
|
|
102
|
+
* try {
|
|
103
|
+
* await AttendanceUtils.ProcessScheduleAttendance(...);
|
|
104
|
+
* } finally {
|
|
105
|
+
* await tasksService.releaseJobLock("attendance-cron");
|
|
106
|
+
* }
|
|
107
|
+
* });
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
acquireJobLock(jobName: string, ttlSeconds?: number): Promise<boolean>;
|
|
111
|
+
/**
|
|
112
|
+
* Release a named job lock.
|
|
113
|
+
* Only releases if this instance owns the lock (atomic check via Lua in Redis mode).
|
|
114
|
+
*
|
|
115
|
+
* @param jobName - The job name used in acquireJobLock()
|
|
116
|
+
* @returns `true` if released, `false` if not owned or error
|
|
117
|
+
*/
|
|
118
|
+
releaseJobLock(jobName: string): Promise<boolean>;
|
|
49
119
|
startPeriodicRefresh(): void;
|
|
50
120
|
stopPeriodicRefresh(): void;
|
|
121
|
+
/**
|
|
122
|
+
* Add a task to the cached "not started" list without querying the DB.
|
|
123
|
+
* Only adds if the task is "Not started" and scheduled within 4 hours.
|
|
124
|
+
*/
|
|
125
|
+
private addTaskToCache;
|
|
126
|
+
/**
|
|
127
|
+
* Remove a task from the cached "not started" list by ID without querying the DB.
|
|
128
|
+
*/
|
|
129
|
+
private removeTaskFromCache;
|
|
130
|
+
/**
|
|
131
|
+
* Update a task in the cached "not started" list without querying the DB.
|
|
132
|
+
* Replaces the cached task object with the new document.
|
|
133
|
+
*/
|
|
134
|
+
private updateTaskInCache;
|
|
51
135
|
registerHooks(): void;
|
|
52
136
|
/**
|
|
53
|
-
* Wait for
|
|
137
|
+
* Wait for all running tasks to complete (with timeout)
|
|
54
138
|
*/
|
|
55
139
|
waitForTaskCompletion(timeoutMs?: number): Promise<void>;
|
|
56
140
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TasksService.d.ts","sourceRoot":"","sources":["../../baasix/services/TasksService.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAIxD,cAAM,YAAY;IAChB,OAAO,CAAC,KAAK,CAAa;IAC1B,OAAO,CAAC,QAAQ,CAAsC;IACtD,OAAO,CAAC,
|
|
1
|
+
{"version":3,"file":"TasksService.d.ts","sourceRoot":"","sources":["../../baasix/services/TasksService.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAIxD,cAAM,YAAY;IAChB,OAAO,CAAC,KAAK,CAAa;IAC1B,OAAO,CAAC,QAAQ,CAAsC;IACtD,OAAO,CAAC,eAAe,CAAa;IACpC,OAAO,CAAC,iBAAiB,CAA+B;IACxD,OAAO,CAAC,WAAW,CAAkB;IAGrC,OAAO,CAAC,YAAY,CAAkB;IAGtC,OAAO,CAAC,WAAW,CAAa;IAChC,OAAO,CAAC,YAAY,CAAa;IAGjC,OAAO,CAAC,YAAY,CAAe;IAGnC,OAAO,CAAC,WAAW,CAAsB;IACzC,OAAO,CAAC,YAAY,CAAkB;IACtC,OAAO,CAAC,UAAU,CAA+B;IACjD,OAAO,CAAC,mBAAmB,CAA+B;IAC1D,OAAO,CAAC,eAAe,CAAkB;IACzC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAsB;IACtD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,gBAAgB,CAAM;IAC9C,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,qBAAqB,CAAS;IAEhD,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAiErB,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC;IAMlC,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC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