extending.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. .. _guide-extending:
  2. ==========================
  3. Extensions and Bootsteps
  4. ==========================
  5. .. contents::
  6. :local:
  7. :depth: 2
  8. .. _extending-bootsteps:
  9. Bootsteps
  10. =========
  11. .. figure:: ../images/worker_graph_full.png
  12. .. _extending-worker-bootsteps:
  13. Worker Bootsteps
  14. ================
  15. .. figure:: ../images/worker_graph.png
  16. :width: 700px
  17. Consumer Bootsteps
  18. ==================
  19. .. figure:: ../images/consumer_graph.png
  20. :width: 700px
  21. .. _extending-programs:
  22. Command-line programs
  23. =====================
  24. .. _extending-commandoptions:
  25. Adding new command-line options
  26. -------------------------------
  27. You can add additional command-line options to the ``worker``, ``beat`` and
  28. ``events`` commands by modifying the :attr:`~@Celery.user_options` attribute of the
  29. application instance.
  30. Celery commands uses the :mod:`optparse` module to parse command-line
  31. arguments, and so you have to use optparse specific option instances created
  32. using :func:`optparse.make_option`. Please see the :mod:`optparse`
  33. documentation to read about the fields supported.
  34. Example adding a custom option to the :program:`celery worker` command:
  35. .. code-block:: python
  36. from celery import Celery
  37. from optparse import make_option as Option
  38. celery = Celery(broker='amqp://')
  39. celery.user_options['worker'].add(
  40. Option('--enable-my-option', action='store_true', default=False,
  41. help='Enable custom option.'),
  42. )
  43. .. _extending-subcommands:
  44. Adding new :program:`celery` sub-commands
  45. -----------------------------------------
  46. New commands can be added to the :program:`celery` umbrella command by using
  47. `setuptools entry-points`_.
  48. .. _`setuptools entry-points`:
  49. http://reinout.vanrees.org/weblog/2010/01/06/zest-releaser-entry-points.html
  50. Entry-points is special metadata that can be added to your packages ``setup.py`` program,
  51. and then after installation, read from the system using the :mod:`pkg_resources` module.
  52. Celery recognizes ``celery.commands`` entry-points to install additional
  53. subcommands, where the value of the entry-point must point to a valid subclass
  54. of :class:`celery.bin.base.Command`. Sadly there is limited documentation,
  55. but you can find inspiration from the various commands in the
  56. :mod:`celery.bin` package.
  57. This is how the Flower_ monitoring extension adds the :program:`celery flower` command,
  58. by adding an entry-point in :file:`setup.py`:
  59. .. code-block:: python
  60. setup(
  61. name='flower',
  62. entry_points={
  63. 'celery.commands': [
  64. 'flower = flower.command.FlowerCommand',
  65. ],
  66. }
  67. )
  68. .. _Flower: http://pypi.python.org/pypi/flower
  69. The command definition is in two parts separated by the equal sign, where the
  70. first part is the name of the subcommand (flower), then the fully qualified
  71. module path to the class that implements the command
  72. (``flower.command.FlowerCommand``).
  73. In the module :file:`flower/command.py`, the command class is defined
  74. something like this:
  75. .. code-block:: python
  76. from celery.bin.base import Command, Option
  77. class FlowerCommand(Command):
  78. def get_options(self):
  79. return (
  80. Option('--port', default=8888, type='int',
  81. help='Webserver port',
  82. ),
  83. Option('--debug', action='store_true'),
  84. )
  85. def run(self, port=None, debug=False, **kwargs):
  86. print('Running our command')