Skip to content

Commit 23bddc9

Browse files
committed
add tests for broker message option
1 parent 5214636 commit 23bddc9

File tree

9 files changed

+353
-146
lines changed

9 files changed

+353
-146
lines changed

celery_slack/attachments.py

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
from celery import __version__ as CELERY_VERSION
1010
from celery.schedules import crontab
1111

12-
if CELERY_VERSION >= '4.0.0':
12+
if CELERY_VERSION >= "4.0.0":
1313
from celery.schedules import solar
1414

1515

1616
STOPWATCH = {}
17-
BEAT_DELIMITER = ' -> '
17+
BEAT_DELIMITER = " -> "
1818

1919

2020
def add_task_to_stopwatch(task_id):
@@ -24,25 +24,25 @@ def add_task_to_stopwatch(task_id):
2424

2525
def get_task_prerun_attachment(task_id, task, args, kwargs, **cbkwargs):
2626
"""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]
2828

29-
lines = ['Name: *' + task.name + '*']
29+
lines = ["Name: *" + task.name + "*"]
3030

3131
if cbkwargs["show_task_id"]:
32-
lines.append('Task ID: ' + task_id)
32+
lines.append("Task ID: " + task_id)
3333

3434
if cbkwargs["use_fixed_width"]:
3535
if cbkwargs["show_task_args"]:
36-
lines.append('args: ' + '`' + str(args) + '`')
36+
lines.append("args: " + "`" + str(args) + "`")
3737
if cbkwargs["show_task_kwargs"]:
38-
lines.append('kwargs: ' + '`' + str(kwargs) + '`')
38+
lines.append("kwargs: " + "`" + str(kwargs) + "`")
3939
else:
4040
if cbkwargs["show_task_args"]:
41-
lines.append('args: ' + str(args))
41+
lines.append("args: " + str(args))
4242
if cbkwargs["show_task_kwargs"]:
43-
lines.append('kwargs: ' + str(kwargs))
43+
lines.append("kwargs: " + str(kwargs))
4444

45-
executing = '\n'.join(lines)
45+
executing = "\n".join(lines)
4646

4747
attachment = {
4848
"attachments": [
@@ -54,13 +54,13 @@ def get_task_prerun_attachment(task_id, task, args, kwargs, **cbkwargs):
5454
"mrkdwn_in": ["text"]
5555
}
5656
],
57-
"text": ''
57+
"text": ""
5858
}
5959

6060
if cbkwargs["flower_base_url"]:
6161
attachment["attachments"][0]["title_link"] = (
6262
cbkwargs["flower_base_url"] +
63-
'/task/{tid}'.format(tid=task_id)
63+
"/task/{tid}".format(tid=task_id)
6464
)
6565

6666
return attachment
@@ -85,36 +85,36 @@ def get_task_success_attachment(task_name, retval, task_id,
8585
else:
8686
retval = str(retval)
8787

88-
message = 'SUCCESS -- ' + task_name.rsplit('.', 1)[-1]
88+
message = "SUCCESS -- " + task_name.rsplit(".", 1)[-1]
8989

9090
elapsed = divmod(time.time() - STOPWATCH.pop(task_id), 60)
9191

92-
lines = ['Name: *' + task_name + '*']
92+
lines = ["Name: *" + task_name + "*"]
9393

9494
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(
9696
m=str(int(elapsed[0])),
9797
s=str(int(elapsed[1])),
9898
))
9999
if cbkwargs["show_task_id"]:
100-
lines.append('Task ID: ' + task_id)
100+
lines.append("Task ID: " + task_id)
101101

102102
if cbkwargs["use_fixed_width"]:
103103
if cbkwargs["show_task_args"]:
104-
lines.append('args: ' + '`' + str(args) + '`')
104+
lines.append("args: " + "`" + str(args) + "`")
105105
if cbkwargs["show_task_kwargs"]:
106-
lines.append('kwargs: ' + '`' + str(kwargs) + '`')
106+
lines.append("kwargs: " + "`" + str(kwargs) + "`")
107107
if cbkwargs["show_task_return_value"]:
108-
lines.append('Returned: ' + '```' + str(retval) + '```')
108+
lines.append("Returned: " + "```" + str(retval) + "```")
109109
else:
110110
if cbkwargs["show_task_args"]:
111-
lines.append('args: ' + str(args))
111+
lines.append("args: " + str(args))
112112
if cbkwargs["show_task_kwargs"]:
113-
lines.append('kwargs: ' + str(kwargs))
113+
lines.append("kwargs: " + str(kwargs))
114114
if cbkwargs["show_task_return_value"]:
115-
lines.append('Returned: ' + str(retval))
115+
lines.append("Returned: " + str(retval))
116116

117-
success = '\n'.join(lines)
117+
success = "\n".join(lines)
118118

119119
attachment = {
120120
"attachments": [
@@ -126,13 +126,13 @@ def get_task_success_attachment(task_name, retval, task_id,
126126
"mrkdwn_in": ["text"]
127127
}
128128
],
129-
"text": ''
129+
"text": ""
130130
}
131131

132132
if cbkwargs["flower_base_url"]:
133133
attachment["attachments"][0]["title_link"] = (
134134
cbkwargs["flower_base_url"] +
135-
'/task/{tid}'.format(tid=task_id)
135+
"/task/{tid}".format(tid=task_id)
136136
)
137137

138138
return attachment
@@ -152,38 +152,38 @@ def get_task_failure_attachment(task_name, exc, task_id, args,
152152
STOPWATCH.pop(task_id)
153153
return
154154

155-
message = 'FAILURE -- ' + task_name.rsplit('.', 1)[-1]
155+
message = "FAILURE -- " + task_name.rsplit(".", 1)[-1]
156156

157157
elapsed = divmod(time.time() - STOPWATCH.pop(task_id), 60)
158158

159-
lines = ['Name: *' + task_name + '*']
159+
lines = ["Name: *" + task_name + "*"]
160160

161161
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(
163163
m=str(int(elapsed[0])),
164164
s=str(int(elapsed[1])),
165165
))
166166
if cbkwargs["show_task_id"]:
167-
lines.append('Task ID: ' + task_id)
167+
lines.append("Task ID: " + task_id)
168168

169169
if cbkwargs["use_fixed_width"]:
170170
if cbkwargs["show_task_args"]:
171-
lines.append('args: ' + '`' + str(args) + '`')
171+
lines.append("args: " + "`" + str(args) + "`")
172172
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) + "`")
175175
if cbkwargs["show_task_exception_info"]:
176-
lines.append('Info: ' + '```' + str(einfo) + '```')
176+
lines.append("Info: " + "```" + str(einfo) + "```")
177177
else:
178178
if cbkwargs["show_task_args"]:
179-
lines.append('args: ' + str(args))
179+
lines.append("args: " + str(args))
180180
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))
183183
if cbkwargs["show_task_exception_info"]:
184-
lines.append('Info: ' + str(einfo))
184+
lines.append("Info: " + str(einfo))
185185

186-
failure = '\n'.join(lines)
186+
failure = "\n".join(lines)
187187

188188
attachment = {
189189
"attachments": [
@@ -195,13 +195,13 @@ def get_task_failure_attachment(task_name, exc, task_id, args,
195195
"mrkdwn_in": ["text"]
196196
}
197197
],
198-
"text": ''
198+
"text": ""
199199
}
200200

201201
if cbkwargs["flower_base_url"]:
202202
attachment["attachments"][0]["title_link"] = (
203203
cbkwargs["flower_base_url"] +
204-
'/task/{tid}'.format(tid=task_id)
204+
"/task/{tid}".format(tid=task_id)
205205
)
206206

207207
return attachment
@@ -225,7 +225,7 @@ def get_celery_startup_attachment(**kwargs):
225225
"mrkdwn_in": ["text"]
226226
}
227227
],
228-
"text": ''
228+
"text": ""
229229
}
230230

231231
return attachment
@@ -249,7 +249,7 @@ def get_celery_shutdown_attachment(**kwargs):
249249
"mrkdwn_in": ["text"]
250250
}
251251
],
252-
"text": ''
252+
"text": ""
253253
}
254254

255255
return attachment
@@ -266,27 +266,27 @@ def get_beat_init_attachment(**kwargs):
266266

267267
beat_schedule = kwargs["beat_schedule"]
268268
if beat_schedule:
269-
message += ' *with schedule:*'
269+
message += " *with schedule:*"
270270

271271
sched = []
272272
for task in sorted(beat_schedule):
273273
if kwargs["beat_show_full_task_path"]:
274274
sched.append(
275275
task + BEAT_DELIMITER +
276-
schedule_to_string(beat_schedule[task]['schedule']))
276+
schedule_to_string(beat_schedule[task]["schedule"]))
277277
else:
278278
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"]))
281281
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(
283283
BEAT_DELIMITER
284284
) +
285-
'```' + '\n'.join(sched) + '```'
285+
"```" + "\n".join(sched) + "```"
286286
)
287287
else:
288-
message = message[:-1] + '.' + message[-1:]
289-
schedule = ''
288+
message = message[:-1] + "." + message[-1:]
289+
schedule = ""
290290

291291
attachment = {
292292
"attachments": [
@@ -297,7 +297,7 @@ def get_beat_init_attachment(**kwargs):
297297
"mrkdwn_in": ["text"]
298298
}
299299
],
300-
"text": ''
300+
"text": ""
301301
}
302302

303303
return attachment
@@ -313,12 +313,14 @@ def get_broker_disconnect_attachment(**kwargs):
313313
"""Create the slack message attachment for broker disconnection."""
314314
if kwargs["show_celery_hostname"]:
315315
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),
317318
host=socket.gethostname()
318319
)
319320
else:
320321
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),
322324
)
323325

324326
attachment = {
@@ -330,7 +332,7 @@ def get_broker_disconnect_attachment(**kwargs):
330332
"mrkdwn_in": ["text"]
331333
}
332334
],
333-
"text": ''
335+
"text": ""
334336
}
335337

336338
return attachment
@@ -340,12 +342,14 @@ def get_broker_connect_attachment(**kwargs):
340342
"""Create the slack message attachment for broker connection."""
341343
if kwargs["show_celery_hostname"]:
342344
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),
344347
host=socket.gethostname()
345348
)
346349
else:
347350
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),
349353
)
350354

351355
attachment = {
@@ -357,7 +361,7 @@ def get_broker_connect_attachment(**kwargs):
357361
"mrkdwn_in": ["text"]
358362
}
359363
],
360-
"text": ''
364+
"text": ""
361365
}
362366

363367
return attachment
@@ -367,9 +371,9 @@ def schedule_to_string(schedule):
367371
"""Transform a crontab, solar, or timedelta to a string."""
368372
if isinstance(schedule, crontab):
369373
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):
371375
return str(schedule)[8:-1]
372376
elif isinstance(schedule, timedelta):
373-
return 'every ' + str(schedule)
377+
return "every " + str(schedule)
374378
else:
375-
return 'every ' + str(schedule) + ' seconds'
379+
return "every " + str(schedule) + " seconds"

0 commit comments

Comments
 (0)