def test_update_interval(self, hset, modify_job): event = timing_event.TimingEvent(timing_id="garbage", txn_type="banana", execution_order="serial", seconds=1) event.update(seconds=3, txn_type="banana", execution_order="serial") hset.assert_called_with( "scheduler:params", "garbage", '{"cron":null,"seconds":3,"contract_id":"garbage","execution_order":"serial","txn_type":"banana"}' )
def test_start_raises_conflict_when_add_job_conflicts(self, a, b): contract_id = "flim-flam" seconds = 3 event = timing_event.TimingEvent(timing_id=contract_id, seconds=seconds) try: event.start() self.fail("no error thrown") except Exception as e: self.assertEqual(str(e), "CONFLICT")
def test_add_job_called_for_cron(self, from_crontab, scheduler_add_jobstore, scheduler_start, scheduler_add_job, hset): contract_id = "flim-flam" cron = "* * * * *" func = ANY event = timing_event.TimingEvent(timing_id=contract_id, cron=cron) event.start() scheduler_add_job.assert_called_once_with(func, id=contract_id, max_instances=1, trigger="whatever")
def test_add_job_called_for_interval(self, scheduler_add_jobstore, scheduler_start, scheduler_add_job, hset): contract_id = "flim-flam" seconds = 3 event = timing_event.TimingEvent(timing_id=contract_id, seconds=seconds) event.start() scheduler_add_job.assert_called_once_with(ANY, trigger=ANY, id=contract_id, max_instances=1)
def test_update_cron(self, hset, modify_job): event = timing_event.TimingEvent(timing_id="garbage", txn_type="banana", execution_order="serial", seconds=3) event.update(cron="* * * * 1", txn_type="banana", execution_order="serial") hset.assert_called_with( "scheduler:params", "garbage", '{"cron":"* * * * 1","seconds":null,"contract_id":"garbage","execution_order":"serial","txn_type":"banana"}', ) modify_job.assert_called_with("garbage", None, next_run_time=ANY, trigger=ANY)
def worker(change_request: dict) -> None: """Process incoming change requests Args: change_request: dict<ChangeRequest> {contract_id: string, action: SchedulerActions enum, seconds?:int, cron?: string} """ _log.debug(f"Change Request: {change_request}") contract_id = change_request["contract_id"] action = change_request["action"] seconds = change_request.get("seconds") cron = change_request.get("cron") txn_type = change_request["txn_type"] execution_order = change_request["execution_order"] # Delete jobs if action == "delete": if not timing_event.exists(contract_id): raise exceptions.TimingEventSchedulerError("NOT_FOUND") event = timing_event.get_by_id(contract_id) event.delete() # Update job if action == "update": if not timing_event.exists(contract_id): raise exceptions.TimingEventSchedulerError("NOT_FOUND") event = timing_event.get_by_id(contract_id) event.update(cron=cron, seconds=seconds, execution_order=execution_order, txn_type=txn_type) # Create new job if action == "create": event = timing_event.TimingEvent(cron=cron, seconds=seconds, timing_id=contract_id, execution_order=execution_order, txn_type=txn_type) event.start() _log.debug(f"Successful {action} on job '{contract_id}'")
def test_update_raises(self, hset, modify_job): event = timing_event.TimingEvent(timing_id="garbage", seconds=1) try: event.update() except Exception as e: self.assertEqual(str(e), "BAD_REQUEST")
def test_delete(self, hdel, remove_job): event = timing_event.TimingEvent(timing_id="garbage", seconds=1) event.delete() hdel.assert_called_with("scheduler:params", "garbage") remove_job.assert_called_with("garbage")
def test_returns_instance(self): event = timing_event.TimingEvent(timing_id="garbage") self.assertIsInstance(event, timing_event.TimingEvent)