Ejemplo n.º 1
0
def create_cron_trigger(name, workflow_name, workflow_input,
                        workflow_params=None, pattern=None, first_time=None,
                        count=None, start_time=None, workflow_id=None):
    if not start_time:
        start_time = datetime.datetime.now()

    if isinstance(first_time, six.string_types):
        try:
            first_time = datetime.datetime.strptime(
                first_time,
                '%Y-%m-%d %H:%M'
            )
        except ValueError as e:
            raise exc.InvalidModelException(e.message)

    validate_cron_trigger_input(pattern, first_time, count)

    first_utc_time = first_time

    if first_time:
        first_second = time.mktime(first_time.timetuple())
        first_utc_time = datetime.datetime.utcfromtimestamp(first_second)
        next_time = first_utc_time

        if not (pattern or count):
            count = 1
    else:
        next_time = get_next_execution_time(pattern, start_time)

    with db_api.transaction():
        wf_def = db_api.get_workflow_definition(
            workflow_id if workflow_id else workflow_name
        )

        eng_utils.validate_input(
            wf_def,
            workflow_input or {},
            parser.get_workflow_spec(wf_def.spec)
        )

        values = {
            'name': name,
            'pattern': pattern,
            'first_execution_time': first_utc_time,
            'next_execution_time': next_time,
            'remaining_executions': count,
            'workflow_name': wf_def.name,
            'workflow_id': wf_def.id,
            'workflow_input': workflow_input or {},
            'workflow_params': workflow_params or {},
            'scope': 'private'
        }

        security.add_trust_id(values)

        trig = db_api.create_cron_trigger(values)

    return trig
Ejemplo n.º 2
0
def create_event_trigger(name, exchange, topic, event, workflow_id,
                         scope='private', workflow_input=None,
                         workflow_params=None):
    with db_api.transaction():
        wf_def = db_api.get_workflow_definition_by_id(workflow_id)

        wf_spec = parser.get_workflow_spec_by_definition_id(
            wf_def.id,
            wf_def.updated_at
        )

        # TODO(rakhmerov): Use Workflow object here instead of utils.
        eng_utils.validate_input(
            wf_spec.get_input(),
            workflow_input,
            wf_spec.get_name(),
            wf_spec.__class__.__name__
        )

        values = {
            'name': name,
            'workflow_id': workflow_id,
            'workflow_input': workflow_input or {},
            'workflow_params': workflow_params or {},
            'exchange': exchange,
            'topic': topic,
            'event': event,
            'scope': scope,
        }

        security.add_trust_id(values)

        trig = db_api.create_event_trigger(values)

        trigs = db_api.get_event_triggers(insecure=True, exchange=exchange,
                                          topic=topic)
        events = [t.event for t in trigs]

        # NOTE(kong): Send RPC message within the db transaction, rollback if
        # any error occurs.
        trig_dict = trig.to_dict()
        trig_dict['workflow_namespace'] = wf_def.namespace

        rpc.get_event_engine_client().create_event_trigger(
            trig_dict,
            events
        )

    return trig
Ejemplo n.º 3
0
def _create_or_update_workflows(wb_db, workflows_spec):
    if workflows_spec:
        for wf_spec in workflows_spec:
            wf_name = '%s.%s' % (wb_db.name, wf_spec.get_name())

            values = {
                'name': wf_name,
                'spec': wf_spec.to_dict(),
                'scope': wb_db.scope,
                'project_id': wb_db.project_id,
                'tags': wf_spec.get_tags()
            }

            security.add_trust_id(values)

            db_api_v2.create_or_update_workflow_definition(wf_name, values)
Ejemplo n.º 4
0
def _create_or_update_workflows(wb_db, workflows_spec):
    if workflows_spec:
        for wf_spec in workflows_spec:
            wf_name = "%s.%s" % (wb_db.name, wf_spec.get_name())

            values = {
                "name": wf_name,
                "spec": wf_spec.to_dict(),
                "scope": wb_db.scope,
                "project_id": wb_db.project_id,
                "tags": wf_spec.get_tags(),
            }

            security.add_trust_id(values)

            db_api_v2.create_or_update_workflow_definition(wf_name, values)
Ejemplo n.º 5
0
def _create_or_update_workflows(wb_db, workflows_spec):
    if workflows_spec:
        for wf_spec in workflows_spec:
            wf_name = '%s.%s' % (wb_db.name, wf_spec.get_name())

            values = {
                'name': wf_name,
                'spec': wf_spec.to_dict(),
                'scope': wb_db.scope,
                'project_id': wb_db.project_id,
                'tags': wf_spec.get_tags()
            }

            security.add_trust_id(values)

            db_api_v2.create_or_update_workflow_definition(wf_name, values)
Ejemplo n.º 6
0
def create_event_trigger(name,
                         exchange,
                         topic,
                         event,
                         workflow_id,
                         scope='private',
                         workflow_input=None,
                         workflow_params=None):
    with db_api.transaction():
        wf_def = db_api.get_workflow_definition_by_id(workflow_id)

        wf_spec = parser.get_workflow_spec_by_definition_id(
            wf_def.id, wf_def.updated_at)

        # TODO(rakhmerov): Use Workflow object here instead of utils.
        eng_utils.validate_input(wf_spec.get_input(), workflow_input,
                                 wf_spec.get_name(),
                                 wf_spec.__class__.__name__)

        values = {
            'name': name,
            'workflow_id': workflow_id,
            'workflow_input': workflow_input or {},
            'workflow_params': workflow_params or {},
            'exchange': exchange,
            'topic': topic,
            'event': event,
            'scope': scope,
        }

        security.add_trust_id(values)

        trig = db_api.create_event_trigger(values)

        trigs = db_api.get_event_triggers(insecure=True,
                                          exchange=exchange,
                                          topic=topic)
        events = [t.event for t in trigs]

        # NOTE(kong): Send RPC message within the db transaction, rollback if
        # any error occurs.
        trig_dict = trig.to_dict()
        trig_dict['workflow_namespace'] = wf_def.namespace

        rpc.get_event_engine_client().create_event_trigger(trig_dict, events)

    return trig
Ejemplo n.º 7
0
def create_cron_trigger(name, workflow_name, workflow_input,
                        workflow_params=None, pattern=None, first_time=None,
                        count=None, start_time=None):
    if not start_time:
        start_time = datetime.datetime.now()

    if type(first_time) in [str, unicode]:
        try:
            first_time = datetime.datetime.strptime(first_time,
                                                    '%Y-%m-%d %H:%M')
        except ValueError as e:
            raise exc.InvalidModelException(e.message)

    validate_cron_trigger_input(pattern, first_time, count)

    if first_time:
        next_time = first_time
        if not (pattern and count):
            count = 1
    else:
        next_time = get_next_execution_time(pattern, start_time)

    with db_api_v2.transaction():
        wf = db_api_v2.get_workflow_definition(workflow_name)

        values = {
            'name': name,
            'pattern': pattern,
            'next_execution_time': next_time,
            'remaining_executions': count,
            'workflow_name': workflow_name,
            'workflow_id': wf.id,
            'workflow_input': workflow_input or {},
            'workflow_params': workflow_params or {},
            'scope': 'private'
        }

        security.add_trust_id(values)

        trig = db_api_v2.create_cron_trigger(values)

    return trig
Ejemplo n.º 8
0
def create_event_trigger(name,
                         exchange,
                         topic,
                         event,
                         workflow_id,
                         workflow_input=None,
                         workflow_params=None):
    with db_api.transaction():
        wf_def = db_api.get_workflow_definition_by_id(workflow_id)

        eng_utils.validate_input(
            wf_def, workflow_input or {},
            parser.get_workflow_spec_by_definition_id(wf_def.id,
                                                      wf_def.updated_at))

        values = {
            'name': name,
            'workflow_id': workflow_id,
            'workflow_input': workflow_input or {},
            'workflow_params': workflow_params or {},
            'exchange': exchange,
            'topic': topic,
            'event': event,
        }

        security.add_trust_id(values)

        trig = db_api.create_event_trigger(values)

        trigs = db_api.get_event_triggers(insecure=True,
                                          exchange=exchange,
                                          topic=topic)
        events = [t.event for t in trigs]

        # NOTE(kong): Send RPC message within the db transaction, rollback if
        # any error occurs.
        rpc.get_event_engine_client().create_event_trigger(
            trig.to_dict(), events)

    return trig
Ejemplo n.º 9
0
def create_delay_tolerant_workload(name, workflow_name, workflow_input,
                                   workflow_params=None, deadline=None,
                                   job_duration=None, workflow_id=None):
    try:
        deadline = date_parser.parse(deadline)
    except ValueError as e:
        raise exc.InvalidModelException(e.message)
    if deadline < datetime.datetime.now() + datetime.timedelta(seconds=60):
        raise exc.InvalidModelException(
            'deadline must be at least 1 minute in the future.'
        )

    with db_api.transaction():
        wf_def = db_api.get_workflow_definition(
            workflow_id if workflow_id else workflow_name
        )

        eng_utils.validate_input(
            wf_def,
            workflow_input or {},
            parser.get_workflow_spec(wf_def.spec)
        )

        values = {
            'name': name,
            'deadline': deadline,
            'job_duration': job_duration,
            'workflow_name': wf_def.name,
            'workflow_id': wf_def.id,
            'workflow_input': workflow_input or {},
            'workflow_params': workflow_params or {},
            'scope': 'private',
            'executed': False
        }

        security.add_trust_id(values)

        dtw = db_api.create_delay_tolerant_workload(values)

    return dtw
Ejemplo n.º 10
0
def create_event_trigger(name, exchange, topic, event, workflow_id,
                         workflow_input=None, workflow_params=None):
    with db_api.transaction():
        wf_def = db_api.get_workflow_definition_by_id(workflow_id)

        eng_utils.validate_input(
            wf_def,
            workflow_input or {},
            parser.get_workflow_spec_by_definition_id(
                wf_def.id,
                wf_def.updated_at
            )
        )

        values = {
            'name': name,
            'workflow_id': workflow_id,
            'workflow_input': workflow_input or {},
            'workflow_params': workflow_params or {},
            'exchange': exchange,
            'topic': topic,
            'event': event,
        }

        security.add_trust_id(values)

        trig = db_api.create_event_trigger(values)

        trigs = db_api.get_event_triggers(insecure=True, exchange=exchange,
                                          topic=topic)
        events = [t.event for t in trigs]

        # NOTE(kong): Send RPC message within the db transaction, rollback if
        # any error occurs.
        rpc.get_event_engine_client().create_event_trigger(
            trig.to_dict(),
            events
        )

    return trig
Ejemplo n.º 11
0
def create_cron_trigger(name,
                        workflow_name,
                        workflow_input,
                        workflow_params=None,
                        pattern=None,
                        first_time=None,
                        count=None,
                        start_time=None,
                        workflow_id=None):
    if not start_time:
        start_time = datetime.datetime.utcnow()

    if isinstance(first_time, six.string_types):
        try:
            first_time = datetime.datetime.strptime(first_time,
                                                    '%Y-%m-%d %H:%M')
        except ValueError as e:
            raise exc.InvalidModelException(str(e))

    validate_cron_trigger_input(pattern, first_time, count)

    if first_time:
        next_time = first_time

        if not (pattern or count):
            count = 1
    else:
        next_time = get_next_execution_time(pattern, start_time)

    with db_api.transaction():
        wf_def = db_api.get_workflow_definition(
            workflow_id if workflow_id else workflow_name)

        wf_spec = parser.get_workflow_spec_by_definition_id(
            wf_def.id, wf_def.updated_at)

        # TODO(rakhmerov): Use Workflow object here instead of utils.
        eng_utils.validate_input(wf_spec.get_input(), workflow_input,
                                 wf_spec.get_name(),
                                 wf_spec.__class__.__name__)

        trigger_parameters = {
            'name': name,
            'pattern': pattern,
            'first_execution_time': first_time,
            'next_execution_time': next_time,
            'remaining_executions': count,
            'workflow_name': wf_def.name,
            'workflow_id': wf_def.id,
            'workflow_input': workflow_input or {},
            'workflow_params': workflow_params or {},
            'scope': 'private'
        }

        security.add_trust_id(trigger_parameters)

        try:
            trig = db_api.create_cron_trigger(trigger_parameters)
        except Exception:
            # Delete trust before raising exception.
            security.delete_trust(trigger_parameters.get('trust_id'))
            raise

    return trig
Ejemplo n.º 12
0
def create_cron_trigger(name, workflow_name, workflow_input,
                        workflow_params=None, pattern=None, first_time=None,
                        count=None, start_time=None, workflow_id=None):
    if not start_time:
        start_time = datetime.datetime.utcnow()

    if isinstance(first_time, six.string_types):
        try:
            first_time = datetime.datetime.strptime(
                first_time,
                '%Y-%m-%d %H:%M'
            )
        except ValueError as e:
            raise exc.InvalidModelException(str(e))

    validate_cron_trigger_input(pattern, first_time, count)

    if first_time:
        next_time = first_time

        if not (pattern or count):
            count = 1
    else:
        next_time = get_next_execution_time(pattern, start_time)

    with db_api.transaction():
        wf_def = db_api.get_workflow_definition(
            workflow_id if workflow_id else workflow_name
        )

        wf_spec = parser.get_workflow_spec_by_definition_id(
            wf_def.id,
            wf_def.updated_at
        )

        # TODO(rakhmerov): Use Workflow object here instead of utils.
        eng_utils.validate_input(
            wf_spec.get_input(),
            workflow_input,
            wf_spec.get_name(),
            wf_spec.__class__.__name__
        )

        trigger_parameters = {
            'name': name,
            'pattern': pattern,
            'first_execution_time': first_time,
            'next_execution_time': next_time,
            'remaining_executions': count,
            'workflow_name': wf_def.name,
            'workflow_id': wf_def.id,
            'workflow_input': workflow_input or {},
            'workflow_params': workflow_params or {},
            'scope': 'private'
        }

        security.add_trust_id(trigger_parameters)

        try:
            trig = db_api.create_cron_trigger(trigger_parameters)
        except Exception:
            # Delete trust before raising exception.
            security.delete_trust(trigger_parameters.get('trust_id'))
            raise

    return trig