Always_a_learner
Always_a_learner

Reputation: 5055

how to use celery beat with node-celery

I am using node-celery github link to implement celery celery first step along with rabbitmq.

As in celery we define task and then we push them.

My task which I have defined inside tasks.py is as below:

import os
import logging
from celery import Celery
import requests

backend = os.getenv('CELERY_BACKEND_URL', 'amqp')
celery = Celery('tasks', backend=backend)

celery.conf.update(
    CELERY_RESULT_SERIALIZER='json',
    CELERY_ENABLE_UTC=True
)

@celery.task
def getOrders():
    requests.get('locahost:4000/getOrders')

My file which I run to trigger tasks:

eta.js:

  var celery = require('../celery'),
        client = celery.createClient({
            CELERY_BROKER_URL: 'amqp://guest:guest@localhost:5672//'
     });
    
    
    
    client.on('error', function(err) {
        console.log(err);
    });
    
    client.on('connect', function() {
        client.call('tasks.getOrders', {
            eta: new Date(Date.now() + 15 * 1000) // an hour later
        });
    });

I start celery worker by using below command:

celery worker -A tasks -l info

Now when I run eta.js and my task 'getOrders' defined inside tasks.py gets triggers and it further hits the url I have requested and that URL does its work.

To run eta.js I run:

 node eta.js

What I want is that my task 'getOrders' keeps running after every x seconds. I have read about setting periodic tasks in celery periodic tasks celery but it is in python and I need this in node-celery.

I want to use celery beat in node-celery if it is possible or should I just avoid using node celery? and use celery python setup, I know python but couldn't find any link to setup celery in python.

Anyone having good knowledge about celery can help me or guide me to follow some tutorial.

thanks!

Upvotes: 1

Views: 1476

Answers (1)

Emilio Camacho
Emilio Camacho

Reputation: 11

You can add the scheduled task to Redis directly so Redbeat will read this new task and it will execute it and reschedule it according to the crontab setup.

To do this:

  1. Add the redbead redis url in your celery config
  2. Create a periodic task
const task = {
     name : "getOrdersTask",
     task : "tasks.getOrders",
     schedule: {
         "__type__": "crontab",
         minute : "*/5",
         hour : "*",
         day_of_week :
         day_of_month : "*/7"
         month_of_year : "[1-12]"
     },
     args : [],
     enabled : true,
}
  1. Store the task in redis with:

    redisClient.hset("redbeat:getOrdersTask", "definition", JSON.stringify(task));
    
  2. Insert the task into the tasks list (the 0 is to force Redbeat to execute it immediately so it will be rescheduled in the proper time according to the period config you selected, for this case every 5 minutes)

    redisClient.zadd("redbeat::schedule", 0, "getOrdersTask")
    

Upvotes: 0

Related Questions