title: Huey Task Queue handle error on last retry only & retrieve exception traceback
body: <p>Currently have this snippet set up in my <code>tasks.py</code> so that an email is sent to the Django project's admins by <code>huey</code> whenever a task fails:</p>
  
  <pre><code>from django.core.mail import mail_admins
  
  from huey import signals
  from huey.contrib import djhuey as huey
  
  
  @huey.signal(signals.SIGNAL_ERROR)
  def task_error(signal, task, exc):
      subject = f'Task [{task.name}] failed
      message = f"""Task ID: {task.id}'
  Args: {task.args}
  Kwargs: {task.kwargs}
  Exception: {exc}"""
      mail_admins(subject, message)
  </code></pre>
  
  <p>This results in the following (example) email subject</p>
  
  <pre><code>[Django] Task [test_task] failed
  </code></pre>
  
  <p>and body:</p>
  
  <pre><code>Task ID: 89e4bfb3-3cd3-4d6f-8693-68874caf21ec
  Args: (123,)
  Kwargs: {}
  Exception: division by zero
  </code></pre>
  
  <p>which is pretty slick but... questions:</p>
  
  <ol>
  <li>Right now, this is triggered whenever a task fails, including retries. I would like it to be sent only in the case all retries failed. So if a task has <code>retries=2</code>, right now I receive 3 emails (original error + 2 retries). How to have it send the email only on the last retry?</li>
  <li>Is there a way to print the exception's <code>exc</code> traceback?</li>
  </ol>
  
  <p>P.S. I tried setting it up via Django project logging, but this approach offers finer-grained control, so I'm satisfied with it.</p>
  
  <h2>Update</h2>
  
  <p>The module with updates based on @Adam Chainz &amp; @coleifer's answers (both correct), now looks like:</p>
  
  <pre><code>import traceback
  
  from django.core.mail import mail_admins
  
  from huey import signals
  from huey.contrib import djhuey as huey
  
  
  @huey.signal(signals.SIGNAL_ERROR)
  def task_error(signal, task, exc):
      if task.retries &gt; 0:
          return
      subject = f'Task [{task.name}] failed'
      message = f"""Task ID: {task.id}
  Args: {task.args}
  Kwargs: {task.kwargs}
  Exception: {exc}
  {traceback.format_exc()}"""
      mail_admins(subject, message)
  </code></pre>