def setup_rq_connection(): redis_conn = get_current_connection() if redis_conn is None: opts = constants.REDIS_SETTINGS.get('connection') logger.debug('Establishing Redis connection to DB %(db)s at %(host)s:%(port)s' % opts) redis_conn = Redis(**opts) push_connection(redis_conn)
def setup_rq_connection(): redis_conn = get_current_connection() if redis_conn == None: opts = OPTIONS.get('connection') logger.debug('Establishing Redis connection to DB %(db)s at %(host)s:%(port)s' % opts) redis_conn = Redis(**opts) push_connection(redis_conn)
def _connect(): u''' Connect to Redis and tell RQ about it. Workaround for https://github.com/nvie/rq/issues/479. ''' conn = connect_to_redis() push_connection(conn) return conn
def perform_job(self, job, queue, heartbeat_ttl=None): """Performs the actual work of a job. Will/should only be called inside the work horse's process. """ self.prepare_job_execution(job, heartbeat_ttl) push_connection(self.connection) started_job_registry = StartedJobRegistry(job.origin, self.connection, job_class=self.job_class) try: job.started_at = utcnow() timeout = job.timeout or self.queue_class.DEFAULT_TIMEOUT with self.death_penalty_class(timeout, JobTimeoutException, job_id=job.id): rv = job.perform(self.workerKwargs) job.ended_at = utcnow() # Pickle the result in the same try-except block since we need # to use the same exc handling when pickling fails job._result = rv self.handle_job_success(job=job, queue=queue, started_job_registry=started_job_registry) except: job.ended_at = utcnow() self.handle_job_failure(job=job, started_job_registry=started_job_registry) self.handle_exception(job, *sys.exc_info()) return False finally: pop_connection() self.log.info('{0}: {1} ({2})'.format( green(job.origin), blue('Job OK'), job.id)) if rv is not None: log_result = "{0!r}".format(as_text(text_type(rv))) self.log.debug('Result: %s', yellow(log_result)) if self.log_result_lifespan: result_ttl = job.get_result_ttl(self.default_result_ttl) if result_ttl == 0: self.log.info('Result discarded immediately') elif result_ttl > 0: self.log.info( 'Result is kept for {0} seconds'.format(result_ttl)) else: self.log.warning( 'Result will never expire, clean up result key manually') return True
def test_decorator_accepts_queue_name_as_argument(self): """Ensure that passing in queue name to the decorator puts the job in the right queue. """ push_connection(self.conn) @job(queue='queue_name') def hello(): return 'Hi' result = hello.delay() self.assertEqual(result.origin, 'queue_name')
def test_decorator_adds_delay_attr(self): """Ensure that decorator adds a delay attribute to function that returns a Job instance when called. """ push_connection(self.conn) self.assertTrue(hasattr(decorated_job, 'delay')) result = decorated_job.delay(1, 2) self.assertTrue(isinstance(result, Job)) # Ensure that job returns the right result when performed self.assertEqual(result.perform(), 3)
def test_decorator_accepts_ttl_as_argument(self): """Ensure that passing in ttl to the decorator sets the ttl on the job """ push_connection(self.conn) # Ensure default result = decorated_job.delay(1, 2) self.assertEqual(result.ttl, None) @job('default', ttl=30) def hello(): return 'Hello' result = hello.delay() self.assertEqual(result.ttl, 30)
def test_decorator_accepts_result_ttl_as_argument(self): """Ensure that passing in result_ttl to the decorator sets the result_ttl on the job """ # Ensure default push_connection(self.conn) result = decorated_job.delay(1, 2) self.assertEqual(result.result_ttl, DEFAULT_RESULT_TTL) @job('default', result_ttl=10) def hello(): return 'Why hello' result = hello.delay() self.assertEqual(result.result_ttl, 10)
def test_decorator_accepts_result_depends_on_as_argument(self): """Ensure that passing in depends_on to the decorator sets the correct dependency on the job """ push_connection(self.conn) @job(queue='queue_name') def foo(): return 'Firstly' @job(queue='queue_name') def bar(): return 'Secondly' foo_job = foo.delay() bar_job = bar.delay(depends_on=foo_job) self.assertFalse(foo_job._parent_ids) self.assertEqual(bar_job._parent_ids, [foo_job.id])
def push_rq_connection(): new_instance_index = None if request.method == 'GET': new_instance_index = request.args.get('redis_instance_index') if request.method == 'POST': new_instance_index = request.form.get('redis_instance_index') if new_instance_index is None: new_instance_index = request.view_args.get('redis_instance_index') #print(new_instance_index, type(new_instance_index)) if new_instance_index is not None: redis_url = current_app.config.get("RQ_MONITOR_REDIS_URL") new_instance_index = int(new_instance_index) if int(new_instance_index) < len(redis_url): new_instance = create_redis_connection( redis_url[new_instance_index]) else: raise RQMonitorException("Invalid redis instance index!", status_code=400) else: new_instance = current_app.redis_connection push_connection(new_instance) current_app.redis_connection = new_instance
def init_app(app): app.before_request(lambda: push_connection(rq_redis_connection)) app.teardown_request(lambda _: pop_connection())
def tearDown(self): super(TestRQWorkerScript, self).tearDown() connections.pop_connection() # Clean up after setup_redis() which clears the active connection if self.current_connection: connections.push_connection(self.current_connection)
def test_connection_detection(self): """Automatic detection of the connection.""" push_connection(self.conn) q = Queue() self.assertEqual(q.connection, self.conn)