-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCeleryTask.js
More file actions
147 lines (129 loc) · 3.23 KB
/
CeleryTask.js
File metadata and controls
147 lines (129 loc) · 3.23 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
function delay(time) {
return new Promise(function (fulfill) {
setTimeout(fulfill, time);
});
}
CeleryDelay = delay;
// holds task related info + stats
function celery_task(t) {
var task = {
task:t,
args:[],
utc: true
};
var timeout = 0;
var remove = true;
this.task = t;
this.remove_results = function(_r) {
if (_r === undefined) {
return remove;
} else {
remove = _r;
return this;
}
}
this.reset = function(str) {
if (str) {
delete task[str];
} else {
task = {task: t,args: []};
this.reset_stats();
}
}
//If true time uses the UTC timezone, if not the current local timezone should be used.
this.utc = function(utc) {
if (utc === undefined) {
return task.utc;
}
task.utc = utc;
return this;
}
// sets timeout value in milliseconds
// timeout is tracked locally. However it is better to use timelimit
// to track it in celery
this.timeout = function(t) {
if (t != undefined) {
timeout = t;
return this;
} else {
return timeout;
}
}
// Current number of times this task has been retried. Defaults to 0 if not specified.
this.retries = function(retries) {
if (retries === undefined) {
return task.retries;
}
task.retries = retries;
return this;
};
// estimated time of arrival
this.eta = function(eta) {
if (eta === undefined) {
return task.eta;
}
task.eta = eta;
return this;
};
// Dictionary of keyword arguments. Will be an empty dictionary if not provided.
this.kwargs = function(kwargs) {
if (kwargs === undefined) {
return task.kwargs;
}
task.kwargs = kwargs;
return this;
}
this.timelimit = function(timelimit) {
if (timelimit === undefined) {
return task.timelimit
}
task.timelimit = timelimit;
return this;
}
// Expiration date. If not provided the message will never expire.
// The message will be expired when the message is received and the
// expiration date has been exceeded.
this.expires = function(expires) {
if (expires === undefined) {
return task.expires;
}
task.expires = expires;
return this;
}
// Returns plain object with required properties ready to be serialized
this._body_ = function() {
task.args = arguments;
if (!(task.args instanceof Array)) {
task.args = [];
for (key in arguments) {
task.args.push(arguments[key]);
}
}
return task;
}
// returns how long it took to complete the last operation
this.execution_time = function() {
if (this._stats_.called_at != undefined && this._stats_.completed_at != undefined) {
return this._stats_.completed_at - this._stats_.called_at;
}
return undefined;
}
// totali execution time. Returns total time executed. It adds up
// if task was called more than once
this.total_execution_time = function() {
return this._stats_.total_execution_time;
}
// reset current stats
this.reset_stats = function() {
this._stats_ = {
called:0,
succeeded:0,
failed:0,
called_at: undefined,
completed_at: undefined,
total_execution_time: 0
};
}
this.reset_stats();
}
CeleryTask = celery_task;