task-retries.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Retrying Tasks &mdash; Celery v0.7.0 (unstable) documentation</title>
  7. <link rel="stylesheet" href="../static/nature.css" type="text/css" />
  8. <link rel="stylesheet" href="../static/pygments.css" type="text/css" />
  9. <script type="text/javascript">
  10. var DOCUMENTATION_OPTIONS = {
  11. URL_ROOT: '../',
  12. VERSION: '0.7.0 (unstable)',
  13. COLLAPSE_MODINDEX: false,
  14. FILE_SUFFIX: '.html',
  15. HAS_SOURCE: true
  16. };
  17. </script>
  18. <script type="text/javascript" src="../static/jquery.js"></script>
  19. <script type="text/javascript" src="../static/doctools.js"></script>
  20. <link rel="top" title="Celery v0.7.0 (unstable) documentation" href="../index.html" />
  21. <link rel="up" title="Cookbook" href="index.html" />
  22. <link rel="next" title="Tutorials" href="../tutorials/index.html" />
  23. <link rel="prev" title="Cookbook" href="index.html" />
  24. </head>
  25. <body>
  26. <div class="related">
  27. <h3>Navigation</h3>
  28. <ul>
  29. <li class="right" style="margin-right: 10px">
  30. <a href="../genindex.html" title="General Index"
  31. accesskey="I">index</a></li>
  32. <li class="right" >
  33. <a href="../modindex.html" title="Global Module Index"
  34. accesskey="M">modules</a> |</li>
  35. <li class="right" >
  36. <a href="../tutorials/index.html" title="Tutorials"
  37. accesskey="N">next</a> |</li>
  38. <li class="right" >
  39. <a href="index.html" title="Cookbook"
  40. accesskey="P">previous</a> |</li>
  41. <li><a href="../index.html">Celery v0.7.0 (unstable) documentation</a> &raquo;</li>
  42. <li><a href="index.html" accesskey="U">Cookbook</a> &raquo;</li>
  43. </ul>
  44. </div>
  45. <div class="document">
  46. <div class="documentwrapper">
  47. <div class="bodywrapper">
  48. <div class="body">
  49. <div class="section" id="retrying-tasks">
  50. <h1>Retrying Tasks<a class="headerlink" href="#retrying-tasks" title="Permalink to this headline">¶</a></h1>
  51. <div class="section" id="retrying-a-task-if-something-fails">
  52. <h2>Retrying a task if something fails<a class="headerlink" href="#retrying-a-task-if-something-fails" title="Permalink to this headline">¶</a></h2>
  53. <p>Simply use <a title="celery.task.base.Task.retry" class="reference external" href="../reference/celery.task.base.html#celery.task.base.Task.retry"><tt class="xref docutils literal"><span class="pre">celery.task.base.Task.retry()</span></tt></a> to re-sent the task, it will
  54. do the right thing, and respect the <a title="celery.task.base.Task.max_retries" class="reference external" href="../reference/celery.task.base.html#celery.task.base.Task.max_retries"><tt class="xref docutils literal"><span class="pre">celery.task.base.Task.max_retries</span></tt></a>
  55. attribute.</p>
  56. <div class="highlight-python"><pre>class SendTwitterStatusTask(Task):
  57. def run(self, oauth, tweet, \*\*kwargs):
  58. try:
  59. twitter = Twitter(oauth)
  60. twitter.update_status(tweet)
  61. except (Twitter.FailWhaleError, Twitter.LoginError), exc:
  62. self.retry(args=[oauth, tweet], kwargs=\*\*kwargs, exc=exc)</pre>
  63. </div>
  64. <p>Here we used the <tt class="docutils literal"><span class="pre">exc</span></tt> argument to pass the current exception to
  65. <a title="celery.task.base.Task.retry" class="reference external" href="../reference/celery.task.base.html#celery.task.base.Task.retry"><tt class="xref docutils literal"><span class="pre">celery.task.base.Task.retry()</span></tt></a>. At each step of the retry this exception
  66. is available as the tombstone (result) of the task, when
  67. <a title="celery.task.base.Task.max_retries" class="reference external" href="../reference/celery.task.base.html#celery.task.base.Task.max_retries"><tt class="xref docutils literal"><span class="pre">celery.task.base.Task.max_retries</span></tt></a> has been exceeded this is the exception
  68. raised. However, if an <tt class="docutils literal"><span class="pre">exc</span></tt> argument is not provided the
  69. <a title="celery.task.base.RetryTaskError" class="reference external" href="../reference/celery.task.base.html#celery.task.base.RetryTaskError"><tt class="xref docutils literal"><span class="pre">celery.task.base.RetryTaskError</span></tt></a> exception is raised instead.</p>
  70. </div>
  71. <div class="section" id="setting-a-custom-delay-for-retries">
  72. <h2>Setting a custom delay for retries.<a class="headerlink" href="#setting-a-custom-delay-for-retries" title="Permalink to this headline">¶</a></h2>
  73. <p>The default countdown is in the tasks
  74. <a title="celery.task.base.Task.default_retry_delay" class="reference external" href="../reference/celery.task.base.html#celery.task.base.Task.default_retry_delay"><tt class="xref docutils literal"><span class="pre">celery.task.base.Task.default_retry_delay</span></tt></a> attribute, which by
  75. default is set to 3 minutes.</p>
  76. <p>You can also provide the <tt class="docutils literal"><span class="pre">countdown</span></tt> argument to
  77. <a title="celery.task.base.Task.retry" class="reference external" href="../reference/celery.task.base.html#celery.task.base.Task.retry"><tt class="xref docutils literal"><span class="pre">celery.task.base.Task.retry()</span></tt></a> to override this default.</p>
  78. <div class="highlight-python"><pre>class MyTask(Task):
  79. default_retry_delay = 30 * 60 # retry in 30 minutes
  80. def run(self, x, y, \*\*kwargs):
  81. try:
  82. ...
  83. except Exception, exc:
  84. self.retry([x, y], \*\*kwargs, exc=exc,
  85. countdown=60 # override the default and
  86. # retry in 1 minute</pre>
  87. </div>
  88. </div>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93. <div class="sphinxsidebar">
  94. <div class="sphinxsidebarwrapper">
  95. <h3><a href="../index.html">Table Of Contents</a></h3>
  96. <ul>
  97. <li><a class="reference external" href="">Retrying Tasks</a><ul>
  98. <li><a class="reference external" href="#retrying-a-task-if-something-fails">Retrying a task if something fails</a></li>
  99. <li><a class="reference external" href="#setting-a-custom-delay-for-retries">Setting a custom delay for retries.</a></li>
  100. </ul>
  101. </li>
  102. </ul>
  103. <h4>Previous topic</h4>
  104. <p class="topless"><a href="index.html"
  105. title="previous chapter">Cookbook</a></p>
  106. <h4>Next topic</h4>
  107. <p class="topless"><a href="../tutorials/index.html"
  108. title="next chapter">Tutorials</a></p>
  109. <h3>This Page</h3>
  110. <ul class="this-page-menu">
  111. <li><a href="../sources/cookbook/task-retries.txt"
  112. rel="nofollow">Show Source</a></li>
  113. </ul>
  114. <div id="searchbox" style="display: none">
  115. <h3>Quick search</h3>
  116. <form class="search" action="../search.html" method="get">
  117. <input type="text" name="q" size="18" />
  118. <input type="submit" value="Go" />
  119. <input type="hidden" name="check_keywords" value="yes" />
  120. <input type="hidden" name="area" value="default" />
  121. </form>
  122. <p class="searchtip" style="font-size: 90%">
  123. Enter search terms or a module, class or function name.
  124. </p>
  125. </div>
  126. <script type="text/javascript">$('#searchbox').show(0);</script>
  127. </div>
  128. </div>
  129. <div class="clearer"></div>
  130. </div>
  131. <div class="related">
  132. <h3>Navigation</h3>
  133. <ul>
  134. <li class="right" style="margin-right: 10px">
  135. <a href="../genindex.html" title="General Index"
  136. >index</a></li>
  137. <li class="right" >
  138. <a href="../modindex.html" title="Global Module Index"
  139. >modules</a> |</li>
  140. <li class="right" >
  141. <a href="../tutorials/index.html" title="Tutorials"
  142. >next</a> |</li>
  143. <li class="right" >
  144. <a href="index.html" title="Cookbook"
  145. >previous</a> |</li>
  146. <li><a href="../index.html">Celery v0.7.0 (unstable) documentation</a> &raquo;</li>
  147. <li><a href="index.html" >Cookbook</a> &raquo;</li>
  148. </ul>
  149. </div>
  150. <div class="footer">
  151. &copy; Copyright 2009, Ask Solem.
  152. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.2.
  153. </div>
  154. </body>
  155. </html>