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_call_task_http_endpoint_fail(self): """ On failed call to HTTP Endpoint, the service must increment retry_count on db the last_retry_date field should also be updated This test tests the setTaskSendAttemptAsFail function via actual HTTP call fails """ 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://localhost/qwerpoiuasddfjasld", "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=None, endpoint_body=data_past["endpoint_body"], endpoint_method=data_past["endpoint_method"], max_retry_count=data_past["max_retry_count"]) task = tasks.getTaskByUUID(task_uuid) print task.uuid print task.endpoint_url print str(task.endpoint_headers) with self.assertRaises(SchedulerHTTPException): tasks.callTaskHTTPEndpoint(task) task_after_call = tasks.getTaskByUUID(task_uuid) self.assertFalse(task_after_call.is_sent) self.assertIsNone(task_after_call.sent_date) self.assertEqual(task_after_call.retry_count, 1) self.assertIsInstance(task_after_call.last_retry_date, datetime) tasks.deleteAllTasks()
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!")
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_call_task_http_endpoint(self): 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": { "Content-Length": 0 }, "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 = tasks.getTaskByUUID(task_uuid) self.assertFalse(task.is_sent) self.assertFalse(task.is_failed) self.assertIsNone(task.sent_date) self.assertEqual(task.retry_count, 0) results = tasks.callTaskHTTPEndpoint(task) self.assertTrue(results) task_after_call = tasks.getTaskByUUID(task_uuid) self.assertTrue(task_after_call.is_sent) self.assertIsInstance(task_after_call.sent_date, datetime) tasks.deleteAllTasks()