Skip to content

Instantly share code, notes, and snippets.

@AndreLobato
Last active August 29, 2015 14:25
Show Gist options
  • Save AndreLobato/4ecbf9556f86e6b3bdec to your computer and use it in GitHub Desktop.
Save AndreLobato/4ecbf9556f86e6b3bdec to your computer and use it in GitHub Desktop.
import logging
import StringIO
import traceback
import time
class NapWrapper(object):
''' This is the minumum base wrapper interface to comunicate directly with the scheduler. The things you have to do is up to you in each method. You may consider to create a wrapper for long running tasks, if you like to have progress control.
'''
def __init__(self, sleep, pipe=None, logger=logging,**kwargs):
self.pipe = pipe
self.logger = logger
self.sleep = sleep
# The rest of your init here
def set_cycle(self, cycle_dt):
'''(Optional) This function will be called by the scheduler passing a datetime object in UTC representing the current cycle or current run time. That's how the system tells to your process in which cycle to run.
'''
self.cycle = cycle_dt
def set_progress(self, partial, total, message):
'''(Optional) Just for long runing (+10min) tasks that you may consider to use this function, you may call it whenever you want to comunicate progess.
Must send trough the pipe 2 numbers and a string in a tuple,
'''
if hasattr(self.pipe, 'send'):
self.pipe.send((partial, total, message))
def run(self):
''' Executes the process, you can return a result or send the traceback on the pipe. The result can be used as arg for a suposed linked action to be executed after this. If an error occours you may send the traceback back to scheduler using the pipe, so can be visualized in the web interface or in the error email.
'''
try:
self.logger.info('Started sleep task at cycle %s' % self.cycle)
for i in range(self.sleep):
time.sleep(1)
self.set_progress(i, self.sleep, 'sleeping for %d' % self.sleep-i )
return 'Took nap of %d seconds' % self.sleep
except Exception as exc:
tb_buff = StringIO.StringIO()
traceback.print_exc(file=tb_buff)
tb = tb_buff.getvalue()
if hasattr(self.pipe, 'send'):
self.pipe.send((tb))
raise exc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment