Django celery crontab every 30 seconds - is it even possible?

Can crontab django celeryab be launched in 30 seconds DURING SPECIFIC HOURS ?

There are only minutes, hours and days settings.

I have crontab running, but I would like to run it every 30 seconds, unlike every minute.

As an alternative...

Is it possible to turn on the interval for 30 seconds and turn on the interval schedule only for a certain period of the day?

+3
source share
3 answers

The very first example that they have in the documentation is ......

Example: Run the tasks.add task every 30 seconds.

from datetime import timedelta

CELERYBEAT_SCHEDULE = {
    "runs-every-30-seconds": {
        "task": "tasks.add",
        "schedule": timedelta(seconds=30),
        "args": (16, 16)
    },
}
+7
source

, , :    script. (, script, 0 30 script crontab, 0 , - 30).

, ! , , .

+3

, / :

# tasks.py
from djcelery.models import PeriodicTask

@app.task
def toggle_thirty_second_task:
    # you would use whatever you named your task below
    thirty_second_tasks = PeriodicTask.objects.filter(name='runs-every-30-seconds')
    if thirty_second_tasks:
        # this returned an array, so we'll want to look at the first object
        thirty_second_task = thirty_second_tasks[0]
        # toggle the enabled value
        thirty_second_task.enabled = not thirty_second_task.enabled
        thirty_second_task.save()

crontab , . , , - .

, .

+1

All Articles