def __init__(self, endpoint=_ENDPOINT, retries=3): if endpoint.startswith('ipc'): register_ipc_file(endpoint) self.started = False self.endpoint = endpoint self.workers = Workers() self.registration = WorkerRegistration(self.workers, self.endpoint) self.retries = retries
class JobRunner(object): def __init__(self, endpoint=_ENDPOINT, retries=3): self.started = False self.endpoint = endpoint self.workers = Workers() self.registration = WorkerRegistration(self.workers, self.endpoint) self.retries = retries def start(self): if self.started: return logger.debug('Starting registration at ' + self.endpoint) self.registration.start() self.started = True def stop(self): if not self.started: return logger.debug('Stopping registration at ' + self.endpoint) self.registration.stop() self.started = False def execute(self, job_id, job_data, timeout=1.): from powerhose import logger e = None for i in range(self.retries): try: return self._execute(job_id, job_data, timeout) except (TimeoutError, ExecutionError), e: logger.debug(str(e)) logger.debug('retrying - %d' % (i + 1)) if e is not None: raise e
def __init__(self, endpoint=_ENDPOINT, retries=3): self.started = False self.endpoint = endpoint self.workers = Workers() self.registration = WorkerRegistration(self.workers, self.endpoint) self.retries = retries
class JobRunner(object): """Class that sends jobs to workers. JobRunner does two things: 1. runs a :class:`WorkerRegistration` instance which is responsible for the registration of workers. 2. offers a method to execute jobs. Options: - **endpoint**: The ZMQ endpoint for workers registration. (default: ipc://master-routing.ipc) - **retries**: The number of retries when a job fails. (default: 3) """ def __init__(self, endpoint=_ENDPOINT, retries=3): if endpoint.startswith('ipc'): register_ipc_file(endpoint) self.started = False self.endpoint = endpoint self.workers = Workers() self.registration = WorkerRegistration(self.workers, self.endpoint) self.retries = retries def start(self): """Starts the registration loop. """ if self.started: return logger.debug('Starting registration at ' + self.endpoint) self.registration.start() self.started = True def stop(self): """Stops the registration loop. """ if not self.started: return logger.debug('Stopping registration at ' + self.endpoint) self.registration.stop() self.started = False def execute(self, job, timeout=1.): """Execute a job and return the result. Options: - **job**: a :class:`Job` instance. - **timeout**: the maximum allowed time in seconds. (default: 1) If the job fails to run, this method may raise one of these exceptions: - :class:`TimeoutError`: timed out. - :class:`ExecutionError`: the worker has failed. In case of an execution error, the exception usually holds more details on the failure. """ from powerhose import logger e = None for i in range(self.retries): try: return self._execute(job, timeout) except (TimeoutError, ExecutionError), e: logger.debug(str(e)) logger.debug('retrying - %d' % (i + 1)) if e is not None: raise e