extending.rst 3.3 KB

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