9
9
from celery import __version__ as CELERY_VERSION
10
10
from celery .schedules import crontab
11
11
12
- if CELERY_VERSION >= ' 4.0.0' :
12
+ if CELERY_VERSION >= " 4.0.0" :
13
13
from celery .schedules import solar
14
14
15
15
16
16
STOPWATCH = {}
17
- BEAT_DELIMITER = ' -> '
17
+ BEAT_DELIMITER = " -> "
18
18
19
19
20
20
def add_task_to_stopwatch (task_id ):
@@ -24,25 +24,25 @@ def add_task_to_stopwatch(task_id):
24
24
25
25
def get_task_prerun_attachment (task_id , task , args , kwargs , ** cbkwargs ):
26
26
"""Create the slack message attachment for a task prerun."""
27
- message = ' Executing -- ' + task .name .rsplit ('.' , 1 )[- 1 ]
27
+ message = " Executing -- " + task .name .rsplit ("." , 1 )[- 1 ]
28
28
29
- lines = [' Name: *' + task .name + '*' ]
29
+ lines = [" Name: *" + task .name + "*" ]
30
30
31
31
if cbkwargs ["show_task_id" ]:
32
- lines .append (' Task ID: ' + task_id )
32
+ lines .append (" Task ID: " + task_id )
33
33
34
34
if cbkwargs ["use_fixed_width" ]:
35
35
if cbkwargs ["show_task_args" ]:
36
- lines .append (' args: ' + '`' + str (args ) + '`' )
36
+ lines .append (" args: " + "`" + str (args ) + "`" )
37
37
if cbkwargs ["show_task_kwargs" ]:
38
- lines .append (' kwargs: ' + '`' + str (kwargs ) + '`' )
38
+ lines .append (" kwargs: " + "`" + str (kwargs ) + "`" )
39
39
else :
40
40
if cbkwargs ["show_task_args" ]:
41
- lines .append (' args: ' + str (args ))
41
+ lines .append (" args: " + str (args ))
42
42
if cbkwargs ["show_task_kwargs" ]:
43
- lines .append (' kwargs: ' + str (kwargs ))
43
+ lines .append (" kwargs: " + str (kwargs ))
44
44
45
- executing = ' \n ' .join (lines )
45
+ executing = " \n " .join (lines )
46
46
47
47
attachment = {
48
48
"attachments" : [
@@ -54,13 +54,13 @@ def get_task_prerun_attachment(task_id, task, args, kwargs, **cbkwargs):
54
54
"mrkdwn_in" : ["text" ]
55
55
}
56
56
],
57
- "text" : ''
57
+ "text" : ""
58
58
}
59
59
60
60
if cbkwargs ["flower_base_url" ]:
61
61
attachment ["attachments" ][0 ]["title_link" ] = (
62
62
cbkwargs ["flower_base_url" ] +
63
- ' /task/{tid}' .format (tid = task_id )
63
+ " /task/{tid}" .format (tid = task_id )
64
64
)
65
65
66
66
return attachment
@@ -85,36 +85,36 @@ def get_task_success_attachment(task_name, retval, task_id,
85
85
else :
86
86
retval = str (retval )
87
87
88
- message = ' SUCCESS -- ' + task_name .rsplit ('.' , 1 )[- 1 ]
88
+ message = " SUCCESS -- " + task_name .rsplit ("." , 1 )[- 1 ]
89
89
90
90
elapsed = divmod (time .time () - STOPWATCH .pop (task_id ), 60 )
91
91
92
- lines = [' Name: *' + task_name + '*' ]
92
+ lines = [" Name: *" + task_name + "*" ]
93
93
94
94
if cbkwargs ["show_task_execution_time" ]:
95
- lines .append (' Execution time: {m} minutes {s} seconds' .format (
95
+ lines .append (" Execution time: {m} minutes {s} seconds" .format (
96
96
m = str (int (elapsed [0 ])),
97
97
s = str (int (elapsed [1 ])),
98
98
))
99
99
if cbkwargs ["show_task_id" ]:
100
- lines .append (' Task ID: ' + task_id )
100
+ lines .append (" Task ID: " + task_id )
101
101
102
102
if cbkwargs ["use_fixed_width" ]:
103
103
if cbkwargs ["show_task_args" ]:
104
- lines .append (' args: ' + '`' + str (args ) + '`' )
104
+ lines .append (" args: " + "`" + str (args ) + "`" )
105
105
if cbkwargs ["show_task_kwargs" ]:
106
- lines .append (' kwargs: ' + '`' + str (kwargs ) + '`' )
106
+ lines .append (" kwargs: " + "`" + str (kwargs ) + "`" )
107
107
if cbkwargs ["show_task_return_value" ]:
108
- lines .append (' Returned: ' + ' ```' + str (retval ) + ' ```' )
108
+ lines .append (" Returned: " + " ```" + str (retval ) + " ```" )
109
109
else :
110
110
if cbkwargs ["show_task_args" ]:
111
- lines .append (' args: ' + str (args ))
111
+ lines .append (" args: " + str (args ))
112
112
if cbkwargs ["show_task_kwargs" ]:
113
- lines .append (' kwargs: ' + str (kwargs ))
113
+ lines .append (" kwargs: " + str (kwargs ))
114
114
if cbkwargs ["show_task_return_value" ]:
115
- lines .append (' Returned: ' + str (retval ))
115
+ lines .append (" Returned: " + str (retval ))
116
116
117
- success = ' \n ' .join (lines )
117
+ success = " \n " .join (lines )
118
118
119
119
attachment = {
120
120
"attachments" : [
@@ -126,13 +126,13 @@ def get_task_success_attachment(task_name, retval, task_id,
126
126
"mrkdwn_in" : ["text" ]
127
127
}
128
128
],
129
- "text" : ''
129
+ "text" : ""
130
130
}
131
131
132
132
if cbkwargs ["flower_base_url" ]:
133
133
attachment ["attachments" ][0 ]["title_link" ] = (
134
134
cbkwargs ["flower_base_url" ] +
135
- ' /task/{tid}' .format (tid = task_id )
135
+ " /task/{tid}" .format (tid = task_id )
136
136
)
137
137
138
138
return attachment
@@ -152,38 +152,38 @@ def get_task_failure_attachment(task_name, exc, task_id, args,
152
152
STOPWATCH .pop (task_id )
153
153
return
154
154
155
- message = ' FAILURE -- ' + task_name .rsplit ('.' , 1 )[- 1 ]
155
+ message = " FAILURE -- " + task_name .rsplit ("." , 1 )[- 1 ]
156
156
157
157
elapsed = divmod (time .time () - STOPWATCH .pop (task_id ), 60 )
158
158
159
- lines = [' Name: *' + task_name + '*' ]
159
+ lines = [" Name: *" + task_name + "*" ]
160
160
161
161
if cbkwargs ["show_task_execution_time" ]:
162
- lines .append (' Execution time: {m} minutes {s} seconds' .format (
162
+ lines .append (" Execution time: {m} minutes {s} seconds" .format (
163
163
m = str (int (elapsed [0 ])),
164
164
s = str (int (elapsed [1 ])),
165
165
))
166
166
if cbkwargs ["show_task_id" ]:
167
- lines .append (' Task ID: ' + task_id )
167
+ lines .append (" Task ID: " + task_id )
168
168
169
169
if cbkwargs ["use_fixed_width" ]:
170
170
if cbkwargs ["show_task_args" ]:
171
- lines .append (' args: ' + '`' + str (args ) + '`' )
171
+ lines .append (" args: " + "`" + str (args ) + "`" )
172
172
if cbkwargs ["show_task_kwargs" ]:
173
- lines .append (' kwargs: ' + '`' + str (kwargs ) + '`' )
174
- lines .append (' Exception: ' + '`' + str (exc ) + '`' )
173
+ lines .append (" kwargs: " + "`" + str (kwargs ) + "`" )
174
+ lines .append (" Exception: " + "`" + str (exc ) + "`" )
175
175
if cbkwargs ["show_task_exception_info" ]:
176
- lines .append (' Info: ' + ' ```' + str (einfo ) + ' ```' )
176
+ lines .append (" Info: " + " ```" + str (einfo ) + " ```" )
177
177
else :
178
178
if cbkwargs ["show_task_args" ]:
179
- lines .append (' args: ' + str (args ))
179
+ lines .append (" args: " + str (args ))
180
180
if cbkwargs ["show_task_kwargs" ]:
181
- lines .append (' kwargs: ' + str (kwargs ))
182
- lines .append (' Exception: ' + str (exc ))
181
+ lines .append (" kwargs: " + str (kwargs ))
182
+ lines .append (" Exception: " + str (exc ))
183
183
if cbkwargs ["show_task_exception_info" ]:
184
- lines .append (' Info: ' + str (einfo ))
184
+ lines .append (" Info: " + str (einfo ))
185
185
186
- failure = ' \n ' .join (lines )
186
+ failure = " \n " .join (lines )
187
187
188
188
attachment = {
189
189
"attachments" : [
@@ -195,13 +195,13 @@ def get_task_failure_attachment(task_name, exc, task_id, args,
195
195
"mrkdwn_in" : ["text" ]
196
196
}
197
197
],
198
- "text" : ''
198
+ "text" : ""
199
199
}
200
200
201
201
if cbkwargs ["flower_base_url" ]:
202
202
attachment ["attachments" ][0 ]["title_link" ] = (
203
203
cbkwargs ["flower_base_url" ] +
204
- ' /task/{tid}' .format (tid = task_id )
204
+ " /task/{tid}" .format (tid = task_id )
205
205
)
206
206
207
207
return attachment
@@ -225,7 +225,7 @@ def get_celery_startup_attachment(**kwargs):
225
225
"mrkdwn_in" : ["text" ]
226
226
}
227
227
],
228
- "text" : ''
228
+ "text" : ""
229
229
}
230
230
231
231
return attachment
@@ -249,7 +249,7 @@ def get_celery_shutdown_attachment(**kwargs):
249
249
"mrkdwn_in" : ["text" ]
250
250
}
251
251
],
252
- "text" : ''
252
+ "text" : ""
253
253
}
254
254
255
255
return attachment
@@ -266,27 +266,27 @@ def get_beat_init_attachment(**kwargs):
266
266
267
267
beat_schedule = kwargs ["beat_schedule" ]
268
268
if beat_schedule :
269
- message += ' *with schedule:*'
269
+ message += " *with schedule:*"
270
270
271
271
sched = []
272
272
for task in sorted (beat_schedule ):
273
273
if kwargs ["beat_show_full_task_path" ]:
274
274
sched .append (
275
275
task + BEAT_DELIMITER +
276
- schedule_to_string (beat_schedule [task ][' schedule' ]))
276
+ schedule_to_string (beat_schedule [task ][" schedule" ]))
277
277
else :
278
278
sched .append (
279
- task .split ('.' , 2 )[- 1 ] + BEAT_DELIMITER +
280
- schedule_to_string (beat_schedule [task ][' schedule' ]))
279
+ task .split ("." , 2 )[- 1 ] + BEAT_DELIMITER +
280
+ schedule_to_string (beat_schedule [task ][" schedule" ]))
281
281
schedule = (
282
- ' \n *Task{}crontab(m/h/d/dM/MY) OR solar OR interval:*\n ' .format (
282
+ " \n *Task{}crontab(m/h/d/dM/MY) OR solar OR interval:*\n " .format (
283
283
BEAT_DELIMITER
284
284
) +
285
- ' ```' + ' \n ' .join (sched ) + ' ```'
285
+ " ```" + " \n " .join (sched ) + " ```"
286
286
)
287
287
else :
288
- message = message [:- 1 ] + '.' + message [- 1 :]
289
- schedule = ''
288
+ message = message [:- 1 ] + "." + message [- 1 :]
289
+ schedule = ""
290
290
291
291
attachment = {
292
292
"attachments" : [
@@ -297,7 +297,7 @@ def get_beat_init_attachment(**kwargs):
297
297
"mrkdwn_in" : ["text" ]
298
298
}
299
299
],
300
- "text" : ''
300
+ "text" : ""
301
301
}
302
302
303
303
return attachment
@@ -313,12 +313,14 @@ def get_broker_disconnect_attachment(**kwargs):
313
313
"""Create the slack message attachment for broker disconnection."""
314
314
if kwargs ["show_celery_hostname" ]:
315
315
message = "*{process} could not connect to broker on {host}.*" .format (
316
- process = processes .get (current_process ()._name , "" ),
316
+ process = processes .get (
317
+ current_process ()._name , current_process ()._name ),
317
318
host = socket .gethostname ()
318
319
)
319
320
else :
320
321
message = "*{process} could not connect to broker.*" .format (
321
- process = processes .get (current_process ()._name , "" ),
322
+ process = processes .get (
323
+ current_process ()._name , current_process ()._name ),
322
324
)
323
325
324
326
attachment = {
@@ -330,7 +332,7 @@ def get_broker_disconnect_attachment(**kwargs):
330
332
"mrkdwn_in" : ["text" ]
331
333
}
332
334
],
333
- "text" : ''
335
+ "text" : ""
334
336
}
335
337
336
338
return attachment
@@ -340,12 +342,14 @@ def get_broker_connect_attachment(**kwargs):
340
342
"""Create the slack message attachment for broker connection."""
341
343
if kwargs ["show_celery_hostname" ]:
342
344
message = "*{process} (re)connected to broker on {host}.*" .format (
343
- process = processes .get (current_process ()._name , "" ),
345
+ process = processes .get (
346
+ current_process ()._name , current_process ()._name ),
344
347
host = socket .gethostname ()
345
348
)
346
349
else :
347
350
message = "*{process} (re)connected to broker.*" .format (
348
- process = processes .get (current_process ()._name , "" ),
351
+ process = processes .get (
352
+ current_process ()._name , current_process ()._name ),
349
353
)
350
354
351
355
attachment = {
@@ -357,7 +361,7 @@ def get_broker_connect_attachment(**kwargs):
357
361
"mrkdwn_in" : ["text" ]
358
362
}
359
363
],
360
- "text" : ''
364
+ "text" : ""
361
365
}
362
366
363
367
return attachment
@@ -367,9 +371,9 @@ def schedule_to_string(schedule):
367
371
"""Transform a crontab, solar, or timedelta to a string."""
368
372
if isinstance (schedule , crontab ):
369
373
return str (schedule )[10 :- 15 ]
370
- elif CELERY_VERSION >= ' 4.0.0' and isinstance (schedule , solar ):
374
+ elif CELERY_VERSION >= " 4.0.0" and isinstance (schedule , solar ):
371
375
return str (schedule )[8 :- 1 ]
372
376
elif isinstance (schedule , timedelta ):
373
- return ' every ' + str (schedule )
377
+ return " every " + str (schedule )
374
378
else :
375
- return ' every ' + str (schedule ) + ' seconds'
379
+ return " every " + str (schedule ) + " seconds"
0 commit comments