def encode(self): """ Makes a raw message out of the Request object to be forwarded. """ self.msg = " ".join([ self.sender, self.conn_id, self.path, tnetstrings.dump(json.dumps(self.headers)) + tnetstrings.dump(self.body) ]) return self.msg
def stop(signum, frame): global loop, checkup_periodic, control_port, stop_timeout def stop_timeout(): print('Terminate request timed out, mongrel2 might be orphaned.') kill_mongrel_with_pid(M2_PID_PATH) shutdown() def terminate_resp(msg): print('Mongrel2 control port confirmed SIGTERM sent.') shutdown() def shutdown(): print('Shutting down.') remove_hosts(HOSTS) control_port.close() loop.stop() print('\nStopping.') # make sure checkup doesn't happen during termination checkup_periodic.stop() # register terminate response callback control_port.on_recv(terminate_resp) # prepare timeout stop_timeout = DelayedCallback(stop_timeout, STOP_TIMEOUT, io_loop=loop) stop_timeout.start() # send terminate request control_port.send(tnetstrings.dump(['terminate', {}]))
def send_checkup(): def checkup_timeout(): global service, responding, timeout timeout.stop() if not responding: # we've timed out, restart print('Mongrel2 not responding, attempting restart.') # since control port isn't responding, do a dirty kill just in case kill_mongrel_with_pid(M2_PID_PATH) # start her back up # TODO: add configurable delay here before starting again start_mongrel() def recv_response(msg): global responding responding = True # access globals global timeout, control_port, responding # listen for ping back control_port.on_recv(recv_response) # do what's needed to rescue on timeout responding = False timeout = DelayedCallback(checkup_timeout, CHECKUP_TIMEOUT, io_loop=loop) timeout.start() # send status request control_port.send(tnetstrings.dump(['status', {'what': 'net'}]))
import zmq from mongrel2 import tnetstrings from pprint import pprint CTX = zmq.Context() addr = "ipc://run/control" ctl = CTX.socket(zmq.REQ) print "CONNECTING" ctl.connect(addr) while True: cmd = raw_input("> ") # will only work with simple commands that have no arguments ctl.send(tnetstrings.dump([cmd, {}])) resp = ctl.recv() pprint(tnetstrings.parse(resp)) ctl.close()
def request(self, name, **args): msg = tnetstrings.dump([name, args]) self.sock.send(msg) rep = self.sock.recv() result, remain = tnetstrings.parse(rep) return result
if req.is_disconnect(): print "DISCONNECT" continue if req.headers.get("killme", None): print "They want to be killed." response = "" else: response = "<pre>\nSENDER: %r\nIDENT:%r\nPATH: %r\nHEADERS:%r\nBODY:%r</pre>" % ( req.sender, req.conn_id, req.path, json.dumps(req.headers), req.body) print response f=open('hello.txt','r') f.seek(0,2) length=f.tell() payload = {'code': 200, 'status': 'OK', 'body': ''} headers={} headers['Content-Length'] = length payload['headers'] = "\r\n".join('%s: %s' % (k,v) for k,v in headers.items()) conn.reply(req, handler.HTTP_FORMAT % payload) conn.deliver(req.sender, ['X',req.conn_id ], tnetstrings.dump(['sendfile','examples/http_0mq/hello.txt'])) if req.should_close(): conn.reply(req,'')
print "REQ received" if req.is_disconnect(): print "DISCONNECT" continue if req.headers.get("killme", None): print "They want to be killed." response = "" else: response = "<pre>\nSENDER: %r\nIDENT:%r\nPATH: %r\nHEADERS:%r\nBODY:%r</pre>" % ( req.sender, req.conn_id, req.path, json.dumps( req.headers), req.body) print response f = open('hello.txt', 'r') f.seek(0, 2) length = f.tell() payload = {'code': 200, 'status': 'OK', 'body': ''} headers = {} headers['Content-Length'] = length payload['headers'] = "\r\n".join('%s: %s' % (k, v) for k, v in headers.items()) conn.reply(req, handler.HTTP_FORMAT % payload) conn.deliver(req.sender, ['X', req.conn_id], tnetstrings.dump(['sendfile', 'examples/http_0mq/hello.txt'])) if req.should_close(): conn.reply(req, '')