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 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_new_task_manager():
    # should be empty
    tm = TaskManager(LOGGER)
    assert len(tm._todo_queue) == 0
    assert len(tm._in_process) == 0
    assert len(tm._done) == 0
    # next task to do should be None
    task, attempt = tm.start_next_attempt("runner", datetime.now())
    assert task is None
    assert attempt is None
def test_add_same_task_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

    tm.add_task(t1)
    assert len(tm._todo_queue) == 1
    assert len(tm._in_process) == 0
    assert len(tm._done) == 0
示例#5
0
        'status': 400,
    },
}

app = Flask(__name__)
Bootstrap(app)
api = Api(app, catch_all_404s=True, errors=errors)

task_id_creator = TaskIDCreator()

log_file_name = util.time_stamped_file_name("stq")
logger = util.basic_logger(log_file_name,
                           file_level=logging.DEBUG,
                           console_level=logging.DEBUG)

task_manager = TaskManager(logger)

task_post_parser = reqparse.RequestParser()
task_post_parser.add_argument('command',
                              dest='command',
                              required=True,
                              help="what gets executed in the command line")
task_post_parser.add_argument(
    'name',
    dest='name',
    required=False,
    help='a brief, human readable name for the task (optional)')
task_post_parser.add_argument(
    'description',
    dest='description',
    required=False,
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_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