def run(self): # If we have a current Job, return BUSY and go no further. if self.current_job: logger.debug("Worker %s is busy with %s (%s / %s)" % ( self.number, self.current_job.code_word, self.current_job.task.key, self.current_job.uid) ) return BUSY # ...otherwise, look for a Job to run. for g in self.task_groups: msg = '%r checking for work in group %r' % (self, g) # logger.debug(msg) name = namespace(g) task = REDIS.lpop(name) if task: logger.debug( 'RUNNING TASK on thread %r: %s' % (self, task) ) self.active_task_string = task break if self.active_task_string: d = threads.deferToThread(run_task, self.active_task_string, self) return d else: return IDLE
def test_task_is_in_queue(self): REDIS.flushall() task = Task() decorated = task(unblocker) # Pushes onto registry dict tm = ThreadManager(1, [("1")]) task_data_string = REDIS.lpop(resolve_group_namespace(1)) self.assertIsNone(task_data_string) # We haven't loaded the task into REDIS yet. decorated.soon() # Now it's in there. d = tm.control() # Run the next task in line, which we hope to be the above. def assert_that_unblocker_ran(n): unblocked_code = q.get() # The q will have pushed the random string. self.assertEqual(unblocked_code, random_string) d.addCallback(assert_that_unblocker_ran) return d
def subprocessLaunch(): """ This function is called by the hxw script. It takes no arguments, and returns an instance of HendrixDeploy """ if not redis_available: raise RedisException("can't launch this subprocess without tiempo/redis.") try: action='start' options = REDIS.get('worker_args') assignDeploymentInstance(action='start', options=options) except Exception, Argument: print Argument chalk.red('\n Could not %s hendrix.\n' % action, pipe=chalk.stderr)
def subprocessLaunch(): """ This function is called by the hxw script. It takes no arguments, and returns an instance of HendrixDeploy """ if not redis_available: raise RedisException("can't launch this subprocess without tiempo/redis.") try: action = 'start' options = REDIS.get('worker_args') assignDeploymentInstance(action='start', options=options) except Exception: chalk.red('\n Encountered an unhandled exception while trying to %s hendrix.\n' % action, pipe=chalk.stderr) raise
def run(): this_loop_runtime = utc_now() # This loop basically does two things: for runner in all_runners(): # 1) Let the runners pick up any queued tasks. result = runner.run() if not result in (BUSY, IDLE): # If the runner is neither busy nor idle, it will have returned a Deferred. result.addCallback(runner.finish_job) # 2) Queue up new tasks. for task_string, task in TIEMPO_REGISTRY.items(): ### REPLACE with task.next_expiration_dt() if hasattr(task, 'force_interval'): expire_key = this_loop_runtime + datetime.timedelta( seconds=task.force_interval ) else: expire_key = task_time_keys().get(task.get_schedule()) ######### if expire_key: stop_key_has_expired = REDIS.setnx(task.stop_key, 0) if stop_key_has_expired: REDIS.expire( task.stop_key, int(float((expire_key - this_loop_runtime).total_seconds())) - 1 ) # OK, we're ready to queue up a new job for this task! task.spawn_job()
def test_task_is_in_queue(self): REDIS.flushall() task = Task() decorated = task(unblocker) # Pushes onto registry dict tm = ThreadManager(1, [('1')]) task_data_string = REDIS.lpop(resolve_group_namespace(1)) self.assertIsNone( task_data_string) # We haven't loaded the task into REDIS yet. decorated.soon() # Now it's in there. d = tm.control( ) # Run the next task in line, which we hope to be the above. def assert_that_unblocker_ran(n): unblocked_code = q.get( ) # The q will have pushed the random string. self.assertEqual(unblocked_code, random_string) d.addCallback(assert_that_unblocker_ran) return d