def CommandHandlerTarget(worker, cmd_future, log_future, ping_handler): logging.info("Gearman command handler starting") context = zmq.Context() cmd_socket = InitSocket(context, cmd_future, type=zmq.SUB, options={zmq.SUBSCRIBE: ""}) log_socket = InitSocket(context, log_future, type=zmq.PUB) poller = zmq.Poller() poller.register(cmd_socket, zmq.POLLIN) while True: socks = dict(poller.poll()) if cmd_socket in socks: cmd = cmd_socket.recv_pyobj() if cmd == COMMAND_QUIT: logging.info("Gearman worker quiting") # exit without error, so wrapper script will not restart worker worker.exit_status = 0 worker.finish = True return elif cmd == COMMAND_RESTART: logging.info("Gearman worker restarting") # exit with error, so worker will be restarted worker.exit_status = -1 worker.finish = True return elif cmd == COMMAND_PING: logging.info("Gearman worker received ping") stats = ping_handler() log_socket.send_pyobj(stats) else: logging.warn("Gearman worker got unknown command: %s" % (cmd,))
def CommandHandlerTarget(worker, cmd_future, log_future, ping_handler): """Logic for process that handles command messages. A *quit* or *restart* command is handled by setting the worker's :attr:`finish <Worker.finish>` flag to True, setting the exit status, and returning. The exit status is 0 on *quit*, or -1 on *restart*. :param Worker worker: Instance for tracking worker state. :param FutureSocket cmd_future: Socket information for the command channel. :param FutureSocket log_future: Socket information for the log channel. :param callable ping_handler: Generates a response message for a *ping* command. """ logging.info("Gearman command handler starting") context = zmq.Context() cmd_socket = InitSocket(context, cmd_future, type = zmq.SUB, options = {zmq.SUBSCRIBE : ""}) log_socket = InitSocket(context, log_future, type = zmq.PUB) poller = zmq.Poller() poller.register(cmd_socket, zmq.POLLIN) while True: socks = dict(poller.poll()) if cmd_socket in socks: cmd, payload = cmd_socket.recv_pyobj() if cmd == COMMAND_QUIT: logging.info("Gearman worker quiting") # exit without error, so wrapper script will not restart worker worker.exit_status = 0 worker.finish = True return elif cmd == COMMAND_RESTART: logging.info("Gearman worker restarting") # exit with error, so worker will be restarted worker.exit_status = -1 worker.finish = True return elif cmd == COMMAND_PING: logging.info("Gearman worker received ping") stats = ping_handler() log_socket.send_pyobj(stats) elif cmd == COMMAND_SETMEMORY: logging.info("Gearman worker received SETMEMORY") if isinstance(payload, dict): memory = worker.memory for k, v in payload.items(): logging.info("\tSETMEMORY[%s]" % k) memory[k] = v else: logging.warn("Invalid payload received for SETMEMORY command") elif cmd == COMMAND_CLEARMEMORY: logging.info("Gearman worker received CLEARMEMORY") worker.memory.clear() else: logging.warn("Gearman worker got unknown command: %s" % (cmd,))
def CommandHandlerTarget(worker, cmd_future, log_future, ping_handler): """Logic for process that handles command messages. A *quit* or *restart* command is handled by setting the worker's :attr:`finish <Worker.finish>` flag to True, setting the exit status, and returning. The exit status is 0 on *quit*, or -1 on *restart*. :param Worker worker: Instance for tracking worker state. :param FutureSocket cmd_future: Socket information for the command channel. :param FutureSocket log_future: Socket information for the log channel. :param callable ping_handler: Generates a response message for a *ping* command. """ logging.info("Gearman command handler starting") context = zmq.Context() cmd_socket = InitSocket(context, cmd_future, type=zmq.SUB, options={zmq.SUBSCRIBE: ""}) log_socket = InitSocket(context, log_future, type=zmq.PUB) poller = zmq.Poller() poller.register(cmd_socket, zmq.POLLIN) while True: socks = dict(poller.poll()) if cmd_socket in socks: cmd, payload = cmd_socket.recv_pyobj() if cmd == COMMAND_QUIT: logging.info("Gearman worker quiting") # exit without error, so wrapper script will not restart worker worker.exit_status = 0 worker.finish = True return elif cmd == COMMAND_RESTART: logging.info("Gearman worker restarting") # exit with error, so worker will be restarted worker.exit_status = -1 worker.finish = True return elif cmd == COMMAND_PING: logging.info("Gearman worker received ping") stats = ping_handler() log_socket.send_pyobj(stats) elif cmd == COMMAND_SETMEMORY: logging.info("Gearman worker received SETMEMORY") if not isinstance(payload, dict): logging.warn( "Invalid payload received for SETMEMORY command") memory = worker.memory for k, v in payload.items(): memory[k] = v elif cmd == COMMAND_CLEARMEMORY: logging.info("Gearman worker received CLEARMEMORY") worker.memory.clear() else: logging.warn("Gearman worker got unknown command: %s" % (cmd, ))