title: Retrieving Celery task kwargs from task-failed / uuid
body: <h2>Main Issue</h2>
  
  <p>I'm testing how to handle certain task failure, for example handling a 'TimeLimitExceeded' exception which instantly kills the task and is not 'catchable' (Yes...I'm aware of the existence of 'SoftTimeLimit' but it doesn't fit my needs).</p>
  
  <h3>First Approach</h3>
  
  <p>This is my <code>tasks.py</code> (The worker runs with a <code>--time-limit</code> flag):</p>
  
  <pre class="lang-py prettyprint-override"><code>import logging
  from celery import Celery
  import time
  
  
  app = Celery('tasks', broker='pyamqp://guest@localhost//')
  
  def my_fail(task, exc, req_id, req_args, req_kwargs, einfo, *ext_args, **kwargs):
      logger.info("args: %r", req_args)
      logger.info("kw: %r", req_kwargs)
  
  @app.task(on_failure=my_fail)
  def sum(x, y, delay=0, **kw):
      result = x+y
      if result == 4:
          raise Exception("Some Error")
      time.sleep(delay)                                                               
      return x+y
  </code></pre>
  
  <p>The main idea when a task fails, to be able to perform some handling based on the args/kwargs of the task</p>
  
  <p>For example if I run <code>sum.delay(3, 1, foo="bar")</code> the <code>Exception("Some Error")</code> is raised and the following is logged:</p>
  
  <pre><code>[2019-06-30 17:21:45,120: INFO/Worker-1] args: (3, 1)
  [2019-06-30 17:21:45,121: INFO/Worker-1] kw: {'foo': 'bar'}
  [2019-06-30 17:21:45,122: ERROR/MainProcess] Task tasks.sum[9e9de032-1469-44e7-8932-4c490fcee2e3] raised unexpected: Exception('Some Error',)
  Traceback (most recent call last):
    File "/home/apernin/.virtualenvs/dr/local/lib/python2.7/site-packages/celery/app/trace.py", line 240, in trace_task
      R = retval = fun(*args, **kwargs)
    File "/home/apernin/.virtualenvs/dr/local/lib/python2.7/site-packages/celery/app/trace.py", line 438, in __protected_call__
      return self.run(*args, **kwargs)
    File "/home/apernin/test/tasks.py", line 89, in sum
      raise Exception("Some Error")
  Exception: Some Error
  </code></pre>
  
  <p>Note the args/kwargs are printed by my <code>on-failure</code> handler.</p>
  
  <p>Now if I run <code>sum.delay(3, 2, delay=7)</code> the <strong>TimeLimit</strong> is triggered</p>
  
  <pre><code>[2019-06-30 17:23:15,244: INFO/MainProcess] Received task: tasks.sum[8c81398b-4378-401d-a674-a3bd3418ccde]
  [2019-06-30 17:23:21,070: ERROR/MainProcess] Task tasks.sum[8c81398b-4378-401d-a674-a3bd3418ccde] raised unexpected: TimeLimitExceeded(5.0,)
  Traceback (most recent call last):
    File "/home/apernin/.virtualenvs/dr/local/lib/python2.7/site-packages/billiard/pool.py", line 645, in on_hard_timeout
      raise TimeLimitExceeded(job._timeout)
  TimeLimitExceeded: TimeLimitExceeded(5.0,)
  [2019-06-30 17:23:21,071: ERROR/MainProcess] Hard time limit (5.0s) exceeded for tasks.sum[8c81398b-4378-401d-a674-a3bd3418ccde]
  [2019-06-30 17:23:21,629: ERROR/MainProcess] Process 'Worker-1' pid:15472 exited with 'signal 15 (SIGTERM)'
  </code></pre>
  
  <p>Note the args/kwargs are note printed, because of the <code>on-failure</code> handler not being excuted. This is somewhat to be expected due to the nature of Celery's Hard Time Limit.</p>
  
  <h3>Second Approach</h3>
  
  <p>My second approach is to use a event-listener.</p>
  
  <pre class="lang-py prettyprint-override"><code>from celery import Celery
  
  
  def my_monitor(app):
      state = app.events.State()
  
      def announce_failed_tasks(event):
          state.event(event)
          # task name is sent only with -received event, and state
          # will keep track of this for us.
          task = state.tasks.get(event['uuid'])
  
      with app.connection() as connection:
          recv = app.events.Receiver(connection, handlers={
                  'task-failed': announce_failed_tasks,
          })
          recv.capture(limit=None, timeout=None, wakeup=True)
  
  if __name__ == '__main__':
      app = Celery(broker='amqp://guest@localhost//')
      my_monitor(app)
  </code></pre>
  
  <p>The only info I was able to retrieve was the task <strong>uuid</strong>, I wasn't able to retrieve the name, args or kwargs of the task (the task object contains the attributes but are all None).</p>
  
  <h3>Question</h3>
  
  <p>Is there a way to either:</p>
  
  <ul>
  <li>Make the <code>on_failure</code> handler in case of a Hard Time Limit?</li>
  <li>Retrieve the tasks args/kwargs of a task with a <code>task-failed</code> event listener?</li>
  </ul>
  
  <p>Thanks in advance</p>