#!/usr/bin/env python

import os
import sys
import logging
from jobqueue import daemon, worker

LOGDIR='/var/log/bumps'
LOGFILE=os.path.join(LOGDIR,'.bumps-worker.log')
PIDFILE=os.path.join(LOGDIR,'.bumps-worker.pid')
ERRFILE=os.path.join(LOGDIR,'.bumps-worker.out')

def startup():
    if not os.path.exists(LOGDIR): os.makedirs(LOGDIR)
    if len(sys.argv) > 1 and sys.argv[1] == 'debug':
        loglevel, logfile = logging.DEBUG, None
    else:
        loglevel, logfile = logging.ERROR, LOGFILE
        daemon.startstop(pidfile=PIDFILE, stdout=ERRFILE)
    logging.basicConfig(level=loglevel,
                        format='%(asctime)s %(levelname)-8s %(message)s',
                        datefmt = '%y-%m-%d %H:%M:%S',
                        filename = logfile, filemode='a')
    worker.main()

if __name__ == "__main__": startup()
