How it Works¶
How to trigger a task¶
First, check configuration to enable tasks to be triggered from Lime admin
To trigger a task:
- Go to Administration page ("Lime admin") -> Settings -> Task starter
- Go to the task you want to trigger
- If your task has arguments, write the correct info in the json field
- Click "Start task as logged in user" or "Start task as user username" if you have configured a impersonate user
Task history with status and result¶
A task can have 3 states, Success, Pending or Failure. If you want to refresh the status, click the arrow circle to the right.
If you want to see a result your task need to return a value, preferible a string describing what the task have accomplished.
If you in your task raise the exception TaskFailure with a message, that will show up as a result and a fail status. If the task encounter a unhandled exception it will show up as failed without any result.
A best practice could be to wrap your task with a try except and handle any exception at this level by raising the TaskFailure exception, example:
@task
def example_task(app, text: str):
try:
return "Done with the task: " + text
except Exception as e:
logger.exception(e)
raise TaskFailure(e)
The package only saves the latest 10 runs per task, to increase this. Add the flag task_history
in your application config:
lisa_application_name:
config:
limepkg_task_starter:
task_history: 20
Track tasks triggered elsewhere¶
Info
Remember to add your task to the application config to see the task history in Lime admin
If you have a task to be visible in the history that you trigger in you own code, add the command save_task_id
after lime_task.send_task
:
start_task = lime_task.send_task(
'solution_xxx.tasks.my_task_file.my_task',
app, 55, 66
)
save_task_id(
app=app,
task_path='solution_xxx.tasks.my_task_file.my_task',
task_id=start_task.id,
arguments={'number': 55, 'numberx': 66}
)
from limepkg_task_starter.db import save_task_id
task_path
parameter wants the task full import path as input, task_id want the task id and arguments
wants a dict with the current argument names and values.
Track scheduled tasks¶
Info
Remember to add your task to the application config to see the task history in Lime admin
If you have a scheduled task to be visible in the history, add this row in the task:
track_current_task(app=app, task=task_name, arguments={'argumentA': argumentA, 'argumentB': argumentB})
from limepkg_task_starter.db import track_current_task
task
parameter wants the current task as input and arguments
wants a dict with the current argument names and values.
Example:
from limepkg_task_starter.db import track_current_task
@task
def dummy_task(app, number: int = 5, numberx: int = 10):
try:
track_current_task(
app=app, task=dummy_task,
arguments={'number': number, 'numberx': numberx}
)
return number * 5 * numberx
except Exception as e:
logger.exception(e)
raise TaskFailure(e)
Action menu¶
On all task-instances, you also have the action menu where you can do:
- Copy task id to clipboard
- Copy arguments to code editor
- Run again with same arguments
- View info
View info actions show you some info about that task instance.