@stacksjs/queue 0.68.1 → 0.69.2

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/job.d.ts ADDED
@@ -0,0 +1,199 @@
1
+ import type { QueueOption } from '@stacksjs/types';
2
+
3
+ declare const queueDriver: 'database';
4
+ declare interface JobConfig {
5
+ handle?: (payload?: any) => Promise<void>
6
+ action?: string | (() => Promise<void>)
7
+ }
8
+ declare interface Dispatchable {
9
+ dispatch: () => Promise<void>
10
+ dispatchNow: () => Promise<void>
11
+ delay: (seconds: number) => this
12
+ afterResponse: () => this
13
+ chain: (jobs: Dispatchable[]) => this
14
+ onQueue: (queue: string) => this
15
+ }
16
+ export declare function runJob(name: string, options: QueueOption): Promise<void>;
17
+ export declare class Queue implements Dispatchable {
18
+ protected options: QueueOption = {}
19
+
20
+ constructor(
21
+ protected name: string,
22
+ protected payload?: any,
23
+ ) { }
24
+
25
+ async dispatch(): Promise<void> {
26
+ const queueName = this.options.queue || 'default'
27
+
28
+ const jobPayload = this.createJobPayload(queueName)
29
+
30
+ if (this.isQueuedDriver()) {
31
+ await this.storeQueuedJob(jobPayload)
32
+ return
33
+ }
34
+
35
+ if (this.options.afterResponse) {
36
+ this.deferAfterResponse()
37
+ return
38
+ }
39
+
40
+ if (this.options.delay) {
41
+ this.deferWithDelay()
42
+ return
43
+ }
44
+
45
+ await this.runJobImmediately(jobPayload)
46
+ }
47
+
48
+ private isQueuedDriver(): boolean {
49
+ return ['database', 'redis'].includes(queueDriver)
50
+ }
51
+
52
+ private createJobPayload(queueName: string): QueueOption {
53
+ return {
54
+ queue: queueName,
55
+ payload: this.payload,
56
+ context: this.options.context,
57
+ maxTries: this.options.maxTries,
58
+ timeout: this.options.timeout,
59
+ backoff: this.options.backoff,
60
+ delay: this.options.delay,
61
+ }
62
+ }
63
+
64
+ private async storeQueuedJob(jobPayload: any): Promise<void> {
65
+ await storeJob(this.name, jobPayload)
66
+ }
67
+
68
+ private deferAfterResponse(): void {
69
+ process.on('beforeExit', async () => {
70
+ await this.dispatchNow()
71
+ })
72
+ }
73
+
74
+ private deferWithDelay(): void {
75
+ setTimeout(async () => {
76
+ await this.dispatchNow()
77
+ }, this.options.delay || 0)
78
+ }
79
+
80
+ private async runJobImmediately(jobPayload: any): Promise<void> {
81
+ try {
82
+ await runJob(this.name, jobPayload)
83
+ await this.runChainedJobs()
84
+ }
85
+ catch (error) {
86
+ log.error(`Failed to dispatch job ${this.name}:`, error)
87
+ throw error
88
+ }
89
+ }
90
+
91
+ private async runChainedJobs(): Promise<void> {
92
+ if (!this.options.chainedJobs?.length)
93
+ return
94
+
95
+ for (const job of this.options.chainedJobs) {
96
+ await job.dispatch()
97
+ }
98
+ }
99
+
100
+ async dispatchNow(): Promise<void> {
101
+ try {
102
+ await runJob(this.name, {
103
+ payload: this.payload,
104
+ context: this.options.context,
105
+ immediate: true,
106
+ })
107
+
108
+ if (this.options.chainedJobs?.length) {
109
+ for (const job of this.options.chainedJobs) {
110
+ await job.dispatchNow()
111
+ }
112
+ }
113
+ }
114
+ catch (error) {
115
+ log.error(`Failed to execute job ${this.name}:`, error)
116
+ throw error
117
+ }
118
+ }
119
+
120
+ delay(seconds: number): this {
121
+ this.options.delay = seconds
122
+ return this
123
+ }
124
+
125
+ onQueue(queue: string): this {
126
+ this.options.queue = queue
127
+ return this
128
+ }
129
+
130
+ chain(jobs: Dispatchable[]): this {
131
+ this.options.chainedJobs = jobs
132
+ return this
133
+ }
134
+
135
+ afterResponse(): this {
136
+ this.options.afterResponse = true
137
+ return this
138
+ }
139
+
140
+ tries(count: number): this {
141
+ this.options.maxTries = count
142
+ return this
143
+ }
144
+
145
+ timeout(seconds: number): this {
146
+ this.options.timeout = seconds
147
+ return this
148
+ }
149
+
150
+ backoff(attempts: number[]): this {
151
+ this.options.backoff = attempts
152
+ return this
153
+ }
154
+
155
+ withContext(context: any): this {
156
+ this.options.context = context
157
+ return this
158
+ }
159
+ }
160
+ export declare class JobFactory {
161
+ static make(name: string, payload?: any): Queue {
162
+ return new Queue(name, payload)
163
+ }
164
+
165
+ static dispatch(name: string, payload?: any): Queue {
166
+ const job = new Queue(name, payload)
167
+ job.dispatch()
168
+ return job
169
+ }
170
+
171
+ static async dispatchNow(name: string, payload?: any): Promise<void> {
172
+ await new Queue(name, payload).dispatchNow()
173
+ }
174
+
175
+ static later(delay: number, name: string, payload?: any): Queue {
176
+ return new Queue(name, payload).delay(delay)
177
+ }
178
+
179
+ static chain(jobs: Queue[]): Queue {
180
+ const firstJob = jobs[0]
181
+ return firstJob.chain(jobs.slice(1))
182
+ }
183
+ }
184
+ export declare function job(name: string, payload?: any): Queue;
185
+ export declare function processOrder(payload, context): void;
186
+
187
+ export default {
188
+ async handle() {
189
+ }
190
+ };
191
+ export default {
192
+ action: 'SendWelcomeEmail'
193
+ };
194
+ export default {
195
+ action: async () => {
196
+ }
197
+ };
198
+ export default async function(payload, context) {
199
+ };
@@ -0,0 +1,24 @@
1
+ import type { JitterConfig, JobOptions } from '@stacksjs/types';
2
+ import type { JobModel } from '../../../orm/src/models/Job';
3
+
4
+ declare interface QueuePayload {
5
+ path: string
6
+ name: string
7
+ maxTries: number
8
+ timeOut: number | null
9
+ timeOutAt: Date | null
10
+ params: any
11
+ classPayload: string
12
+ }
13
+ export declare function processJobs(queue: string | undefined): Promise<Ok<string, never>>;
14
+ export declare function executeFailedJobs(): Promise<void>;
15
+ export declare function retryFailedJob(id: number): Promise<void>;
16
+ declare function executeJobs(queue: string | undefined): Promise<void>;
17
+ declare function enforceMaxDelay(maxDelay: number | undefined, delay: number): number;
18
+ declare function addDelay(timestamp: number | undefined, currentAttempts: number, classPayload: JobOptions): number;
19
+ declare function meilisecondsToSeconds(meiliseconds: number): number;
20
+ declare function applyJitter(delay: number, jitterConfig: JitterConfig): number;
21
+ declare function storeFailedJob(job: JobModel, exception: string): void;
22
+ declare function now(): string;
23
+ declare function updateJobAttempts(job: JobModel, currentAttempts: number, delay: number | null): Promise<void>;
24
+ declare function timestampNow(): number;
@@ -0,0 +1,4 @@
1
+ import type { QueueOption } from '@stacksjs/types';
2
+
3
+ export declare function storeJob(name: string, options: QueueOption): Promise<void>;
4
+ declare function generateUnixTimestamp(secondsToAdd: number): number;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/queue",
3
3
  "type": "module",
4
- "version": "0.68.1",
4
+ "version": "0.69.2",
5
5
  "description": "The Stacks Queue system.",
6
6
  "author": "Chris Breuer",
7
7
  "contributors": ["Chris Breuer <chris@stacksjs.org>"],
@@ -42,10 +42,7 @@
42
42
  "typecheck": "bun tsc --noEmit",
43
43
  "prepublishOnly": "bun run build"
44
44
  },
45
- "dependencies": {
46
- "@poppinss/defer": "^1.1.0"
47
- },
48
45
  "devDependencies": {
49
- "@stacksjs/development": "0.67.0"
46
+ "@stacksjs/development": "0.69.2"
50
47
  }
51
48
  }