Giter Club home page Giter Club logo

python-daemon-manager's Introduction

python-daemon-manager

python-daemon-manager is a daemon manager and framework that allows you to easily implement daemon managing functions in dozens lines of code.

It provides easy and flexible API for Python and simplest configuration file you ever seen

Installation

At this time installation from debian repository is unavailable, so you need firstly build a package

sudo apt-get install devscripts debhelper cdbs
git clone [email protected]:kazarinov/python-daemon-manager.git
cd python-daemon-manager
debuild
sudo debi

Debian package will be installed and two command line tools will be available:

sudo daemon-tools list
> No enabled daemons

sudo daemon-runtime status
> running (22693)

A motivating example

Here's some extremely simple daemon that was written with the use of Python daemon framework

#!/usr/bin/env python

import time
import logging

from upstart.daemon import Daemon

log = logging.getLogger('test')

class MyDaemon(Daemon):
    def run(self, param='default'):
        while True:
            print "running"
            log.debug("param=%s", param)
            log.debug('running')
            time.sleep(1)


if __name__ == '__main__':
    MyDaemon(
        log=log,
        pidfile='daemon-test.pid',
        stop_timeout=1
    ).execute()

Another motivating example with master-workers paradigm

#!/usr/bin/env python

import time
import logging

from upstart.daemon import DaemonMaster, DaemonWorker

log = logging.getLogger('test')

class MyDaemonWorker(DaemonWorker):
    def __init__(self, i):
        super(MyDaemonWorker, self).__init__()
        self.i = i

    def run(self):
        while True:
            log.debug('running worker %s', self.i)
            time.sleep(1)


class MyDaemonMaster(DaemonMaster):
    def get_workers(self):
        workers = []
        for i in xrange(self.workers_number):
            workers.append(MyDaemonWorker(i))
        return workers

    def start_worker(self, worker):
        return MyDaemonWorker(worker.i)

    def run(self, workers=10):
        self.workers_number = workers
        super(MyDaemonMaster, self).run()


if __name__ == '__main__':
    MyDaemonMaster(
        log=log,
        pidfile='workers-test.pid',
        stop_timeout=1
    ).execute()

Writing configuration file

A simpliest example of config

pid: daemon-test.pid 
expect: daemon
run: simple.py start
  • pid - path to pid file
  • expect - (None, fork, daemon) - expecting daemonization mechanizm of running script
  • run - path to running script

Put this file to /etc/daemon-manager/conf-enabled/simple-daemon and run

daemon-tools list
> Following daemons are enabled:
> * simple-daemon
daemon-tools status
> * simple-daemon is stopped
daemon-tools start simple-daemon
> starting simple-daemon ... started (22921)
daemon-tools status
> * simple-daemon is running (22921)

python-daemon-manager's People

Contributors

kazarinov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.