Skip to content

Commit 8e3ae6a

Browse files
authored
Merge pull request #7 from StatelessStudio/r2.0.0
v2.0.0
2 parents 07b6172 + 05e9ddf commit 8e3ae6a

3 files changed

Lines changed: 1180 additions & 4075 deletions

File tree

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,66 @@ Specify an error-handler function. Default logs to stderr
157157
### fatal
158158

159159
Specify a fatal error-handler function. Default logs to stderr and exits
160+
161+
### maxAttempts
162+
163+
Maximum number of attempts for a task before it's considered failed. Default is 1. If a task fails, it will be retried up to this many times.
164+
165+
```typescript
166+
export const addQueue = new Queue<AddTask, number>({
167+
name: 'add-queue',
168+
callback: async (task: AddTask) => task.a + task.b,
169+
maxAttempts: 3,
170+
});
171+
```
172+
173+
## Task Options
174+
175+
When pushing tasks to the queue, you can specify additional options:
176+
177+
### schedule
178+
179+
Schedule a task to run at a specific time in the future, with optional expiration. The task will not run until the scheduled time, and will be discarded if it expires.
180+
181+
`src/add-queue.ts`
182+
```typescript
183+
export interface AddTask {
184+
a: number;
185+
b: number;
186+
}
187+
188+
export const addQueue = new Queue<AddTask, number>({
189+
name: 'add-queue',
190+
callback: async (task: AddTask) => task.a + task.b,
191+
});
192+
```
193+
194+
`src/index.ts`
195+
```typescript
196+
import { AddTask, addQueue } from './add-queue';
197+
198+
if (addQueue.isMainThread()) {
199+
// Schedule a task to run in 5 minutes
200+
const sum = await addQueue.await({
201+
data: { a: 4, b: 8 },
202+
schedule: {
203+
scheduledAt: new Date(Date.now() + 5 * 60 * 1000),
204+
}
205+
});
206+
207+
console.log('Sum is', sum);
208+
// Sum is 12
209+
210+
// Schedule a task to run in 5 minutes, but expire after 10 minutes
211+
const sum2 = await addQueue.await({
212+
data: { a: 10, b: 20 },
213+
schedule: {
214+
scheduledAt: new Date(Date.now() + 5 * 60 * 1000),
215+
expiresAt: new Date(Date.now() + 10 * 60 * 1000),
216+
}
217+
});
218+
219+
console.log('Sum2 is', sum2);
220+
// Sum2 is 30
221+
}
222+
```

0 commit comments

Comments
 (0)