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