def basic_simple_task_queue(): time_stamp = datetime.now() tq = SimpleTaskQueue(LOGGER) t1 = Task(1, "run command example", time_stamp, name="example run", desc="this is a bologna command that does nothing") tq.add_task(t1) t2 = Task( 2, "python -m some_script", time_stamp, name= "example python run that will only try to run once and should last 3 minutes", duration=3 * 60, max_attempts=1) tq.add_task(t2) t3 = Task(3, "cd my_directory; python -m some_script", time_stamp, name="multiple commands", desc="an example of multiple commands in one task") tq.add_task(t3) return tq
def basic_task_manager(): time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=5, microsecond=100222) tq = SimpleTaskQueue(LOGGER) t1 = Task(1, "run command example", time_stamp, name="example run", desc="this is a bologna command that does nothing") tq.add_task(t1) t2 = Task( 2, "python -m some_script", time_stamp, name= "example python run that will only try to run once and should last 3 minutes" ) tq.add_task(t2) t3 = Task(3, "cd my_directory; python -m some_script", time_stamp, name="multiple commands", desc="an example of multiple commands in one task") tq.add_task(t3) tm = TaskManager(LOGGER) tm._todo_queue = tq return tm
def test_task_to_retry_first_started_retry(): time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=5, microsecond=100222) t1 = Task(1, "run command example", time_stamp, name="example run", desc="this is a bologna command that does nothing", duration=100, max_attempts=3) start_time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=6, microsecond=100222) t1.attempt_task("runner", start_time_stamp) t2 = Task(2, "run command example 2", time_stamp, name="example run 2", desc="this is a bologna command that does nothing", duration=820, max_attempts=3) start_time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=6, microsecond=100222) t2.attempt_task("runner", start_time_stamp) ot = OpenTasks(LOGGER) ot.add_task(t1) ot.add_task(t2) current_time = datetime(year=2018, month=8, day=13, hour=5, minute=12, second=8, microsecond=100222) task, failed_tasks = ot.task_to_retry(current_time) # should be no failed tasks assert task.task_id() == t1.task_id() assert task == t1 assert len(failed_tasks) == 0
def test_remove_task_that_doesnt_exist(): time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=5, microsecond=100222) t1 = Task(1, "run command example", time_stamp, name="example run", desc="this is a bologna command that does nothing", duration=500, max_attempts=3) start_time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=6, microsecond=100222) t1.attempt_task("runner", start_time_stamp) t2 = Task(2, "run command example 2", time_stamp, name="example run 2", desc="this is a bologna command that does nothing", duration=90, max_attempts=3) start_time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=8, microsecond=100222) t2.attempt_task("runner", start_time_stamp) ot = OpenTasks(LOGGER) ot.add_task(t1) ot.add_task(t2) assert len(ot) == 2 assert ot.get_task(1) == t1 assert ot.get_task(2) == t2 # remove non-existent task ot.remove_task("id that doesn't exist") assert len(ot) == 2 assert ot.get_task(1) == t1 assert ot.get_task(2) == t2
def test_all_tasks_added_in_reverse_order(): # same as test_all_tasks but the creation times are reversed time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=5, microsecond=100222) t1 = Task(1, "run command example", time_stamp, name="example run", desc="this is a bologna command that does nothing", duration=500, max_attempts=3) start_time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=8, microsecond=100222) t1.attempt_task("runner", start_time_stamp) t2 = Task(2, "run command example 2", time_stamp, name="example run 2", desc="this is a bologna command that does nothing", duration=90, max_attempts=3) start_time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=6, microsecond=100222) t2.attempt_task("runner", start_time_stamp) ot = OpenTasks(LOGGER) ot.add_task(t1) ot.add_task(t2) # should be sorted by creation time tasks = ot.all_tasks() assert len(tasks) == 2 assert tasks[0] == t1 assert tasks[1] == t2
def test_mark_completed_when_attempt_unknown(): time_stamp1 = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=5, microsecond=100222) tq = SimpleTaskQueue(LOGGER) t1 = Task(1, "run command example", time_stamp1, name="example run", desc="this is a bologna command that does nothing", duration=100, max_attempts=2) tq.add_task(t1) tm = TaskManager(LOGGER) tm._todo_queue = tq completed_time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=44, microsecond=100222) assert tm.complete_attempt(t1.task_id(), "some_random_unknown_attempt_id", completed_time_stamp) is False
def test_add_already_added_task(): time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=5, microsecond=100222) t1 = Task(1, "run command example", time_stamp, name="example run", desc="this is a bologna command that does nothing", duration=500, max_attempts=3) start_time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=6, microsecond=100222) t1.attempt_task("runner", start_time_stamp) ot = OpenTasks(LOGGER) assert len(ot) == 0 ot.add_task(t1) # should be there now assert len(ot) == 1 assert ot.get_task(1) == t1 ot.add_task(t1) # should be there now assert len(ot) == 1 assert ot.get_task(1) == t1
def test_attempt_task(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) # no attempts assert task.num_attempts() == 0 assert task.most_recent_attempt() is None attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt = task.attempt_task("runner 1", attempt_time) assert task.num_attempts() == 1 assert task.most_recent_attempt() == attempt attempt_time_2 = datetime.datetime(2018, 1, 15, 12, 35, 45) attempt_2 = task.attempt_task("runner 2", attempt_time_2) assert task.num_attempts() == 2 assert task.most_recent_attempt() == attempt_2 # now get another attempt (which will be greater than max_attempts so should fail. attempt_time_3 = datetime.datetime(2018, 1, 15, 12, 36, 10) attempt_3 = task.attempt_task("runner 3", attempt_time_3) assert attempt_3 is None assert task.num_attempts() == 2 assert task.most_recent_attempt() == attempt_2
def test_open_time_open_attempt(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) task.attempt_task("runner 1", attempt_time) assert task.open_time() is None
def test_is_completed_open_attempt(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) task.attempt_task("runner 1", attempt_time) assert task.is_completed() is False
def test_is_in_process_open_attempt(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) task.attempt_task("runner 1", attempt_time) assert task.is_in_process() is True
def test_completed_time_one_completed_attempt(): task_created_time = datetime.datetime(2018, 1, 15, 12, 35, 0) task = Task("1234", "some cmd", task_created_time, max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt = task.attempt_task("runner 1", attempt_time) attempt_completed_time = datetime.datetime(2018, 1, 15, 12, 35, 45) attempt.mark_completed(attempt_completed_time) assert task.completed_time() == attempt_completed_time
def test_move_task_to_done_when_not_in_process(): time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=5, microsecond=100222) tq = SimpleTaskQueue(LOGGER) t1 = Task(1, "run command example", time_stamp, name="example run", desc="this is a bologna command that does nothing") tq.add_task(t1) t2 = Task( 2, "python -m some_script", time_stamp, name= "example python run that will only try to run once and should last 3 minutes" ) tq.add_task(t2) t3 = Task(3, "cd my_directory; python -m some_script", time_stamp, name="multiple commands", desc="an example of multiple commands in one task") tq.add_task(t3) tm = TaskManager(LOGGER) tm._todo_queue = tq assert len(tm._todo_queue) == 3 assert len(tm._in_process) == 0 assert len(tm._done) == 0 assert tm._find_task(1, todo=True) is not None assert tm._find_task(1, in_process=True) is None assert tm._find_task(1, done=True) is None tm._move_task_to_done(t1) assert len(tm._todo_queue) == 2 assert len(tm._in_process) == 0 assert len(tm._done) == 1 assert tm._find_task(1, todo=True) is None assert tm._find_task(1, in_process=True) is None assert tm._find_task(1, done=True) is not None
def test_is_completed_completed_attempt(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt = task.attempt_task("runner 1", attempt_time) attempt.mark_completed(datetime.datetime(2018, 1, 15, 12, 35, 45)) assert task.is_completed() is True
def test_is_in_process_one_fail(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt = task.attempt_task("runner 1", attempt_time) attempt.mark_failed(datetime.datetime(2018, 1, 15, 12, 35, 45)) assert task.is_in_process() is True
def test_open_time_one_attempt_failed(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt = task.attempt_task("runner 1", attempt_time) attempt.mark_failed(datetime.datetime(2018, 1, 15, 12, 35, 45)) assert task.open_time() is None
def test_started_time_with_completed_attempts(): task_created_time = datetime.datetime(2018, 1, 15, 12, 35, 0) task = Task("1234", "some cmd", task_created_time, max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt = task.attempt_task("runner 1", attempt_time) attempt_completed_time = datetime.datetime(2018, 1, 15, 12, 35, 45) attempt.mark_completed(attempt_completed_time) attempt_time_2 = datetime.datetime(2018, 1, 15, 12, 35, 45) attempt_2 = task.attempt_task("runner 2", attempt_time_2) attempt_2.mark_completed(datetime.datetime(2018, 1, 15, 12, 35, 48)) assert task.started_time() == attempt_time
def test_completed_time_all_failed_attempts(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt = task.attempt_task("runner 1", attempt_time) attempt.mark_failed(datetime.datetime(2018, 1, 15, 12, 35, 45)) attempt_time_2 = datetime.datetime(2018, 1, 15, 12, 35, 45) attempt_2 = task.attempt_task("runner 2", attempt_time_2) attempt_2.mark_failed(datetime.datetime(2018, 1, 15, 12, 35, 48)) assert task.completed_time() is None
def test_add_same_task_id_more_than_once(): tm = TaskManager(LOGGER) t1 = Task(1, "run command example", datetime.now(), name="example run", desc="this is a bologna command that does nothing") tm.add_task(t1) assert len(tm._todo_queue) == 1 assert len(tm._in_process) == 0 assert len(tm._done) == 0 t2 = Task(1, "another run command example", datetime.now(), name="example run", desc="this is a bologna command that does nothing") tm.add_task(t2) assert len(tm._todo_queue) == 1 assert len(tm._in_process) == 0 assert len(tm._done) == 0
def test_started_both_failed_attempts(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt = task.attempt_task("runner 1", attempt_time) attempt.mark_failed(datetime.datetime(2018, 1, 15, 12, 35, 45)) attempt_time_2 = datetime.datetime(2018, 1, 15, 12, 35, 45) attempt_2 = task.attempt_task("runner 2", attempt_time_2) attempt_2.mark_failed(datetime.datetime(2018, 1, 15, 12, 35, 48)) assert task.is_started() is True
def test_get_attempt(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt_1 = task.attempt_task("runner 1", attempt_time) attempt_time_2 = datetime.datetime(2018, 1, 15, 12, 35, 45) attempt_2 = task.attempt_task("runner 2", attempt_time_2) # no id should be None assert task.get_attempt("fake_attempt_id") is None assert attempt_1 == task.get_attempt(attempt_1._attempt_id) assert attempt_2 == task.get_attempt(attempt_2._attempt_id)
def post(self): args = task_post_parser.parse_args() self._logger.info("TaskManagement.post: %s" % str(args)) task = Task( task_id_creator.id(), args.command, datetime.now(), name=args.name if args.name is not None else "", desc=args.description if args.description is not None else "", duration=args.duration, max_attempts=args.max_attempts if args.max_attempts is not None else 1, dependent_on=args.dependent_on) task_manager.add_task(task) return task.to_json(), 201
def test_add_task_without_an_attempt(): time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=5, microsecond=100222) t1 = Task(1, "run command example", time_stamp, name="example run", desc="this is a bologna command that does nothing", duration=500, max_attempts=3) ot = OpenTasks(LOGGER) assert len(ot) == 0 with pytest.raises(AssertionError): ot.add_task(t1) # should be there now assert len(ot) == 0 assert ot.get_task(1) is None
def test_add_task(): tq = SimpleTaskQueue(LOGGER) t = Task(15, 'run command', datetime.now()) tq.add_task(t) assert len(tq) == 1 assert tq.task(15) == t
def test_started_new_task(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) assert task.is_started() is False
def test_open_time_new_task(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) assert task.open_time() is None
def test_started_time_open_attempt(): task_created_time = datetime.datetime(2018, 1, 15, 12, 35, 0) task = Task("1234", "some cmd", task_created_time, max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) task.attempt_task("runner 1", attempt_time) assert task.started_time() == attempt_time
def test_is_completed_no_attempts(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) assert task.is_completed() is False
def test_completed_time_new_task(): task_created_time = datetime.datetime(2018, 1, 15, 12, 35, 0) task = Task("1234", "some cmd", task_created_time, max_attempts=2) assert task.completed_time() is None
def test_mark_completed_when_done(): time_stamp1 = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=5, microsecond=100222) time_stamp2 = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=10, microsecond=0) time_stamp3 = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=15, microsecond=222222) tq = SimpleTaskQueue(LOGGER) t1 = Task(1, "run command example", time_stamp1, name="example run", desc="this is a bologna command that does nothing", duration=100, max_attempts=5) tq.add_task(t1) t2 = Task( 2, "python -m some_script", time_stamp2, name= "example python run that will only try to run once and should last 3 minutes", duration=180, max_attempts=1) tq.add_task(t2) t3 = Task(3, "cd my_directory; python -m some_script", time_stamp3, name="multiple commands", desc="an example of multiple commands in one task", duration=200) tq.add_task(t3) basic_task_manager = TaskManager(LOGGER) basic_task_manager._todo_queue = tq # make sure baseline is what we expect assert len(basic_task_manager._todo_queue) == 3 assert len(basic_task_manager._in_process) == 0 assert len(basic_task_manager._done) == 0 start_time = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=30, microsecond=500000) complete_time = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=40, microsecond=600222) # start one up and make sure as expected task, attempt = basic_task_manager.start_next_attempt("runner", start_time) assert task.task_id() == 1 assert len(basic_task_manager._todo_queue) == 2 assert len(basic_task_manager._in_process) == 1 assert len(basic_task_manager._done) == 0 # start a second attempt second_start_time = datetime(year=2018, month=8, day=13, hour=5, minute=13, second=30, microsecond=100222) task2, attempt2 = basic_task_manager.start_next_attempt( "runner2", second_start_time) assert task2.task_id() == 1 assert attempt2.id() != attempt.id() assert len(basic_task_manager._todo_queue) == 2 assert len(basic_task_manager._in_process) == 1 assert len(basic_task_manager._done) == 0 # mark completed basic_task_manager.complete_attempt(task.task_id(), attempt.id(), complete_time) assert len(basic_task_manager._todo_queue) == 2 assert len(basic_task_manager._in_process) == 0 assert len(basic_task_manager._done) == 1 assert task.is_completed() assert not task.is_in_process() assert not task.is_failed() assert task.open_time() == 35.5 # mark completed again second_complete_time = datetime(year=2018, month=8, day=13, hour=5, minute=10 + 3, second=40, microsecond=100222) basic_task_manager.complete_attempt(task2.task_id(), attempt2.id(), second_complete_time) # should have no impact; all should be the same assert len(basic_task_manager._todo_queue) == 2 assert len(basic_task_manager._in_process) == 0 assert len(basic_task_manager._done) == 1 assert task.is_completed() assert not task.is_in_process() assert not task.is_failed() assert task.open_time() == 35.5