Scheduled tasks¶
Prerequisites¶
- Your solution is based on lime-crm 2.80.0 or later
Scheduling tasks¶
Scheduling tasks in Lime CRM is easy. The only thing you have to do is to write a few lines of code in your solution/limepkg.
Run the following command to scaffold a scheduled task for your solution:
The command above adds the function register_scheduled_tasks
to the solution's tasks
package.
Your tasks/__init__.py
module will look similar to the example below:
from . import tasks
from lime_task.schedule import (
CronTab,
ScheduledTask,
TaskSession)
def get_task_modules():
"""List all task modules that lime-task-handler should register"""
return [tasks]
def register_scheduled_tasks():
return[
ScheduledTask(
task=tasks.my_task,
session=TaskSession(
user_name='integration-user',
language='sv'),
args=['arg1', {'test': 'test'}, True, 1337, None],
schedule=CronTab(hour=7, minute=30, day_of_week=1))
]
The code above defines the function register_scheduled_tasks
and returns a list of ScheduledTask
instances.
Tasks returned by this function will be scheduled in Lime CRM.
For each ScheduledTask
instance you have to configure the following:
- Which task to run. This is the actual task function (decorated with
@task
) that you want to execute. - A
SessionTask
, i.e. which user should run the task and under what language. - A schedule, i.e. describe when the task should run and how often.
- Arguments that should be sent to the task. The first argument to a task will always be the
LimeApplication
instance. But if you have a task that takes additional arguments it's possible to send them in via theargs
argument.
Check content of __init__.py
file¶
If you create the scheduled-task in an old solution, you need to check that you have this in the __init__.py
file in the root of the solution:
try:
from .tasks import register_scheduled_tasks # noqa
except ImportError:
logger.info('solution_xxxxxxxx doesn\'t implement any scheduled tasks')
Otherwise, the scheduled-tasks won't be registered as scheduling tasks. New solutions should already have this generated from the lime-project new solution templates.
Configure the schedule¶
You configure the schedule for a task by creating a CronTab
instance. The CronTab
instance is defined in lime_task
but is actually just a wrapper around the Celery Crontab scheduler.
Please checkout the celery documentation for information about how to use it.
Warning
Ensure that the taskhandler (and the scheduler) is up and running. Otherwise your scheduled tasks will not be run.
Disable a scheduled task¶
It's possible to temporarily disable a task.
Note
A disabled task remains scheduled but will not be executed. A "dummy" function with information logs about the disabled task will be run instead.
You disable a task via the Application Configuration. (Read more here)
In the following example, the task my_solution.tasks.my_tasks.my_special_task
has been disabled:
Tip
remember to use the full path to your task, otherwise it will not be disabled
Error handling¶
It's important to know if a scheduled task failed. It's therefore possible to create an event handler that reacts to either succeeding or failing tasks. Read more about event handlers here and more about task events here
Services¶
You can find more information on the task-handler service here.
Windows¶
Scheduled tasks are dispatched by the lime-task-scheduler
and run by the lime-task-handler
. That means that you have to ensure that both services are running.
If it's only the scheduler that's running, a scheduled task will be dispatched, but never run.
Linux/Mac OS¶
Scheduled tasks are run and dispatched by the same service.