Welcome | Get started | Dive | Contribute | Topics | Reference | Changes | More

lino.utils.daemoncommand

Copyright (c) 2009, Sean Creeley All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of the Optaros, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Modifications by Luc Saffre :

  • fixed: Overriding default values in a subclass didn’t work. /blog/2010/1210

  • added DaemonCommand.preserve_loggers class variable and get_handlers() and get_logger_files(). /blog/2010/1214

  • when a pidfile is specified and a file of that name already exists, DaemonCommand now no longer waits forever but raises a LockTimeout See /blog/2011/0126 and http://packages.python.org/lockfile/lockfile.html#lockfile.FileLock.acquire.

  • now works with versions of daemon who use “pidfile” instead of “pidlockfile”.

  • 20130610 Changes needed for Django 1.5: No longer overrides handle but execute. Converts any options ‘stdout’ and ‘stderr’ if given because Django expects them to be file-like objects (not names of files). Removed the stderr, stdout and stdin class attributes because they interferred with Django.

  • 2016-06-07 converted from optparse to argparse (changes needed for Django 1.10)

DaemonCommand

Django Management Command for starting a daemon.

Use

Simple use of daemon command:

from daemonextension import DaemonCommand
from django.conf import settings
import os

class Command(DaemonCommand):

    stdout = os.path.join(settings.DIRNAME, "log/cubbyscott.out")
    stderr = os.path.join(settings.DIRNAME, "log/cubbyscott.err")
    pidfile = os.path.join(settings.DIRNAME, "pid/cb_link.pid")

    def handle_daemon(self, *args, **options):
        from flopsy import Connection, Consumer
        consumer = Consumer(connection=Connection())
        consumer.declare(queue='links',
                         exchange='cubbyscott',
                         routing_key='importer', auto_delete=False)

        def message_callback(message):
            print 'Recieved: ' + message.body
            consumer.channel.basic_ack(message.delivery_tag)

        consumer.register(message_callback)

        consumer.wait()

call:

python python manage.py linkconsumer

Functions

get_handlers(logger)

get_logger_files(loggers)

Return a list of the file handles used by the specified loggers.

Classes

DaemonCommand([stdout, stderr, no_color, ...])

If you have an existing Django management command, just rename it's handle method to handle_daemon and inherit from this instead of django.core.management.base.BaseCommand.

lino.utils.daemoncommand.get_logger_files(loggers)

Return a list of the file handles used by the specified loggers. Thanks to http://mail.python.org/pipermail/python-list/2010-April/1241406.html

class lino.utils.daemoncommand.DaemonCommand(stdout=None, stderr=None, no_color=False, force_color=False)

Bases: BaseCommand

If you have an existing Django management command, just rename it’s handle method to handle_daemon and inherit from this instead of django.core.management.base.BaseCommand.

help = 'Create a daemon'

The loggers to preserve. If not None, this should be a list of loggers (logging.Logger instances) whose file handles should not get closed.

execute(*args, **options)

Takes the options and starts a daemon context from them.

Example:

python manage.py linkconsumer --pidfile=/var/run/cb_link.pid
    --stdout=/var/log/cb/links.out --stderr=/var/log/cb/links.err