@gravito/horizon 1.0.0-alpha.6 → 1.0.0-beta.6
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/dist/chunk-MCKGQKYU.js +15 -0
- package/dist/index.cjs +7826 -5630
- package/dist/index.d.cts +366 -0
- package/dist/index.d.ts +366 -0
- package/dist/index.js +746 -0
- package/dist/parser-EEVME3L3.js +7780 -0
- package/package.json +6 -5
- package/dist/index.mjs +0 -6346
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import { CacheManager } from '@gravito/stasis';
|
|
2
|
+
import { GravitoOrbit, PlanetCore, ActionCallback, Logger, HookManager } from 'gravito-core';
|
|
3
|
+
|
|
4
|
+
declare class CronParser {
|
|
5
|
+
/**
|
|
6
|
+
* Get the next execution date based on a cron expression.
|
|
7
|
+
*
|
|
8
|
+
* @param expression - Cron expression
|
|
9
|
+
* @param timezone - Timezone identifier
|
|
10
|
+
* @param currentDate - Reference date
|
|
11
|
+
*/
|
|
12
|
+
static nextDate(expression: string, timezone?: string, currentDate?: Date): Promise<Date>;
|
|
13
|
+
/**
|
|
14
|
+
* Check if the cron expression is due to run at the current time (minute precision).
|
|
15
|
+
*
|
|
16
|
+
* @param expression - Cron expression
|
|
17
|
+
* @param timezone - Timezone identifier
|
|
18
|
+
* @param currentDate - Reference date
|
|
19
|
+
*/
|
|
20
|
+
static isDue(expression: string, timezone?: string, currentDate?: Date): Promise<boolean>;
|
|
21
|
+
private static minuteMatches;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface LockStore {
|
|
25
|
+
/**
|
|
26
|
+
* Attempt to acquire a lock
|
|
27
|
+
* @param key The lock key
|
|
28
|
+
* @param ttlSeconds Time to live in seconds
|
|
29
|
+
* @returns true if lock acquired, false if lock already exists
|
|
30
|
+
*/
|
|
31
|
+
acquire(key: string, ttlSeconds: number): Promise<boolean>;
|
|
32
|
+
/**
|
|
33
|
+
* Release a lock
|
|
34
|
+
* @param key The lock key
|
|
35
|
+
*/
|
|
36
|
+
release(key: string): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Force acquire a lock (overwrite existing)
|
|
39
|
+
* @param key The lock key
|
|
40
|
+
* @param ttlSeconds Time to live in seconds
|
|
41
|
+
*/
|
|
42
|
+
forceAcquire(key: string, ttlSeconds: number): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Check if a lock exists
|
|
45
|
+
* @param key The lock key
|
|
46
|
+
*/
|
|
47
|
+
exists(key: string): Promise<boolean>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
declare class CacheLockStore implements LockStore {
|
|
51
|
+
private cache;
|
|
52
|
+
private prefix;
|
|
53
|
+
constructor(cache: CacheManager, prefix?: string);
|
|
54
|
+
private getKey;
|
|
55
|
+
acquire(key: string, ttlSeconds: number): Promise<boolean>;
|
|
56
|
+
release(key: string): Promise<void>;
|
|
57
|
+
forceAcquire(key: string, ttlSeconds: number): Promise<void>;
|
|
58
|
+
exists(key: string): Promise<boolean>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
declare class LockManager {
|
|
62
|
+
private store;
|
|
63
|
+
constructor(driver: 'memory' | 'cache' | LockStore, context?: {
|
|
64
|
+
cache?: CacheManager;
|
|
65
|
+
});
|
|
66
|
+
acquire(key: string, ttlSeconds: number): Promise<boolean>;
|
|
67
|
+
release(key: string): Promise<void>;
|
|
68
|
+
forceAcquire(key: string, ttlSeconds: number): Promise<void>;
|
|
69
|
+
exists(key: string): Promise<boolean>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
declare class MemoryLockStore implements LockStore {
|
|
73
|
+
private locks;
|
|
74
|
+
acquire(key: string, ttlSeconds: number): Promise<boolean>;
|
|
75
|
+
release(key: string): Promise<void>;
|
|
76
|
+
forceAcquire(key: string, ttlSeconds: number): Promise<void>;
|
|
77
|
+
exists(key: string): Promise<boolean>;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
declare class OrbitHorizon implements GravitoOrbit {
|
|
81
|
+
/**
|
|
82
|
+
* Install the Horizon Orbit into PlanetCore.
|
|
83
|
+
*
|
|
84
|
+
* @param core - The PlanetCore instance.
|
|
85
|
+
*/
|
|
86
|
+
install(core: PlanetCore): void;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
interface ProcessResult {
|
|
90
|
+
exitCode: number;
|
|
91
|
+
stdout: string;
|
|
92
|
+
stderr: string;
|
|
93
|
+
success: boolean;
|
|
94
|
+
}
|
|
95
|
+
declare function runProcess(command: string): Promise<ProcessResult>;
|
|
96
|
+
declare class Process {
|
|
97
|
+
static run(command: string): Promise<ProcessResult>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
interface ScheduledTask {
|
|
101
|
+
name: string;
|
|
102
|
+
expression: string;
|
|
103
|
+
timezone: string;
|
|
104
|
+
callback: () => void | Promise<void>;
|
|
105
|
+
shouldRunOnOneServer: boolean;
|
|
106
|
+
lockTtl: number;
|
|
107
|
+
background: boolean;
|
|
108
|
+
nodeRole?: string;
|
|
109
|
+
command?: string;
|
|
110
|
+
onSuccessCallbacks: ActionCallback[];
|
|
111
|
+
onFailureCallbacks: ActionCallback[];
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Fluent API for defining task schedules.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* scheduler.task('backup')
|
|
118
|
+
* .daily()
|
|
119
|
+
* .at('02:00')
|
|
120
|
+
* .onOneServer()
|
|
121
|
+
*/
|
|
122
|
+
declare class TaskSchedule {
|
|
123
|
+
private task;
|
|
124
|
+
/**
|
|
125
|
+
* Create a new TaskSchedule instance.
|
|
126
|
+
*
|
|
127
|
+
* @param name - The unique name of the task.
|
|
128
|
+
* @param callback - The function to execute.
|
|
129
|
+
*/
|
|
130
|
+
constructor(name: string, callback: () => void | Promise<void>);
|
|
131
|
+
/**
|
|
132
|
+
* Set a custom cron expression.
|
|
133
|
+
*
|
|
134
|
+
* @param expression - Standard cron expression (e.g., "* * * * *")
|
|
135
|
+
* @returns The TaskSchedule instance.
|
|
136
|
+
*/
|
|
137
|
+
cron(expression: string): this;
|
|
138
|
+
/**
|
|
139
|
+
* Run the task every minute.
|
|
140
|
+
*
|
|
141
|
+
* @returns The TaskSchedule instance.
|
|
142
|
+
*/
|
|
143
|
+
everyMinute(): this;
|
|
144
|
+
/**
|
|
145
|
+
* Run the task every 5 minutes.
|
|
146
|
+
*
|
|
147
|
+
* @returns The TaskSchedule instance.
|
|
148
|
+
*/
|
|
149
|
+
everyFiveMinutes(): this;
|
|
150
|
+
/**
|
|
151
|
+
* Run the task every 10 minutes.
|
|
152
|
+
*
|
|
153
|
+
* @returns The TaskSchedule instance.
|
|
154
|
+
*/
|
|
155
|
+
everyTenMinutes(): this;
|
|
156
|
+
/**
|
|
157
|
+
* Run the task every 15 minutes.
|
|
158
|
+
*
|
|
159
|
+
* @returns The TaskSchedule instance.
|
|
160
|
+
*/
|
|
161
|
+
everyFifteenMinutes(): this;
|
|
162
|
+
/**
|
|
163
|
+
* Run the task every 30 minutes.
|
|
164
|
+
*
|
|
165
|
+
* @returns The TaskSchedule instance.
|
|
166
|
+
*/
|
|
167
|
+
everyThirtyMinutes(): this;
|
|
168
|
+
/**
|
|
169
|
+
* Run the task hourly (at minute 0).
|
|
170
|
+
*
|
|
171
|
+
* @returns The TaskSchedule instance.
|
|
172
|
+
*/
|
|
173
|
+
hourly(): this;
|
|
174
|
+
/**
|
|
175
|
+
* Run the task hourly at a specific minute.
|
|
176
|
+
*
|
|
177
|
+
* @param minute - Minute (0-59)
|
|
178
|
+
* @returns The TaskSchedule instance.
|
|
179
|
+
*/
|
|
180
|
+
hourlyAt(minute: number): this;
|
|
181
|
+
/**
|
|
182
|
+
* Run the task daily at midnight (00:00).
|
|
183
|
+
*
|
|
184
|
+
* @returns The TaskSchedule instance.
|
|
185
|
+
*/
|
|
186
|
+
daily(): this;
|
|
187
|
+
/**
|
|
188
|
+
* Run the task daily at a specific time.
|
|
189
|
+
*
|
|
190
|
+
* @param time - Time in "HH:mm" format (24h)
|
|
191
|
+
* @returns The TaskSchedule instance.
|
|
192
|
+
*/
|
|
193
|
+
dailyAt(time: string): this;
|
|
194
|
+
/**
|
|
195
|
+
* Run the task weekly on Sunday at midnight.
|
|
196
|
+
*
|
|
197
|
+
* @returns The TaskSchedule instance.
|
|
198
|
+
*/
|
|
199
|
+
weekly(): this;
|
|
200
|
+
/**
|
|
201
|
+
* Run the task weekly on a specific day and time.
|
|
202
|
+
*
|
|
203
|
+
* @param day - Day of week (0-7, 0 or 7 is Sunday)
|
|
204
|
+
* @param time - Time in "HH:mm" format (default "00:00")
|
|
205
|
+
* @returns The TaskSchedule instance.
|
|
206
|
+
*/
|
|
207
|
+
weeklyOn(day: number, time?: string): this;
|
|
208
|
+
/**
|
|
209
|
+
* Run the task monthly on the 1st day at midnight.
|
|
210
|
+
*
|
|
211
|
+
* @returns The TaskSchedule instance.
|
|
212
|
+
*/
|
|
213
|
+
monthly(): this;
|
|
214
|
+
/**
|
|
215
|
+
* Run the task monthly on a specific day and time.
|
|
216
|
+
*
|
|
217
|
+
* @param day - Day of month (1-31)
|
|
218
|
+
* @param time - Time in "HH:mm" format (default "00:00")
|
|
219
|
+
* @returns The TaskSchedule instance.
|
|
220
|
+
*/
|
|
221
|
+
monthlyOn(day: number, time?: string): this;
|
|
222
|
+
/**
|
|
223
|
+
* Set the timezone for the task execution.
|
|
224
|
+
*
|
|
225
|
+
* @param timezone - Timezone identifier (e.g., "Asia/Taipei", "UTC")
|
|
226
|
+
* @returns The TaskSchedule instance.
|
|
227
|
+
*/
|
|
228
|
+
timezone(timezone: string): this;
|
|
229
|
+
/**
|
|
230
|
+
* Set the time of execution for the current frequency.
|
|
231
|
+
* Useful when chaining with daily(), weekly(), etc.
|
|
232
|
+
*
|
|
233
|
+
* @param time - Time in "HH:mm" format
|
|
234
|
+
* @returns The TaskSchedule instance.
|
|
235
|
+
*/
|
|
236
|
+
at(time: string): this;
|
|
237
|
+
/**
|
|
238
|
+
* Ensure task runs on only one server at a time (Distributed Locking).
|
|
239
|
+
* Requires a configured LockStore (Cache or Redis).
|
|
240
|
+
*
|
|
241
|
+
* @param lockTtlSeconds - Time in seconds to hold the lock (default 300)
|
|
242
|
+
* @returns The TaskSchedule instance.
|
|
243
|
+
*/
|
|
244
|
+
onOneServer(lockTtlSeconds?: number): this;
|
|
245
|
+
/**
|
|
246
|
+
* Alias for onOneServer.
|
|
247
|
+
* Prevents overlapping executions of the same task.
|
|
248
|
+
*
|
|
249
|
+
* @param expiresAt - Lock TTL in seconds
|
|
250
|
+
* @returns The TaskSchedule instance.
|
|
251
|
+
*/
|
|
252
|
+
withoutOverlapping(expiresAt?: number): this;
|
|
253
|
+
/**
|
|
254
|
+
* Run task in background (do not wait for completion in the loop).
|
|
255
|
+
* Note: In Node.js non-blocking environment this is largely semantic,
|
|
256
|
+
* but affects how we handle error catching and lock release.
|
|
257
|
+
*
|
|
258
|
+
* @returns The TaskSchedule instance.
|
|
259
|
+
*/
|
|
260
|
+
runInBackground(): this;
|
|
261
|
+
/**
|
|
262
|
+
* Restrict task execution to a specific node role.
|
|
263
|
+
*
|
|
264
|
+
* @param role - The required node role (e.g., 'api', 'worker')
|
|
265
|
+
* @returns The TaskSchedule instance.
|
|
266
|
+
*/
|
|
267
|
+
onNode(role: string): this;
|
|
268
|
+
/**
|
|
269
|
+
* Set the command string for exec tasks.
|
|
270
|
+
*
|
|
271
|
+
* @param command - The command string.
|
|
272
|
+
* @returns The TaskSchedule instance.
|
|
273
|
+
* @internal
|
|
274
|
+
*/
|
|
275
|
+
setCommand(command: string): this;
|
|
276
|
+
/**
|
|
277
|
+
* Register a callback to run on task success.
|
|
278
|
+
*
|
|
279
|
+
* @param callback - The callback function.
|
|
280
|
+
* @returns The TaskSchedule instance.
|
|
281
|
+
*/
|
|
282
|
+
onSuccess(callback: ActionCallback): this;
|
|
283
|
+
/**
|
|
284
|
+
* Register a callback to run on task failure.
|
|
285
|
+
*
|
|
286
|
+
* @param callback - The callback function.
|
|
287
|
+
* @returns The TaskSchedule instance.
|
|
288
|
+
*/
|
|
289
|
+
onFailure(callback: ActionCallback): this;
|
|
290
|
+
/**
|
|
291
|
+
* Set a description for the task (useful for listing).
|
|
292
|
+
*
|
|
293
|
+
* @param _text - The description text.
|
|
294
|
+
* @returns The TaskSchedule instance.
|
|
295
|
+
*/
|
|
296
|
+
description(_text: string): this;
|
|
297
|
+
/**
|
|
298
|
+
* Get the underlying task configuration.
|
|
299
|
+
*
|
|
300
|
+
* @returns The ScheduledTask object.
|
|
301
|
+
*/
|
|
302
|
+
getTask(): ScheduledTask;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Core Scheduler Manager responsible for managing and executing tasks.
|
|
307
|
+
*/
|
|
308
|
+
declare class SchedulerManager {
|
|
309
|
+
lockManager: LockManager;
|
|
310
|
+
private logger?;
|
|
311
|
+
private hooks?;
|
|
312
|
+
private currentNodeRole?;
|
|
313
|
+
private tasks;
|
|
314
|
+
constructor(lockManager: LockManager, logger?: Logger | undefined, hooks?: HookManager | undefined, currentNodeRole?: string | undefined);
|
|
315
|
+
/**
|
|
316
|
+
* Define a new scheduled task.
|
|
317
|
+
*
|
|
318
|
+
* @param name - Unique name for the task
|
|
319
|
+
* @param callback - Function to execute
|
|
320
|
+
* @returns The newly created TaskSchedule.
|
|
321
|
+
*/
|
|
322
|
+
task(name: string, callback: () => void | Promise<void>): TaskSchedule;
|
|
323
|
+
/**
|
|
324
|
+
* Define a new scheduled command execution task.
|
|
325
|
+
*
|
|
326
|
+
* @param name - Unique name for the task
|
|
327
|
+
* @param command - Shell command to execute
|
|
328
|
+
* @returns The newly created TaskSchedule.
|
|
329
|
+
*/
|
|
330
|
+
exec(name: string, command: string): TaskSchedule;
|
|
331
|
+
/**
|
|
332
|
+
* Add a pre-configured task schedule object.
|
|
333
|
+
*
|
|
334
|
+
* @param schedule - The task schedule to add.
|
|
335
|
+
*/
|
|
336
|
+
add(schedule: TaskSchedule): void;
|
|
337
|
+
/**
|
|
338
|
+
* Get all registered task definitions.
|
|
339
|
+
*
|
|
340
|
+
* @returns An array of scheduled tasks.
|
|
341
|
+
*/
|
|
342
|
+
getTasks(): ScheduledTask[];
|
|
343
|
+
/**
|
|
344
|
+
* Trigger the scheduler to check and run due tasks.
|
|
345
|
+
* This is typically called every minute by a system cron or worker loop.
|
|
346
|
+
*
|
|
347
|
+
* @param date - The current reference date (default: now)
|
|
348
|
+
* @returns A promise that resolves when the scheduler run is complete.
|
|
349
|
+
*/
|
|
350
|
+
run(date?: Date): Promise<void>;
|
|
351
|
+
/**
|
|
352
|
+
* Execute a specific task with locking logic.
|
|
353
|
+
*
|
|
354
|
+
* @param task - The task to execute.
|
|
355
|
+
* @internal
|
|
356
|
+
*/
|
|
357
|
+
runTask(task: ScheduledTask, date?: Date): Promise<void>;
|
|
358
|
+
/**
|
|
359
|
+
* Execute the task callback and handle hooks.
|
|
360
|
+
*
|
|
361
|
+
* @param task - The task to execute.
|
|
362
|
+
*/
|
|
363
|
+
private executeTask;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export { CacheLockStore, CronParser, LockManager, type LockStore, MemoryLockStore, OrbitHorizon, Process, type ProcessResult, type ScheduledTask, SchedulerManager, TaskSchedule, runProcess };
|