3
3
import sys
4
4
import shutil
5
5
import functools
6
+ import tempfile
6
7
7
8
from sentry_sdk .consts import OP
8
9
from sentry_sdk ._compat import reraise
@@ -320,6 +321,11 @@ def sentry_workloop(*args, **kwargs):
320
321
def _get_headers (task ):
321
322
# type: (Task) -> Dict[str, Any]
322
323
headers = task .request .get ("headers" ) or {}
324
+
325
+ if "headers" in headers :
326
+ headers .update (headers ["headers" ])
327
+ del headers ["headers" ]
328
+
323
329
return headers
324
330
325
331
@@ -392,9 +398,11 @@ def _reinstall_patched_tasks(app, sender, add_updated_periodic_tasks):
392
398
add_updated_periodic_task ()
393
399
394
400
# Start Celery Beat (with new (cloned) schedule, because old one is still in use)
395
- new_schedule_filename = sender .schedule_filename + ".new"
396
- shutil .copy2 (sender .schedule_filename , new_schedule_filename )
397
- app .Beat (schedule = new_schedule_filename ).run ()
401
+ cloned_schedule = tempfile .NamedTemporaryFile (suffix = "-patched-by-sentry-sdk" )
402
+ with open (sender .schedule_filename , "rb" ) as original_schedule :
403
+ shutil .copyfileobj (original_schedule , cloned_schedule )
404
+
405
+ app .Beat (schedule = cloned_schedule .name ).run ()
398
406
399
407
400
408
# Nested functions do not work as Celery hook receiver,
@@ -480,9 +488,7 @@ def crons_task_before_run(sender, **kwargs):
480
488
if "sentry-monitor-slug" not in headers :
481
489
return
482
490
483
- monitor_config = (
484
- headers ["sentry-monitor-config" ] if "sentry-monitor-config" in headers else {}
485
- )
491
+ monitor_config = headers .get ("sentry-monitor-config" , {})
486
492
487
493
start_timestamp_s = now ()
488
494
@@ -506,9 +512,7 @@ def crons_task_success(sender, **kwargs):
506
512
if "sentry-monitor-slug" not in headers :
507
513
return
508
514
509
- monitor_config = (
510
- headers ["sentry-monitor-config" ] if "sentry-monitor-config" in headers else {}
511
- )
515
+ monitor_config = headers .get ("sentry-monitor-config" , {})
512
516
513
517
start_timestamp_s = headers ["sentry-monitor-start-timestamp-s" ]
514
518
@@ -529,9 +533,7 @@ def crons_task_failure(sender, **kwargs):
529
533
if "sentry-monitor-slug" not in headers :
530
534
return
531
535
532
- monitor_config = (
533
- headers ["sentry-monitor-config" ] if "sentry-monitor-config" in headers else {}
534
- )
536
+ monitor_config = headers .get ("sentry-monitor-config" , {})
535
537
536
538
start_timestamp_s = headers ["sentry-monitor-start-timestamp-s" ]
537
539
@@ -552,9 +554,7 @@ def crons_task_retry(sender, **kwargs):
552
554
if "sentry-monitor-slug" not in headers :
553
555
return
554
556
555
- monitor_config = (
556
- headers ["sentry-monitor-config" ] if "sentry-monitor-config" in headers else {}
557
- )
557
+ monitor_config = headers .get ("sentry-monitor-config" , {})
558
558
559
559
start_timestamp_s = headers ["sentry-monitor-start-timestamp-s" ]
560
560
0 commit comments