-
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathclient.ts
More file actions
148 lines (134 loc) · 3.41 KB
/
client.ts
File metadata and controls
148 lines (134 loc) · 3.41 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { v4 } from "uuid";
import Base from "./base";
import Task from "./task";
import { AsyncResult } from "./result";
export class TaskMessage {
constructor(
readonly headers: object,
readonly properties: object,
readonly body: [Array<any>, object, object] | object,
readonly sentEvent: object
) {}
}
export default class Client extends Base {
private taskProtocols = {
1: this.asTaskV1,
2: this.asTaskV2
};
get createTaskMessage(): (...args: any[]) => TaskMessage {
return this.taskProtocols[this.conf.TASK_PROTOCOL];
}
public sendTaskMessage(taskName: string, message: TaskMessage): void {
const { headers, properties, body /*, sentEvent */ } = message;
const exchange = "";
// exchangeType = 'direct';
// const serializer = 'json';
this.isReady().then(() =>
this.broker.publish(
body,
exchange,
this.conf.CELERY_QUEUE,
headers,
properties
)
);
}
public asTaskV2(
taskId: string,
taskName: string,
args?: Array<any>,
kwargs?: object
): TaskMessage {
const message: TaskMessage = {
headers: {
lang: "js",
task: taskName,
id: taskId
/*
'shadow': shadow,
'eta': eta,
'expires': expires,
'group': group_id,
'retries': retries,
'timelimit': [time_limit, soft_time_limit],
'root_id': root_id,
'parent_id': parent_id,
'argsrepr': argsrepr,
'kwargsrepr': kwargsrepr,
'origin': origin or anon_nodename()
*/
},
properties: {
correlationId: taskId,
replyTo: ""
},
body: [args, kwargs, {}],
sentEvent: null
};
return message;
}
/**
* create json string representing celery task message. used by Client.publish
*
* celery protocol reference: https://docs.celeryproject.org/en/latest/internals/protocol.html
* celery code: https://github.com/celery/celery/blob/4aefccf8a89bffe9dac9a72f2601db1fa8474f5d/celery/app/amqp.py#L307-L464
*
* @function createTaskMessage
*
* @returns {String} JSON serialized string of celery task message
*/
public asTaskV1(
taskId: string,
taskName: string,
args?: Array<any>,
kwargs?: object
): TaskMessage {
const message: TaskMessage = {
headers: {},
properties: {
correlationId: taskId,
replyTo: ""
},
body: {
task: taskName,
id: taskId,
args: args,
kwargs: kwargs
},
sentEvent: null
};
return message;
}
/**
* createTask
* @method Client#createTask
* @param {string} name for task name
* @returns {Task} task object
*
* @example
* client.createTask('task.add').delay([1, 2])
*/
public createTask(name: string): Task {
return new Task(this, name);
}
/**
* get AsyncResult by task id
* @param {string} taskId for task identification.
* @returns {AsyncResult}
*/
public asyncResult(taskId: string): AsyncResult {
return new AsyncResult(taskId, this.backend);
}
public sendTask(
taskName: string,
args?: Array<any>,
kwargs?: object,
taskId?: string
): AsyncResult {
taskId = taskId || v4();
const message = this.createTaskMessage(taskId, taskName, args, kwargs);
this.sendTaskMessage(taskName, message);
const result = new AsyncResult(taskId, this.backend);
return result;
}
}