-
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathtask.ts
More file actions
74 lines (61 loc) · 1.7 KB
/
task.ts
File metadata and controls
74 lines (61 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import Client from "./client";
import { AsyncResult } from "./result";
export interface TaskOptions {
/**
* @member {string}
*/
taskId: string;
/**
* @member {number}
* Number of seconds into the future that the task should execute.
* Defaults to immediate execution.
*/
countdown: number;
/**
* @member {Date}
* Absolute time and date of when the task should be executed.
* May not be specified if `countdown` is also supplied.
*/
eta: Date;
// /**
// * @member {Number | Date}
// * Datetime or seconds in the future for the task should expire.
// * The task won't be executed after the expiration time.
// */
// expires: number | Date;
// TODO:: retry, retryPolicy
}
export default class Task {
client: Client;
name: string;
/**
* Asynchronous Task
* @constructor Task
* @param {Client} clinet celery client instance
* @param {string} name celery task name
*/
constructor(client: Client, name: string) {
this.client = client;
this.name = name;
}
/**
* @method Task#delay
*
* @returns {AsyncResult} the result of client.publish
*
* @example
* client.createTask('task.add').delay(1, 2)
*/
public delay(...args: any[]): AsyncResult {
return this.applyAsync([...args]);
}
public applyAsync(args: Array<any>, kwargs?: object, taskOptions?: TaskOptions): AsyncResult {
if (args && !Array.isArray(args)) {
throw new Error("args is not array");
}
if (kwargs && typeof kwargs !== "object") {
throw new Error("kwargs is not object");
}
return this.client.sendTask(this.name, args || [], kwargs || {}, taskOptions?.taskId, taskOptions?.countdown, taskOptions?.eta);
}
}