def test_get_active_tasks_retry_limit(self): """ Only tasks that are below their retry limits should be returned by getActiveTasks """ engine = create_engine('sqlite:///db/test.db', echo=False) tasks = TaskLogic(db_engine=engine) tasks.deleteAllTasks() data_past = { "scheduled_time": datetime.utcnow() - timedelta(hours=1), "endpoint_url": "http://asdfasdfasdfasd/asdfasdfwqerqwerzcxvzxcv", "endpoint_headers": None, "endpoint_body": "Test Body", "endpoint_method": "POST", "max_retry_count": 3 } task_uuid = tasks.createTask( scheduled_time=data_past["scheduled_time"], endpoint_url=data_past["endpoint_url"], endpoint_headers=data_past["endpoint_headers"], endpoint_body=data_past["endpoint_body"], endpoint_method=data_past["endpoint_method"], max_retry_count=data_past["max_retry_count"]) tasks.createTask( scheduled_time=data_past["scheduled_time"], endpoint_url=data_past["endpoint_url"], endpoint_headers=data_past["endpoint_headers"], endpoint_body=data_past["endpoint_body"], endpoint_method=data_past["endpoint_method"], max_retry_count=data_past["max_retry_count"]) # First attempt task1 = tasks.getTaskByUUID(task_uuid) with self.assertRaises(SchedulerHTTPException): tasks.callTaskHTTPEndpoint(task1) active_tasks1 = tasks.getActiveTasks() self.assertEqual(len(active_tasks1), 2) # Second attempt task2 = tasks.getTaskByUUID(task_uuid) with self.assertRaises(SchedulerHTTPException): tasks.callTaskHTTPEndpoint(task2) active_tasks2 = tasks.getActiveTasks() self.assertEqual(len(active_tasks2), 2) # Third attempt MUST NOT return the failing task anymore task3 = tasks.getTaskByUUID(task_uuid) with self.assertRaises(SchedulerHTTPException): tasks.callTaskHTTPEndpoint(task3) active_tasks3 = tasks.getActiveTasks() self.assertEqual(len(active_tasks3), 1) # Verify that task's is_failed flag is True after max attempt failed_task = tasks.getTaskByUUID(task_uuid) self.assertTrue(failed_task.is_failed)
def test_get_active_tasks_no_is_sent(self): """ Tasks with the is_sent flag should not be included in getActiveTasks results """ engine = create_engine('sqlite:///db/test.db', echo=False) tasks = TaskLogic(db_engine=engine) data_past = { "scheduled_time": datetime.utcnow() - timedelta(hours=1), "endpoint_url": "http://example.com", "endpoint_headers": None, "endpoint_body": "Test Body", "endpoint_method": "POST", "max_retry_count": 5 } task_uuid = tasks.createTask( scheduled_time=data_past["scheduled_time"], endpoint_url=data_past["endpoint_url"], endpoint_headers=data_past["endpoint_headers"], endpoint_body=data_past["endpoint_body"], endpoint_method=data_past["endpoint_method"], max_retry_count=data_past["max_retry_count"]) task_uuid2 = tasks.createTask( scheduled_time=data_past["scheduled_time"], endpoint_url=data_past["endpoint_url"], endpoint_headers=data_past["endpoint_headers"], endpoint_body=data_past["endpoint_body"], endpoint_method=data_past["endpoint_method"], max_retry_count=data_past["max_retry_count"]) active_tasks = tasks.getActiveTasks() self.assertEqual(len(active_tasks), 2) # Send first task task1 = tasks.getTaskByUUID(task_uuid) tasks.callTaskHTTPEndpoint(task1) active_tasks_after_call = tasks.getActiveTasks() self.assertEqual(len(active_tasks_after_call), 1) self.assertEqual(active_tasks_after_call[0].uuid, task_uuid2) # Send second task task2 = tasks.getTaskByUUID(task_uuid2) tasks.callTaskHTTPEndpoint(task2) active_tasks_after_call2 = tasks.getActiveTasks() self.assertEqual(len(active_tasks_after_call2), 0) tasks.deleteAllTasks()
def test_get_active_tasks_with_limit(self): """ getActiveTasks must follow limit if it is set """ engine = create_engine('sqlite:///db/test.db', echo=False) tasks = TaskLogic(db_engine=engine) data_past = { "scheduled_time": datetime.utcnow() - timedelta(days=1), "endpoint_url": "http://example.com", "endpoint_headers": None, "endpoint_body": "Test Body", "endpoint_method": "POST", "max_retry_count": 5 } data_future = { "scheduled_time": datetime.utcnow() + timedelta(days=1), "endpoint_url": "http://example.com", "endpoint_headers": None, "endpoint_body": "Test Body", "endpoint_method": "POST", "max_retry_count": 5 } # Clear DB for good measure tasks.deleteAllTasks() # Insert 8 tasks set in the past for x in xrange(8): tasks.createTask( scheduled_time=data_past["scheduled_time"], endpoint_url=data_past["endpoint_url"], endpoint_headers=data_past["endpoint_headers"], endpoint_body=data_past["endpoint_body"], endpoint_method=data_past["endpoint_method"], max_retry_count=data_past["max_retry_count"]) # Insert 4 tasks set in the future for x in xrange(4): tasks.createTask( scheduled_time=data_future["scheduled_time"], endpoint_url=data_future["endpoint_url"], endpoint_headers=data_future["endpoint_headers"], endpoint_body=data_future["endpoint_body"], endpoint_method=data_future["endpoint_method"], max_retry_count=data_future["max_retry_count"]) active_tasks = tasks.getActiveTasks(limit=5) self.assertEqual(len(active_tasks), 5) tasks.deleteAllTasks()
def test_get_active_tasks_empty(self): """ getActiveTasks must return an empty list if no task is currently active """ engine = create_engine('sqlite:///db/test.db', echo=False) tasks = TaskLogic(db_engine=engine) tasks.deleteAllTasks() active_tasks = tasks.getActiveTasks() self.assertEqual(len(active_tasks), 0) self.assertIsInstance(active_tasks, list)
class SchedHTTPService(Daemon): def setupDaemon(self, config, logger): self.config = config self.logger = logger self.db_engine = create_engine( self.config.DATABASE_URI, poolclass=self.config.SQLALCHEMY_POOLCLASS, pool_size=20, max_overflow=0, pool_recycle=3600, # Recycle connections every 1 hr echo=self.config.SQLALCHEMY_ECHO) self.tasks = TaskLogic(db_engine=self.db_engine) return True def start(self): logging.info("Sched HTTP Service Started") Daemon.start(self) def stop(self): logging.info("Sched HTTP Service Terminated") Daemon.stop(self) def run(self): while True: active_tasks = self.tasks.getActiveTasks() self.logger.info("Active Tasks: %s: %s" % (len(active_tasks), str(active_tasks))) for task in active_tasks: self.logger.info("Task: %s", task.uuid) self.logger.info("Retries: %s", task.retry_count) try: self.tasks.callTaskHTTPEndpoint(task) self.logger.info("Sent! %s: %s" % (task.uuid, task.endpoint_url)) except SchedulerHTTPException, e: self.logger.exception("Error: %s", e.value) except: self.logger.exception("Error in calling task!")