# return '/usr/sbin/haproxy -f frontend.cfg -f local.cfg', {} def signaled(self, js, process=None): # # - this pod can be switched to draining mode when being signaled # - the configuration file will be re-written using an alternate template # - the pod must then be restarted for the change to take effect # - the input YAML payload should be a comma separated list of urls, for instance : # # urls: # - 127.345.24.100:9000 # - 138.12.123.56:9000 # urls = js['urls'].split(',') logger.info('rendering draining.cfg (%s)' % js['urls']) env = Environment( loader=FileSystemLoader(join(dirname(__file__), 'templates'))) mappings = \ { 'default': self.hints['namespace'], 'listeners': {'listener-%d' % index: endpoint for index, endpoint in enumerate(urls)} } template = env.get_template('draining.cfg') with open('%s/frontend.cfg' % self.cwd, 'w') as f: f.write(template.render(mappings)) Pod().boot(Strategy, model=Model)
if __name__ == '__main__': class Strategy(Piped): cwd = '/opt/flask' checks = 5 check_every = 1 metrics = True def sanity_check(self, pid): # # - Randomly decide to be stressed # - Curl to the Flask in subprocess to check number of threaded requests running. # @retry(timeout=30.0, pause=0) def _self_curl(): reply = get('http://localhost:9000/threads') code = reply.status_code assert code == 200 or code == 201, 'Self curling failed' return merge({'stressed': choice(['Very', 'Nope'])}, json.loads(reply.text)) return _self_curl() def configure(self, _): return 'python -u webserver.py', {} Pod().boot(Strategy)
# # - you can override the default settings, for instance here to tell ochopod what configure() # returns should actually be treated as a shell command # shell = True # # - you can also pipe the sub-process stderr/out and include them in the pod log # pipe_subprocess = True def configure(self, _): # # - simply return what you wish to run (a simple shell statement in our case) # - deriving from Piped means the SDK will use popen() to fork an ancillary process # - you can also set optional environment variables on that process (presently $LAPSE) # - note that whatever value you pass will be turned into a string (e.g you can use numbers) # return "sleep $LAPSE && echo 'hello world' && exit 0", {'LAPSE': 5} # # - that's it, just boot the SDK with your process strategy # - the local=1 means we force everything to be looked up on localhost # - if you run this script locally you should see 'hello world' printed on stdout after 5 seconds # - since we exit with 0 the pod will automatically be finalized (e.g shutdown gracefully) # - simply type CTRL-C to exit # Pod().boot(Strategy, local=1)