|
| 1 | +import { Db } from 'mongodb'; |
| 2 | +import { RedisClientType as RedisClient } from 'redis'; |
| 3 | +interface ErrorDetails { |
| 4 | + description: string |
| 5 | + error: Error |
| 6 | + uid: null | string |
| 7 | + task?: unknown |
| 8 | +} |
| 9 | + |
| 10 | +interface ExecutedDetails { |
| 11 | + uid: string |
| 12 | + date: Date |
| 13 | + delay: number |
| 14 | + timestamp: number |
| 15 | +} |
| 16 | + |
| 17 | +type OnErrorFunc = (title: string, details: ErrorDetails) => void |
| 18 | +type OnExecutedFunc = (uid: string, details: ExecutedDetails) => void |
| 19 | +type AsyncTaskFunc = () => Promise<void> |
| 20 | +type SyncTaskFunc = () => void |
| 21 | +type SyncNextTaskFunc = (ready: (next?: Date) => void) => void |
| 22 | + |
| 23 | +export interface JoSkOption { |
| 24 | + debug?: boolean; |
| 25 | + onError?: OnErrorFunc; |
| 26 | + autoClear?: boolean; |
| 27 | + zombieTime?: number; |
| 28 | + onExecuted?: OnExecutedFunc; |
| 29 | + minRevolvingDelay?: number; |
| 30 | + maxRevolvingDelay?: number; |
| 31 | +} |
| 32 | + |
| 33 | +/** Class representing a JoSk task runner (cron). */ |
| 34 | +export class JoSk { |
| 35 | + /** |
| 36 | + * Create a JoSk instance |
| 37 | + * @param {object} opts - configuration object |
| 38 | + * @param {boolean} [opts.debug] - Enable debug logging |
| 39 | + * @param {function} [opts.onError] - Informational hook, called instead of throwing exceptions, see readme for more details |
| 40 | + * @param {boolean} [opts.autoClear] - Remove obsolete tasks (any tasks which are not found in the instance memory during runtime, but exists in the database) |
| 41 | + * @param {number} [opts.zombieTime] - Time in milliseconds, after this period of time - task will be interpreted as "zombie". This parameter allows to rescue task from "zombie mode" in case when: `ready()` wasn't called, exception during runtime was thrown, or caused by bad logic |
| 42 | + * @param {function} [opts.onExecuted] - Informational hook, called when task is finished, see readme for more details |
| 43 | + * @param {number} [opts.minRevolvingDelay] - Minimum revolving delay β the minimum delay between tasks executions in milliseconds |
| 44 | + * @param {number} [opts.maxRevolvingDelay] - Maximum revolving delay β the maximum delay between tasks executions in milliseconds |
| 45 | + */ |
| 46 | + constructor(opts?: JoSkOption); |
| 47 | + debug: boolean; |
| 48 | + onError: false | OnErrorFunc; |
| 49 | + autoClear: boolean; |
| 50 | + zombieTime: number; |
| 51 | + onExecuted: false | OnExecutedFunc; |
| 52 | + isDestroyed: boolean; |
| 53 | + minRevolvingDelay: number; |
| 54 | + maxRevolvingDelay: number; |
| 55 | + nextRevolutionTimeout: number; |
| 56 | + tasks: {}; |
| 57 | + _debug: (...args: any[]) => void; |
| 58 | + adapter: any; |
| 59 | + /** |
| 60 | + * @async |
| 61 | + * @memberOf JoSk |
| 62 | + * @name ping |
| 63 | + * @description Check package readiness and connection to Storage |
| 64 | + * @returns {Promise<object>} |
| 65 | + * @throws {mix} |
| 66 | + */ |
| 67 | + ping(): Promise<unknown>; |
| 68 | + /** |
| 69 | + * @async |
| 70 | + * @memberOf JoSk |
| 71 | + * Create recurring task (loop) |
| 72 | + * @name setInterval |
| 73 | + * @param {function} func - Function (task) to execute |
| 74 | + * @param {number} delay - Delay between task execution in milliseconds |
| 75 | + * @param {string} uid - Unique function (task) identification as a string |
| 76 | + * @returns {Promise<string>} - Timer ID |
| 77 | + */ |
| 78 | + setInterval(func: AsyncTaskFunc | SyncNextTaskFunc, delay: number, uid: string): Promise<string>; |
| 79 | + /** |
| 80 | + * @async |
| 81 | + * @memberOf JoSk |
| 82 | + * Create delayed task |
| 83 | + * @name setTimeout |
| 84 | + * @param {function} func - Function (task) to execute |
| 85 | + * @param {number} delay - Delay before task execution in milliseconds |
| 86 | + * @param {string} uid - Unique function (task) identification as a string |
| 87 | + * @returns {Promise<string>} - Timer ID |
| 88 | + */ |
| 89 | + setTimeout(func: AsyncTaskFunc | SyncTaskFunc, delay: number, uid: string): Promise<string>; |
| 90 | + /** |
| 91 | + * @async |
| 92 | + * @memberOf JoSk |
| 93 | + * Create task, which would get executed immediately and only once across multi-server setup |
| 94 | + * @name setImmediate |
| 95 | + * @param {function} func - Function (task) to execute |
| 96 | + * @param {string} uid - Unique function (task) identification as a string |
| 97 | + * @returns {Promise<string>} - Timer ID |
| 98 | + */ |
| 99 | + setImmediate(func: AsyncTaskFunc | SyncTaskFunc, uid: string): Promise<string>; |
| 100 | + /** |
| 101 | + * @async |
| 102 | + * @memberOf JoSk |
| 103 | + * Cancel (abort) current interval timer. |
| 104 | + * Must be called in a separate event loop from `.setInterval()` |
| 105 | + * @name clearInterval |
| 106 | + * @param {string|Promise<string>} timerId - Unique function (task) identification as a string, returned from `.setInterval()` |
| 107 | + * @param {function} [callback] - optional callback |
| 108 | + * @returns {Promise<boolean>} - `true` if task cleared, `false` if task doesn't exist |
| 109 | + */ |
| 110 | + clearInterval(timerId: string | Promise<string>): Promise<boolean>; |
| 111 | + /** |
| 112 | + * @async |
| 113 | + * @memberOf JoSk |
| 114 | + * Cancel (abort) current timeout timer. |
| 115 | + * Must be called in a separate event loop from `.setTimeout()` |
| 116 | + * @name clearTimeout |
| 117 | + * @param {string|Promise<string>} timerId - Unique function (task) identification as a string, returned from `.setTimeout()` |
| 118 | + * @param {function} [callback] - optional callback |
| 119 | + * @returns {Promise<boolean>} - `true` if task cleared, `false` if task doesn't exist |
| 120 | + */ |
| 121 | + clearTimeout(timerId: string | Promise<string>): Promise<boolean>; |
| 122 | + /** |
| 123 | + * @memberOf JoSk |
| 124 | + * Destroy JoSk instance and stop all tasks |
| 125 | + * @name destroy |
| 126 | + * @returns {boolean} - `true` if instance successfully destroyed, `false` if instance already destroyed |
| 127 | + */ |
| 128 | + destroy(): boolean; |
| 129 | + __checkState(): boolean; |
| 130 | + __remove(timerId: string): Promise<any>; |
| 131 | + __add(uid: string, isInterval: boolean, delay: number): Promise<void>; |
| 132 | + __execute(task: unknown): Promise<void>; |
| 133 | + __iterate(): Promise<void>; |
| 134 | + __tick(): void; |
| 135 | + __errorHandler(error: unknown, title: string, description: string, uid: string): void; |
| 136 | +} |
| 137 | + |
| 138 | +export interface MongoAdapterOption { |
| 139 | + db: Db; |
| 140 | + lockCollectionName?: string; |
| 141 | + prefix?: string; |
| 142 | + resetOnInit?: boolean; |
| 143 | +} |
| 144 | + |
| 145 | +/** Class representing MongoDB adapter for JoSk */ |
| 146 | +export class MongoAdapter { |
| 147 | + /** |
| 148 | + * Create a MongoAdapter instance |
| 149 | + * @param {JoSk} joskInstance - JoSk instance |
| 150 | + * @param {object} opts - configuration object |
| 151 | + * @param {Db} opts.db - Required, Mongo's `Db` instance, like one returned from `MongoClient#db()` method |
| 152 | + * @param {string} [opts.lockCollectionName] - custom "lock" collection name |
| 153 | + * @param {string} [opts.prefix] - prefix for scope isolation; use when creating multiple JoSK instances within the single application |
| 154 | + * @param {boolean} [opts.resetOnInit] - Make sure all old tasks is completed before setting a new one, see readme for more details |
| 155 | + */ |
| 156 | + constructor(opts?: MongoAdapterOption); |
| 157 | + name: string; |
| 158 | + prefix: string; |
| 159 | + lockCollectionName: string; |
| 160 | + resetOnInit: boolean; |
| 161 | + db: Db; |
| 162 | + uniqueName: string; |
| 163 | + collection: any; |
| 164 | + lockCollection: any; |
| 165 | + /** |
| 166 | + * @async |
| 167 | + * @memberOf MongoAdapter |
| 168 | + * @name ping |
| 169 | + * @description Check connection to MongoDB |
| 170 | + * @returns {Promise<object>} |
| 171 | + */ |
| 172 | + ping(): Promise<unknown>; |
| 173 | + acquireLock(): Promise<boolean>; |
| 174 | + releaseLock(): Promise<void>; |
| 175 | + remove(uid: string): Promise<boolean>; |
| 176 | + add(uid: string, isInterval: boolean, delay: number): Promise<boolean>; |
| 177 | + update(task: unknown, nextExecuteAt: Date): Promise<boolean>; |
| 178 | + iterate(nextExecuteAt: Date): Promise<void>; |
| 179 | +} |
| 180 | + |
| 181 | +export interface RedisAdapterOption { |
| 182 | + client: RedisClient; |
| 183 | + lockCollectionName?: string; |
| 184 | + prefix?: string; |
| 185 | + resetOnInit?: boolean; |
| 186 | +} |
| 187 | + |
| 188 | +/** Class representing Redis adapter for JoSk */ |
| 189 | +export class RedisAdapter { |
| 190 | + /** |
| 191 | + * Create a RedisAdapter instance |
| 192 | + * @param {object} opts - configuration object |
| 193 | + * @param {RedisClient} opts.client - Required, Redis'es `RedisClient` instance, like one returned from `await redis.createClient().connect()` method |
| 194 | + * @param {string} [opts.lockCollectionName] - custom "lock" collection name |
| 195 | + * @param {string} [opts.prefix] - prefix for scope isolation; use when creating multiple JoSK instances within the single application |
| 196 | + * @param {boolean} [opts.resetOnInit] - Make sure all old tasks is completed before setting a new one, see readme for more details |
| 197 | + */ |
| 198 | + constructor(opts?: RedisAdapterOption); |
| 199 | + name: string; |
| 200 | + prefix: string; |
| 201 | + uniqueName: string; |
| 202 | + lockKey: string; |
| 203 | + resetOnInit: boolean; |
| 204 | + client: RedisClient; |
| 205 | + /** |
| 206 | + * @async |
| 207 | + * @memberOf RedisAdapter |
| 208 | + * @name ping |
| 209 | + * @description Check connection to Redis |
| 210 | + * @returns {Promise<object>} |
| 211 | + */ |
| 212 | + ping(): Promise<unknown>; |
| 213 | + acquireLock(): Promise<boolean>; |
| 214 | + releaseLock(): Promise<void>; |
| 215 | + remove(uid: string): Promise<boolean>; |
| 216 | + add(uid: string, isInterval: boolean, delay: number): Promise<boolean>; |
| 217 | + update(task: unknown, nextExecuteAt: Date): Promise<boolean>; |
| 218 | + iterate(nextExecuteAt: Date): Promise<void>; |
| 219 | + __getTaskKey(uid: string): string; |
| 220 | +} |
0 commit comments