Skip to content

Latest commit

 

History

History
346 lines (220 loc) · 7.94 KB

robocorp.tasks.md

File metadata and controls

346 lines (220 loc) · 7.94 KB

module robocorp.tasks

Robocorp tasks helps in creating entry points for your automation project.

To use:

Mark entry points with:

from robocorp.tasks import task

@task
def my_method():
    ...

Running options:

Runs all the tasks in a .py file:

python -m robocorp.tasks run <path_to_file>

Run all the tasks in files named task.py:

python -m robocorp.tasks run <directory>

Run only tasks with a given name:

python -m robocorp.tasks run <directory or file> -t <task_name>

Note: Using the cli.main(args) is possible to run tasks programmatically, but clients using this approach MUST make sure that any code which must be automatically logged is not imported prior the the cli.main call.

Functions


task

Decorator for tasks (entry points) which can be executed by robocorp.tasks.

i.e.:

If a file such as tasks.py has the contents below:

from robocorp.tasks import task

@task
def enter_user():
    ...

It's also possible to pass options to the task decorator that can then be introspected by task.options:

from robocorp.tasks import task

@task(this_is_option="option")
def enter_user():
    ...

It'll be executable by robocorp tasks as:

python -m robocorp.tasks run tasks.py -t enter_user

Args:

  • func: A function which is a task to robocorp.tasks.
  • **kwargs: Options to be introspected by task.options.

Link to source

task(*args, **kwargs)

setup

Run code before any tasks start, or before each separate task.

Receives as an argument the task or tasks that will be run.

Can be used as a decorator without arguments:

from robocorp.tasks import setup

@setup
def my_fixture(task):
    print(f"Before task: {task.name}")

Alternatively, can be called with a scope argument to decide when the fixture is run:

from robocorp.tasks import setup

@setup(scope="task")
def before_each(task):
    print(f"Running task '{task.name}'")

@setup(scope="session")
def before_all(tasks):
    print(f"Running {len(tasks)} task(s)")

By default, runs setups in task scope.

The setup fixture also allows running code after the execution, if it yields the execution to the task(s):

import time
from robocorp.tasks import setup

@setup
def measure_time(task):
    start = time.time()
    yield  # Task executes here
    duration = time.time() - start
    print(f"Task took {duration} seconds")

@task
def my_long_task():
    ...

Note: If fixtures are defined in another file, they need to be imported in the main tasks file to be taken into use

Link to source

setup(
    *args,
    **kwargs
) → Union[Callable[[ITask], Any], Callable[[Callable[[ITask], Any]], Callable[[ITask], Any]], Callable[[Callable[[Sequence[ITask]], Any]], Callable[[Sequence[ITask]], Any]]]

teardown

Run code after tasks have been run, or after each separate task.

Receives as an argument the task or tasks that were executed, which contain (among other things) the resulting status and possible error message.

Can be used as a decorator without arguments:

from robocorp.tasks import teardown

@teardown
def my_fixture(task):
    print(f"After task: {task.name})

Alternatively, can be called with a scope argument to decide when the fixture is run:

from robocorp.tasks import teardown

@teardown(scope="task")
def after_each(task):
    print(f"Task '{task.name}' status is '{task.status}'")

@teardown(scope="session")
def after_all(tasks):
    print(f"Executed {len(tasks)} task(s)")

By default, runs teardowns in task scope.

Note: If fixtures are defined in another file, they need to be imported in the main tasks file to be taken into use

Link to source

teardown(
    *args,
    **kwargs
) → Union[Callable[[ITask], Any], Callable[[Callable[[ITask], Any]], Callable[[ITask], Any]], Callable[[Callable[[Sequence[ITask]], Any]], Callable[[Sequence[ITask]], Any]]]

session_cache

Provides decorator which caches return and clears automatically when all tasks have been run.

A decorator which automatically cache the result of the given function and will return it on any new invocation until robocorp-tasks finishes running all tasks.

The function may be either a generator with a single yield (so, the first yielded value will be returned and when the cache is released the generator will be resumed) or a function returning some value.

Args:

  • func: wrapped function.

Link to source

session_cache(func)

task_cache

Provides decorator which caches return and clears it automatically when the current task has been run.

A decorator which automatically cache the result of the given function and will return it on any new invocation until robocorp-tasks finishes running the current task.

The function may be either a generator with a single yield (so, the first yielded value will be returned and when the cache is released the generator will be resumed) or a function returning some value.

Args:

  • func: wrapped function.

Link to source

task_cache(func)

get_output_dir

Provide the output directory being used for the run or None if there's no output dir configured.

Link to source

get_output_dir() → Optional[Path]

get_current_task

Provides the task which is being currently run or None if not currently running a task.

Link to source

get_current_task() → Optional[ITask]

Class ITask

Properties

  • failed

Returns true if the task failed. (in which case usually exc_info is not None).

  • input_schema

The input schema from the function signature.

Example:

{
    "properties": {
        "value": {
            "type": "integer",
            "description": "Some value.",
            "title": "Value",
            "default": 0
        }
    },
    "type": "object"
}
  • lineno

The line where the task is declared.

  • managed_params_schema

The schema for the managed parameters.

Example:

{
    "my_password": {
        "type": "Secret"
    },
    "request": {
        "type": "Request"
    }
}
  • name

The name of the task.

  • output_schema

The output schema based on the function signature.

Example:

{
    "type": "string",
    "description": ""
}

Methods


run

Runs the task and returns its result.

Link to source

run() → Any

Enums


Status

Task state

Values

  • NOT_RUN = NOT_RUN
  • PASS = PASS
  • FAIL = FAIL