extending.rst 3.0 KB

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