def start_fetching(): scheduler = Scheduler(connection=get_connection()) from app.tasks import fetch_data scheduler.schedule(scheduled_time=datetime.utcnow(), func=fetch_data, interval=int(60), repeat=None)
def job_result(id): """ 通过任务id来返回结果 """ conn = get_connection() result = conn.hget(id, 'result') if result: return rq_loads(result) return "Working ..."
def get_current_job(): rq_job = rq.get_current_job(connection=get_connection()) return Job.query.get(rq_job.id)
def test_connection_from_url(self): connection = get_connection('high') connection_kwargs = connection.connection_pool.connection_kwargs self.assertEqual(connection_kwargs.get('host'), 'localhost') self.assertEqual(connection_kwargs.get('port'), 6379) self.assertEqual(connection_kwargs.get('db'), 3)
def test_get_connection_default(self): connection = get_connection() connection_kwargs = connection.connection_pool.connection_kwargs self.assertEqual(connection_kwargs.get('host'), 'localhost') self.assertEqual(connection_kwargs.get('port'), 6379)
def get_job_id(): rq_job = rq.get_current_job(connection=get_connection()) return rq_job.id
def run_scheduler(): """rq-scheduler for periodic tasks""" if app.debug: setup_loghandlers('DEBUG') scheduler = Scheduler(connection=get_connection(), interval=30) scheduler.run()
def run_worker(): """Initializes a rq task queue.""" listen = ['default'] with Connection(get_connection()): worker = Worker(map(Queue, listen)) worker.work(with_scheduler=False)
def stop_fetching(): scheduler = Scheduler(connection=get_connection()) for j in scheduler.get_jobs(): scheduler.cancel(j)