workmatic 1.0.0
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 +508 -0
- package/dashboard/app.js +249 -0
- package/dashboard/index.html +143 -0
- package/dashboard/style.css +464 -0
- package/dist/cli.d.ts +15 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +505 -0
- package/dist/cli.js.map +1 -0
- package/dist/client.d.ts +28 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +99 -0
- package/dist/client.js.map +1 -0
- package/dist/dashboard.d.ts +48 -0
- package/dist/dashboard.d.ts.map +1 -0
- package/dist/dashboard.js +412 -0
- package/dist/dashboard.js.map +1 -0
- package/dist/database.d.ts +29 -0
- package/dist/database.d.ts.map +1 -0
- package/dist/database.js +99 -0
- package/dist/database.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +218 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +45 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +64 -0
- package/dist/utils.js.map +1 -0
- package/dist/worker.d.ts +26 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +331 -0
- package/dist/worker.js.map +1 -0
- package/package.json +55 -0
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import type { Kysely, Generated } from 'kysely';
|
|
2
|
+
/**
|
|
3
|
+
* Job status types
|
|
4
|
+
*/
|
|
5
|
+
export type JobStatus = 'ready' | 'running' | 'done' | 'failed' | 'dead';
|
|
6
|
+
/**
|
|
7
|
+
* Database table schema for workmatic_jobs
|
|
8
|
+
*/
|
|
9
|
+
export interface WorkmaticJobsTable {
|
|
10
|
+
id: Generated<number>;
|
|
11
|
+
public_id: string;
|
|
12
|
+
queue: string;
|
|
13
|
+
payload: string;
|
|
14
|
+
status: JobStatus;
|
|
15
|
+
priority: number;
|
|
16
|
+
run_at: number;
|
|
17
|
+
attempts: number;
|
|
18
|
+
max_attempts: number;
|
|
19
|
+
lease_until: number;
|
|
20
|
+
created_at: number;
|
|
21
|
+
updated_at: number;
|
|
22
|
+
last_error: string | null;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Database table schema for workmatic_settings
|
|
26
|
+
*/
|
|
27
|
+
export interface WorkmaticSettingsTable {
|
|
28
|
+
queue: string;
|
|
29
|
+
paused: number;
|
|
30
|
+
updated_at: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Kysely database schema
|
|
34
|
+
*/
|
|
35
|
+
export interface WorkmaticDatabase {
|
|
36
|
+
workmatic_jobs: WorkmaticJobsTable;
|
|
37
|
+
workmatic_settings: WorkmaticSettingsTable;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Kysely database instance type
|
|
41
|
+
*/
|
|
42
|
+
export type WorkmaticDb = Kysely<WorkmaticDatabase>;
|
|
43
|
+
/**
|
|
44
|
+
* Job representation returned to users
|
|
45
|
+
*/
|
|
46
|
+
export interface Job<TPayload = unknown> {
|
|
47
|
+
/** Unique public identifier (nanoid) */
|
|
48
|
+
id: string;
|
|
49
|
+
/** Queue name */
|
|
50
|
+
queue: string;
|
|
51
|
+
/** Job payload */
|
|
52
|
+
payload: TPayload;
|
|
53
|
+
/** Current status */
|
|
54
|
+
status: JobStatus;
|
|
55
|
+
/** Priority (lower = higher priority) */
|
|
56
|
+
priority: number;
|
|
57
|
+
/** Number of attempts made */
|
|
58
|
+
attempts: number;
|
|
59
|
+
/** Maximum allowed attempts */
|
|
60
|
+
maxAttempts: number;
|
|
61
|
+
/** When the job was created (unix ms) */
|
|
62
|
+
createdAt: number;
|
|
63
|
+
/** Last error message if failed */
|
|
64
|
+
lastError: string | null;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Options for adding a job
|
|
68
|
+
*/
|
|
69
|
+
export interface AddJobOptions {
|
|
70
|
+
/** Job priority (lower = higher priority). Default: 0 */
|
|
71
|
+
priority?: number;
|
|
72
|
+
/** Delay before job becomes available (ms). Default: 0 */
|
|
73
|
+
delayMs?: number;
|
|
74
|
+
/** Maximum retry attempts. Default: 3 */
|
|
75
|
+
maxAttempts?: number;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Result of adding a job
|
|
79
|
+
*/
|
|
80
|
+
export interface AddJobResult {
|
|
81
|
+
ok: true;
|
|
82
|
+
id: string;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Options for creating a database
|
|
86
|
+
*/
|
|
87
|
+
export interface DatabaseOptions {
|
|
88
|
+
/** Existing better-sqlite3 Database instance */
|
|
89
|
+
db?: import('better-sqlite3').Database;
|
|
90
|
+
/** Path to SQLite database file (ignored if db is provided) */
|
|
91
|
+
filename?: string;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Options for creating a client
|
|
95
|
+
*/
|
|
96
|
+
export interface ClientOptions {
|
|
97
|
+
/** Kysely database instance */
|
|
98
|
+
db: WorkmaticDb;
|
|
99
|
+
/** Queue name. Default: 'default' */
|
|
100
|
+
queue?: string;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Backoff function type
|
|
104
|
+
*/
|
|
105
|
+
export type BackoffFunction = (attempts: number) => number;
|
|
106
|
+
/**
|
|
107
|
+
* Options for creating a worker
|
|
108
|
+
*/
|
|
109
|
+
export interface WorkerOptions {
|
|
110
|
+
/** Kysely database instance */
|
|
111
|
+
db: WorkmaticDb;
|
|
112
|
+
/** Queue name. Default: 'default' */
|
|
113
|
+
queue?: string;
|
|
114
|
+
/** Number of concurrent job processors. Default: 1 */
|
|
115
|
+
concurrency?: number;
|
|
116
|
+
/** Lease duration in ms. Default: 30000 */
|
|
117
|
+
leaseMs?: number;
|
|
118
|
+
/** Poll interval in ms when no jobs available. Default: 1000 */
|
|
119
|
+
pollMs?: number;
|
|
120
|
+
/** Job execution timeout in ms. Default: undefined (no timeout) */
|
|
121
|
+
timeoutMs?: number;
|
|
122
|
+
/** Backoff function for retries. Default: exponential */
|
|
123
|
+
backoff?: BackoffFunction;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Job processor function type
|
|
127
|
+
*/
|
|
128
|
+
export type JobProcessor<TPayload = unknown> = (job: Job<TPayload>) => Promise<void>;
|
|
129
|
+
/**
|
|
130
|
+
* Stats by status
|
|
131
|
+
*/
|
|
132
|
+
export interface JobStats {
|
|
133
|
+
ready: number;
|
|
134
|
+
running: number;
|
|
135
|
+
done: number;
|
|
136
|
+
failed: number;
|
|
137
|
+
dead: number;
|
|
138
|
+
total: number;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Client interface
|
|
142
|
+
*/
|
|
143
|
+
export interface WorkmaticClient {
|
|
144
|
+
/** Add a job to the queue */
|
|
145
|
+
add<TPayload = unknown>(payload: TPayload, options?: AddJobOptions): Promise<AddJobResult>;
|
|
146
|
+
/** Get job statistics */
|
|
147
|
+
stats(): Promise<JobStats>;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Worker interface
|
|
151
|
+
*/
|
|
152
|
+
export interface WorkmaticWorker {
|
|
153
|
+
/** Set the job processor function */
|
|
154
|
+
process<TPayload = unknown>(fn: JobProcessor<TPayload>): void;
|
|
155
|
+
/** Start processing jobs */
|
|
156
|
+
start(): void;
|
|
157
|
+
/** Stop processing jobs (drains current jobs) */
|
|
158
|
+
stop(): Promise<void>;
|
|
159
|
+
/** Pause processing (stops claiming new jobs) */
|
|
160
|
+
pause(): void;
|
|
161
|
+
/** Resume processing */
|
|
162
|
+
resume(): void;
|
|
163
|
+
/** Get job statistics */
|
|
164
|
+
stats(): Promise<JobStats>;
|
|
165
|
+
/** Check if worker is running */
|
|
166
|
+
readonly isRunning: boolean;
|
|
167
|
+
/** Check if worker is paused */
|
|
168
|
+
readonly isPaused: boolean;
|
|
169
|
+
/** Queue name */
|
|
170
|
+
readonly queue: string;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Options for creating a dashboard
|
|
174
|
+
*/
|
|
175
|
+
export interface DashboardOptions {
|
|
176
|
+
/** Kysely database instance */
|
|
177
|
+
db: WorkmaticDb;
|
|
178
|
+
/** HTTP server port. Default: 3000 */
|
|
179
|
+
port?: number;
|
|
180
|
+
/** Worker instances to control */
|
|
181
|
+
workers?: WorkmaticWorker[];
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Options for creating dashboard middleware
|
|
185
|
+
*/
|
|
186
|
+
export interface DashboardMiddlewareOptions {
|
|
187
|
+
/** Kysely database instance */
|
|
188
|
+
db: WorkmaticDb;
|
|
189
|
+
/** Worker instances to control */
|
|
190
|
+
workers?: WorkmaticWorker[];
|
|
191
|
+
/** Base path for mounting (e.g., '/workmatic'). Default: '' */
|
|
192
|
+
basePath?: string;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Express-compatible request handler
|
|
196
|
+
*/
|
|
197
|
+
export type DashboardMiddleware = (req: import('http').IncomingMessage, res: import('http').ServerResponse, next?: () => void) => void;
|
|
198
|
+
/**
|
|
199
|
+
* Dashboard interface
|
|
200
|
+
*/
|
|
201
|
+
export interface WorkmaticDashboard {
|
|
202
|
+
/** Close the dashboard server */
|
|
203
|
+
close(): Promise<void>;
|
|
204
|
+
/** Server port */
|
|
205
|
+
readonly port: number;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Internal job representation from database
|
|
209
|
+
*/
|
|
210
|
+
export interface ClaimedJob {
|
|
211
|
+
id: number;
|
|
212
|
+
public_id: string;
|
|
213
|
+
queue: string;
|
|
214
|
+
payload: string;
|
|
215
|
+
attempts: number;
|
|
216
|
+
max_attempts: number;
|
|
217
|
+
}
|
|
218
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|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,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAC3B,iBAAiB;IACjB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,+BAA+B;IAC/B,EAAE,EAAE,WAAW,CAAC;IAChB,sCAAsC;IACtC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,kCAAkC;IAClC,OAAO,CAAC,EAAE,eAAe,EAAE,CAAC;CAC7B;AAED;;GAEG;AACH,MAAM,WAAW,0BAA0B;IACzC,+BAA+B;IAC/B,EAAE,EAAE,WAAW,CAAC;IAChB,kCAAkC;IAClC,OAAO,CAAC,EAAE,eAAe,EAAE,CAAC;IAC5B,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,CAChC,GAAG,EAAE,OAAO,MAAM,EAAE,eAAe,EACnC,GAAG,EAAE,OAAO,MAAM,EAAE,cAAc,EAClC,IAAI,CAAC,EAAE,MAAM,IAAI,KACd,IAAI,CAAC;AAEV;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,iCAAiC;IACjC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,kBAAkB;IAClB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;CACtB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { BackoffFunction } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Default exponential backoff function
|
|
4
|
+
* Returns delay in milliseconds: 1000 * 2^attempts
|
|
5
|
+
*
|
|
6
|
+
* @param attempts - Number of failed attempts
|
|
7
|
+
* @returns Delay in milliseconds
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* defaultBackoff(0) // 1000ms (1 second)
|
|
12
|
+
* defaultBackoff(1) // 2000ms (2 seconds)
|
|
13
|
+
* defaultBackoff(2) // 4000ms (4 seconds)
|
|
14
|
+
* defaultBackoff(3) // 8000ms (8 seconds)
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare const defaultBackoff: BackoffFunction;
|
|
18
|
+
/**
|
|
19
|
+
* Validate that a payload is JSON-serializable
|
|
20
|
+
*
|
|
21
|
+
* @param payload - The payload to validate
|
|
22
|
+
* @throws Error if payload cannot be serialized to JSON
|
|
23
|
+
* @returns The JSON string representation
|
|
24
|
+
*/
|
|
25
|
+
export declare function validatePayload(payload: unknown): string;
|
|
26
|
+
/**
|
|
27
|
+
* Parse a JSON payload safely
|
|
28
|
+
*
|
|
29
|
+
* @param json - JSON string to parse
|
|
30
|
+
* @returns Parsed payload
|
|
31
|
+
* @throws Error if JSON is invalid
|
|
32
|
+
*/
|
|
33
|
+
export declare function parsePayload<T = unknown>(json: string): T;
|
|
34
|
+
/**
|
|
35
|
+
* Sleep for a specified duration
|
|
36
|
+
*
|
|
37
|
+
* @param ms - Duration in milliseconds
|
|
38
|
+
* @returns Promise that resolves after the duration
|
|
39
|
+
*/
|
|
40
|
+
export declare function sleep(ms: number): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Get current timestamp in milliseconds
|
|
43
|
+
*/
|
|
44
|
+
export declare function now(): number;
|
|
45
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAElD;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,cAAc,EAAE,eAE5B,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAQxD;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC,CAQzD;AAED;;;;;GAKG;AACH,wBAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE/C;AAED;;GAEG;AACH,wBAAgB,GAAG,IAAI,MAAM,CAE5B"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default exponential backoff function
|
|
3
|
+
* Returns delay in milliseconds: 1000 * 2^attempts
|
|
4
|
+
*
|
|
5
|
+
* @param attempts - Number of failed attempts
|
|
6
|
+
* @returns Delay in milliseconds
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* defaultBackoff(0) // 1000ms (1 second)
|
|
11
|
+
* defaultBackoff(1) // 2000ms (2 seconds)
|
|
12
|
+
* defaultBackoff(2) // 4000ms (4 seconds)
|
|
13
|
+
* defaultBackoff(3) // 8000ms (8 seconds)
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export const defaultBackoff = (attempts) => {
|
|
17
|
+
return 1000 * Math.pow(2, attempts);
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Validate that a payload is JSON-serializable
|
|
21
|
+
*
|
|
22
|
+
* @param payload - The payload to validate
|
|
23
|
+
* @throws Error if payload cannot be serialized to JSON
|
|
24
|
+
* @returns The JSON string representation
|
|
25
|
+
*/
|
|
26
|
+
export function validatePayload(payload) {
|
|
27
|
+
try {
|
|
28
|
+
return JSON.stringify(payload);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
throw new Error(`Payload is not JSON-serializable: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Parse a JSON payload safely
|
|
36
|
+
*
|
|
37
|
+
* @param json - JSON string to parse
|
|
38
|
+
* @returns Parsed payload
|
|
39
|
+
* @throws Error if JSON is invalid
|
|
40
|
+
*/
|
|
41
|
+
export function parsePayload(json) {
|
|
42
|
+
try {
|
|
43
|
+
return JSON.parse(json);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
throw new Error(`Invalid JSON payload: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Sleep for a specified duration
|
|
51
|
+
*
|
|
52
|
+
* @param ms - Duration in milliseconds
|
|
53
|
+
* @returns Promise that resolves after the duration
|
|
54
|
+
*/
|
|
55
|
+
export function sleep(ms) {
|
|
56
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Get current timestamp in milliseconds
|
|
60
|
+
*/
|
|
61
|
+
export function now() {
|
|
62
|
+
return Date.now();
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,MAAM,cAAc,GAAoB,CAAC,QAAgB,EAAU,EAAE;IAC1E,OAAO,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;AACtC,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAAC,OAAgB;IAC9C,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CACb,qCAAqC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe,EAAE,CAChG,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAc,IAAY;IACpD,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAM,CAAC;IAC/B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CACb,yBAAyB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe,EAAE,CACpF,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,KAAK,CAAC,EAAU;IAC9B,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AACzD,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,GAAG;IACjB,OAAO,IAAI,CAAC,GAAG,EAAE,CAAC;AACpB,CAAC"}
|
package/dist/worker.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { WorkerOptions, WorkmaticWorker } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Create a job queue worker for processing jobs
|
|
4
|
+
*
|
|
5
|
+
* @param options - Worker options
|
|
6
|
+
* @returns Worker instance
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const worker = createWorker({ db, concurrency: 4 });
|
|
11
|
+
*
|
|
12
|
+
* // Set the processor function
|
|
13
|
+
* worker.process(async (job) => {
|
|
14
|
+
* console.log('Processing job:', job.id);
|
|
15
|
+
* await sendEmail(job.payload.email);
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* // Start processing
|
|
19
|
+
* worker.start();
|
|
20
|
+
*
|
|
21
|
+
* // Later, gracefully stop
|
|
22
|
+
* await worker.stop();
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function createWorker(options: WorkerOptions): WorkmaticWorker;
|
|
26
|
+
//# sourceMappingURL=worker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../src/worker.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAEV,aAAa,EACb,eAAe,EAMhB,MAAM,YAAY,CAAC;AAGpB;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,aAAa,GAAG,eAAe,CAgXpE"}
|