Exemple #1
0
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], '',
                                   ['reload', 'quit', 'status', 'config=',
                                    'dump=', 'run=', 'pause', 'quiet', 'resume',
                                    'show-job=', 'with-deps', 'kill'])
    except getopt.GetoptError:
        usage(1)
    #global scheduled_jobs
    alt_config = with_deps = quiet = False
    for opt, val in opts:
        if opt == '--with-deps':
            with_deps = True
        elif opt == '--quiet':
            quiet = True
    for opt, val in opts:
        if opt in('--reload', '--quit', '--status', '--run', '--pause',
                  '--resume', '--show-job', '--kill'):
            if opt == '--reload':
                cmd = 'RELOAD'
            elif opt == '--quit':
                cmd = 'QUIT'
            elif opt == '--status':
                cmd = 'STATUS'
            elif opt == '--pause':
                cmd = 'PAUSE'
            elif opt == '--resume':
                cmd = 'RESUME'
            elif opt == '--run':
                cmd = 'RUNJOB %s %i' % (val, with_deps)
            elif opt == '--show-job':
                cmd = 'SHOWJOB %s' % val
            elif opt == '--kill':
                cmd = 'KILL'
            sock = SocketHandling(logger)
            try:
                print "Response: %s" % sock.send_cmd(cmd)
            except SocketHandling.Timeout:
                print "Timout contacting server, is it running?"
            sys.exit(0)
        elif opt in ('--config',):
            if val.find("/") == -1:
                sys.path.insert(0, '.')
                name = val
            else:
                sys.path.insert(0, val[:val.rindex("/")])
                name = val[val.rindex("/")+1:]
            name = name[:name.rindex(".")]
            exec("import %s as tmp" % name)
            scheduled_jobs = tmp
            # sys.path = sys.path[1:] #With this reload(module) loads another file(!)
            alt_config = True
        elif opt in ('--dump',):
            JobQueue.dump_jobs(scheduled_jobs, int(val))
            sys.exit(0)
    if not alt_config:
        import scheduled_jobs
    sock = SocketHandling(logger)
    ca = CallableAction()
    ca.set_id("master_jr_lock")
    try:
        if(sock.ping_server()):
            if not quiet:
                print "Server already running"
            sys.exit(1)
        try:
            ca.check_lockfile()
        except LockExists:
            logger.error(
                ("%s: Master lock exists, but jr-socket didn't respond to "+
                 "ping. This should be a very rare error!") %
                ca.lockfile_name)
            sys.exit(1)
        ca.make_lockfile()
    except SocketHandling.Timeout:
        # Assuming that previous run aborted without removing socket
        logger.warn("Socket timeout, assuming server is dead")
        try:
            os.unlink(cereconf.JOB_RUNNER_SOCKET)
        except OSError:
            pass
        pass
    jr = JobRunner(scheduled_jobs)
    if True:
        socket_thread = threading.Thread(target=sock.start_listener, args=(jr,))
        socket_thread.setDaemon(True)
        socket_thread.setName("socket_thread")
        socket_thread.start()

    jr.run_job_loop()
    logger.debug("bye")
    sock.cleanup()
    ca.free_lock()
Exemple #2
0
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], '', [
            'reload', 'quit', 'status', 'config=', 'dump=', 'run=', 'pause',
            'quiet', 'resume', 'show-job=', 'with-deps', 'kill'
        ])
    except getopt.GetoptError:
        usage(1)
    #global scheduled_jobs
    alt_config = with_deps = quiet = False
    for opt, val in opts:
        if opt == '--with-deps':
            with_deps = True
        elif opt == '--quiet':
            quiet = True
    for opt, val in opts:
        if opt in ('--reload', '--quit', '--status', '--run', '--pause',
                   '--resume', '--show-job', '--kill'):
            if opt == '--reload':
                cmd = 'RELOAD'
            elif opt == '--quit':
                cmd = 'QUIT'
            elif opt == '--status':
                cmd = 'STATUS'
            elif opt == '--pause':
                cmd = 'PAUSE'
            elif opt == '--resume':
                cmd = 'RESUME'
            elif opt == '--run':
                cmd = 'RUNJOB %s %i' % (val, with_deps)
            elif opt == '--show-job':
                cmd = 'SHOWJOB %s' % val
            elif opt == '--kill':
                cmd = 'KILL'
            sock = SocketHandling(logger)
            try:
                print "Response: %s" % sock.send_cmd(cmd)
            except SocketHandling.Timeout:
                print "Timout contacting server, is it running?"
            sys.exit(0)
        elif opt in ('--config', ):
            if val.find("/") == -1:
                sys.path.insert(0, '.')
                name = val
            else:
                sys.path.insert(0, val[:val.rindex("/")])
                name = val[val.rindex("/") + 1:]
            name = name[:name.rindex(".")]
            exec("import %s as tmp" % name)
            scheduled_jobs = tmp
            # sys.path = sys.path[1:] #With this reload(module) loads another file(!)
            alt_config = True
        elif opt in ('--dump', ):
            JobQueue.dump_jobs(scheduled_jobs, int(val))
            sys.exit(0)
    if not alt_config:
        import scheduled_jobs
    sock = SocketHandling(logger)
    ca = CallableAction()
    ca.set_id("master_jr_lock")
    try:
        if (sock.ping_server()):
            if not quiet:
                print "Server already running"
            sys.exit(1)
        try:
            ca.check_lockfile()
        except LockExists:
            logger.error(
                ("%s: Master lock exists, but jr-socket didn't respond to " +
                 "ping. This should be a very rare error!") % ca.lockfile_name)
            sys.exit(1)
        ca.make_lockfile()
    except SocketHandling.Timeout:
        # Assuming that previous run aborted without removing socket
        logger.warn("Socket timeout, assuming server is dead")
        try:
            os.unlink(cereconf.JOB_RUNNER_SOCKET)
        except OSError:
            pass
        pass
    jr = JobRunner(scheduled_jobs)
    if True:
        socket_thread = threading.Thread(target=sock.start_listener,
                                         args=(jr, ))
        socket_thread.setDaemon(True)
        socket_thread.setName("socket_thread")
        socket_thread.start()

    jr.run_job_loop()
    logger.debug("bye")
    sock.cleanup()
    ca.free_lock()