示例#1
0
def test_unique_workflow(dbsession):
    workflow1 = Workflow(name='workflow1', active=True)
    dbsession.add(workflow1)
    dbsession.commit()

    workflow_instance = WorkflowInstance(
        workflow_name='workflow1',
        scheduled=False,
        run_at=datetime(2017, 6, 3, 6),
        status='queued',
        priority='normal',
        unique='foo')
    dbsession.add(workflow_instance)
    dbsession.commit()

    with pytest.raises(IntegrityError):
        workflow_instance = WorkflowInstance(
            workflow_name='workflow1',
            scheduled=False,
            run_at=datetime(2017, 6, 3, 6),
            status='queued',
            priority='normal',
            unique='foo')
        dbsession.add(workflow_instance)
        dbsession.commit()
示例#2
0
def test_workflow_starts(dbsession, workflows):
    now = datetime(2017, 6, 3, 6, 12)

    taskflow = Taskflow()
    taskflow.add_workflows(workflows)

    workflow_instance = WorkflowInstance(
        workflow_name='workflow1',
        scheduled=True,
        run_at=datetime(2017, 6, 3, 6),
        status='queued',
        priority='normal')
    dbsession.add(workflow_instance)
    dbsession.commit()

    scheduler = Scheduler(taskflow, now_override=now)
    scheduler.run(dbsession)

    assert workflow_instance.status == 'running'
    assert workflow_instance.started_at == now

    task_instances = dbsession.query(TaskInstance).all()
    assert len(task_instances) == 2
    for instance in task_instances:
        assert instance.task_name in ['task1','task2']
        assert instance.status == 'queued'
示例#3
0
def test_workflow_running_no_change(dbsession, workflows):
    taskflow = Taskflow()
    taskflow.add_workflows(workflows)

    workflow1 = workflows[0]

    workflow_instance = WorkflowInstance(
        workflow_name='workflow1',
        scheduled=True,
        run_at=datetime(2017, 6, 3, 6),
        started_at=datetime(2017, 6, 3, 6),
        status='running',
        priority='normal')
    dbsession.add(workflow_instance)
    dbsession.commit()

    task_instance1 = TaskInstance(
        task_name='task1',
        scheduled=True,
        workflow_instance_id=workflow_instance.id,
        status='running',
        run_at=datetime(2017, 6, 3, 6, 0, 34),
        attempts=1,
        priority='normal',
        push=False,
        timeout=300,
        retry_delay=300)
    task_instance2 = TaskInstance(
        task_name='task2',
        scheduled=True,
        workflow_instance_id=workflow_instance.id,
        status='running',
        run_at=datetime(2017, 6, 3, 6, 0, 34),
        attempts=1,
        priority='normal',
        push=False,
        timeout=300,
        retry_delay=300)
    dbsession.add(task_instance1)
    dbsession.add(task_instance2)
    dbsession.commit()

    scheduler = Scheduler(taskflow, now_override=datetime(2017, 6, 3, 6, 12))
    scheduler.run(dbsession)

    task_instances = dbsession.query(TaskInstance).all()
    assert len(task_instances) == 2
    for instance in task_instances:
        assert instance.task_name in ['task1','task2']
        assert instance.status == 'running'
示例#4
0
def test_workflow_fail(dbsession, workflows):
    taskflow = Taskflow()
    taskflow.add_workflows(workflows)

    workflow_instance = WorkflowInstance(
        workflow_name='workflow1',
        scheduled=True,
        run_at=datetime(2017, 6, 3, 6),
        status='running',
        priority='normal')
    dbsession.add(workflow_instance)
    dbsession.commit()
    task_instance1 = TaskInstance(
        task_name='task1',
        scheduled=True,
        workflow_instance_id=workflow_instance.id,
        status='success',
        run_at=datetime(2017, 6, 3, 6, 0, 34),
        attempts=1,
        priority='normal',
        push=False,
        timeout=300,
        retry_delay=300)
    task_instance2 = TaskInstance(
        task_name='task2',
        scheduled=True,
        workflow_instance_id=workflow_instance.id,
        status='success',
        run_at=datetime(2017, 6, 3, 6, 0, 34),
        attempts=1,
        priority='normal',
        push=False,
        timeout=300,
        retry_delay=300)
    task_instance3 = TaskInstance(
        task_name='task3',
        scheduled=True,
        workflow_instance_id=workflow_instance.id,
        status='failed',
        run_at=datetime(2017, 6, 3, 6, 0, 34),
        attempts=1,
        priority='normal',
        push=False,
        timeout=300,
        retry_delay=300)
    dbsession.add(task_instance1)
    dbsession.add(task_instance2)
    dbsession.add(task_instance3)
    dbsession.commit()

    now = datetime(2017, 6, 3, 6, 12)

    scheduler = Scheduler(taskflow, now_override=now)
    scheduler.run(dbsession)

    dbsession.refresh(workflow_instance)
    assert workflow_instance.status == 'failed'
    assert workflow_instance.ended_at == now

    task_instances = dbsession.query(TaskInstance).all()
    assert len(task_instances) == 3
    for instance in task_instances:
        assert instance.task_name in ['task1','task2','task3']
        if instance.task_name in ['task1','task2']:
            assert instance.status == 'success'
        elif instance.task_name == 'task3':
            assert instance.status == 'failed'
示例#5
0
def instances(dbsession, workflows):
    workflow_instance = WorkflowInstance(workflow_name='workflow1',
                                         scheduled=True,
                                         run_at=datetime(2017, 6, 3, 6),
                                         started_at=datetime(2017, 6, 3, 6),
                                         status='running',
                                         priority='normal')
    dbsession.add(workflow_instance)
    dbsession.commit()
    task_instance1 = TaskInstance(task_name='task1',
                                  scheduled=True,
                                  workflow_instance_id=workflow_instance.id,
                                  status='success',
                                  run_at=datetime(2017, 6, 3, 6, 0, 12),
                                  started_at=datetime(2017, 6, 3, 6, 0, 12),
                                  ended_at=datetime(2017, 6, 3, 6, 0, 18),
                                  attempts=1,
                                  priority='normal',
                                  push=False,
                                  timeout=300,
                                  retry_delay=300)
    task_instance2 = TaskInstance(task_name='task2',
                                  scheduled=True,
                                  workflow_instance_id=workflow_instance.id,
                                  status='success',
                                  run_at=datetime(2017, 6, 3, 6, 0, 20),
                                  started_at=datetime(2017, 6, 3, 6, 0, 20),
                                  ended_at=datetime(2017, 6, 3, 6, 0, 27),
                                  attempts=1,
                                  priority='normal',
                                  push=False,
                                  timeout=300,
                                  retry_delay=300)
    task_instance3 = TaskInstance(task_name='task3',
                                  scheduled=True,
                                  workflow_instance_id=workflow_instance.id,
                                  status='success',
                                  run_at=datetime(2017, 6, 3, 6, 0, 28),
                                  started_at=datetime(2017, 6, 3, 6, 0, 28),
                                  ended_at=datetime(2017, 6, 3, 6, 0, 32),
                                  attempts=1,
                                  priority='normal',
                                  push=False,
                                  timeout=300,
                                  retry_delay=300)
    task_instance4 = TaskInstance(task_name='task4',
                                  scheduled=True,
                                  workflow_instance_id=workflow_instance.id,
                                  status='running',
                                  run_at=datetime(2017, 6, 3, 6, 0, 34),
                                  started_at=datetime(2017, 6, 3, 6, 0, 34),
                                  attempts=1,
                                  priority='normal',
                                  push=False,
                                  timeout=300,
                                  retry_delay=300)
    dbsession.add(task_instance1)
    dbsession.add(task_instance2)
    dbsession.add(task_instance3)
    dbsession.add(task_instance4)
    dbsession.commit()
示例#6
0
def test_slack_monitor(dbsession, workflows):
    taskflow = Taskflow()
    taskflow.add_workflows(workflows)

    workflow_instance = WorkflowInstance(workflow_name='workflow1',
                                         scheduled=True,
                                         run_at=datetime(2017, 6, 3, 6),
                                         started_at=datetime(2017, 6, 3, 6),
                                         ended_at=datetime(
                                             2017, 6, 3, 6, 0, 36),
                                         status='running',
                                         priority='normal')
    dbsession.add(workflow_instance)
    dbsession.commit()
    task_instance1 = TaskInstance(task_name='task1',
                                  scheduled=True,
                                  workflow_instance_id=workflow_instance.id,
                                  status='success',
                                  run_at=datetime(2017, 6, 3, 6, 0, 34),
                                  attempts=1,
                                  priority='normal',
                                  push=False,
                                  timeout=300,
                                  retry_delay=300)
    task_instance2 = TaskInstance(task_name='task2',
                                  scheduled=True,
                                  workflow_instance_id=workflow_instance.id,
                                  status='success',
                                  run_at=datetime(2017, 6, 3, 6, 0, 34),
                                  attempts=1,
                                  priority='normal',
                                  push=False,
                                  timeout=300,
                                  retry_delay=300)
    task_instance3 = TaskInstance(task_name='task3',
                                  scheduled=True,
                                  workflow_instance_id=workflow_instance.id,
                                  status='failed',
                                  run_at=datetime(2017, 6, 3, 6, 0, 34),
                                  attempts=1,
                                  priority='normal',
                                  push=False,
                                  timeout=300,
                                  retry_delay=300)
    dbsession.add(task_instance1)
    dbsession.add(task_instance2)
    dbsession.add(task_instance3)
    dbsession.commit()

    slack_url = 'http://fakeurl.com/foo'

    slack_monitor = SlackMonitor(taskflow, slack_url=slack_url)

    with requests_mock.Mocker() as m:
        m.post(slack_url)

        slack_monitor.workflow_failed(dbsession, workflow_instance)

        assert m.called
        assert m.call_count == 1

        request = m.request_history[0]
        data = request.json()
        assert dict_contains(
            data['attachments'][0], {
                'text':
                '<!channel> A workflow in Taskflow failed',
                'title':
                'Workflow Failure',
                'color':
                '#ff0000',
                'fields': [{
                    'title': 'Workflow',
                    'short': False,
                    'value': 'workflow1'
                }, {
                    'title': 'ID',
                    'value': 1
                }, {
                    'title': 'Priority',
                    'value': 'normal'
                }, {
                    'title': 'Scheduled Run Time',
                    'value': '2017-06-03 06:00:00'
                }, {
                    'title': 'Start Time',
                    'value': '2017-06-03 06:00:00'
                }, {
                    'title': 'Failure Time',
                    'value': '2017-06-03 06:00:36'
                }, {
                    'title': 'Schedule',
                    'value': 'At 06:00 AM (0 6 * * *)'
                }]
            })
        assert dict_contains(
            data['attachments'][1], {
                'title':
                'Failed Workflow Task',
                'color':
                '#ff0000',
                'fields': [{
                    'title': 'Task',
                    'value': 'task3'
                }, {
                    'title': 'ID',
                    'value': 3
                }, {
                    'title': 'Number of Attempts',
                    'value': 1
                }, {
                    'title': 'Logs',
                    'value': None
                }]
            })