0

I have tried to add following content in celery beat "before_task_publish" signal as below in my task.py file

@before_task_publish.connect
def task_before_publish_handler(*args, **kwargs):
    my_data={"foo":"bar"}
    kwargs['request'][1]['my_data']=my_dat
    return kwargs

Now in the worker end of task.py I tried to intercept the message using signal "task_received", as below,

@task_received.connect
def task_receive_handler(*args, **kwargs):
    print(kwargs['request'])

Here in the receiver end signals I get the message in kwargs['request'] but not my appended value my_data. May be how I intercept in the celery beat signal is not proper

Naggappan Ramukannan
  • 2,564
  • 9
  • 36
  • 59
  • 1
    I believe the following thread answers your question: https://stackoverflow.com/questions/55660979/in-celery-what-is-the-appropriate-way-to-pass-contextual-metadata-from-sender-p – DejanLekic Oct 10 '19 at 11:01

1 Answers1

2

I don't think you need to involve signals here. As per the documentation, you can specify options argument when setting beat entries, which can hold any arguments supported by apply_async. One of the arguments is headers which is what you want I guess.

Tomáš Linhart
  • 9,832
  • 1
  • 27
  • 39
  • 1
    my actual value is kind of ID which will be generated and passed on for tracking purpose. I am using newrelic third party tool to do distributed tracing. So I can't put that during the schedule time the life cycle starts only when the beat component put the message into the queue. – Naggappan Ramukannan Oct 10 '19 at 08:56
  • Why don't you then refactor your task to grab that extra information right at the start? – DejanLekic Oct 10 '19 at 09:41
  • @DejanLekic How to refactor at the start? Because lets say I have created a schedule every day 5pm and when every cycle is getting started I need to add some ID and other data to message and pass that to the workers . Please let me know if this is supported by celery-beat – Naggappan Ramukannan Oct 10 '19 at 10:13
  • 1
    I think task can have extra logic to grab the necessary data based on its arguments... It is not a job of the scheduler to pass different arguments to its tasks. Its job is to run tasks at particular time. – DejanLekic Oct 10 '19 at 10:17