Exemplo n.º 1
0
def update_datadoc_schedule(id, cron=None, enabled=None, kwargs=None):
    if kwargs is not None:
        kwargs_valid, kwargs_valid_reason = validate_datadoc_schedule_config(
            kwargs)
        api_assert(kwargs_valid, kwargs_valid_reason)
    if cron is not None:
        api_assert(validate_cron(cron), "Invalid cron expression")

    schedule_name = schedule_logic.get_data_doc_schedule_name(id)
    with DBSession() as session:
        assert_can_write(id, session=session)

        schedule = schedule_logic.get_task_schedule_by_name(schedule_name,
                                                            session=session)
        api_assert(schedule, "Schedule does not exist")
        verify_data_doc_permission(id, session=session)

        updated_fields = {}
        if cron is not None:
            updated_fields["cron"] = cron
        if enabled is not None:
            updated_fields["enabled"] = enabled
        if kwargs is not None:
            updated_fields["kwargs"] = {
                **kwargs,
                "user_id": current_user.id,
                "doc_id": id,
            }

        return schedule_logic.update_task_schedule(
            schedule.id,
            session=session,
            **updated_fields,
        )
Exemplo n.º 2
0
def create_datadoc_schedule(
    id,
    cron,
    kwargs,
):
    kwargs_valid, kwargs_valid_reason = validate_datadoc_schedule_config(
        kwargs)
    api_assert(kwargs_valid, kwargs_valid_reason)
    api_assert(validate_cron(cron), "Invalid cron expression")

    schedule_name = schedule_logic.get_data_doc_schedule_name(id)
    with DBSession() as session:
        assert_can_write(id, session=session)
        data_doc = logic.get_data_doc_by_id(id, session=session)
        verify_environment_permission([data_doc.environment_id])

        return schedule_logic.create_task_schedule(
            schedule_name,
            "tasks.run_datadoc.run_datadoc",
            cron=cron,
            kwargs={
                **kwargs, "user_id": current_user.id,
                "doc_id": id
            },
            task_type="user",
            session=session,
        )
Exemplo n.º 3
0
def update_schedule(id, **kwargs):
    allowed_fields = ["cron", "args", "kwargs", "enabled", "options"]
    filtered_kwargs = {k: v for k, v in kwargs.items() if k in allowed_fields}

    if "cron" in filtered_kwargs:
        api_assert(validate_cron(filtered_kwargs["cron"]), "Invalid cron expression")

    return logic.update_task_schedule(id=id, **filtered_kwargs)
Exemplo n.º 4
0
def create_task_schedule(
    cron, name, task, task_type, enabled, args=None, kwargs=None, options=None,
):
    with DBSession() as session:
        api_assert(validate_cron(cron), "Invalid cron expression")

        return logic.create_task_schedule(
            name=name,
            task=task,
            cron=cron,
            args=args,
            kwargs=kwargs,
            task_type=task_type,
            options=options,
            enabled=enabled,
            session=session,
        )