title: Executing a function right before\after revoking a task in celery
body: <p>For the demonstration, I have a class that does something like this:</p>
  <pre><code>class Writer:
      def __init__(self, num):
          self.num = num
      def write(self):
          for i in range(0, num):
              with open(&quot;/outfile&quot;, &quot;w&quot;) as o:
                  o.write(i)
      
      def cleanup(self): # should be executed when write has not finished
          os.system('rm -rf /outfile')
  </code></pre>
  <p>Im using a shared task in celery to execute it like:</p>
  <pre><code>@shared_task
  def mytask():
      w = Writer(100)
      w.write()
      return &quot;Done&quot;
  </code></pre>
  <p>Im using django and DRF, and i have an endpoint api that recieves a task_id and revokes the task using <code>revoke(task_id, terminate=True)</code>
  My goal is to run the <code>cleanup</code> function from the <code>Writer</code> class when the revoke happens.
  The reason I cannot just run <code>cleanup</code> after the revoke as a regular function is because it needs to access the attributes of its class (in the real senario and not this example), and each class might have a different <code>cleanup</code> function.
  how can i detect the revoke in <code>mytask()</code> ? Is there a solution using something like try except? Moving the task to a class based task? Have not found anything online or in the docs.
  Thanks in advance.</p>