Shivam Raj
Shivam Raj

Reputation: 23

How to define task in node-celery ?

I am using node-celery. I just need to implement scheduling task so that Task can run on the background at the specific time. I am confused how can I define my task, currently I am defining task at the same file where I am implementing node-celery.

const celery = require('node-celery');
let client = celery.createClient({
                CELERY_BROKER_URL: 'amqp://guest:guest@localhost:5672//',
                CELERY_RESULT_BACKEND: 'amqp://'
            });

In the above code, I just require node-celery and then created a client for amqp. Now I have to connect client and then call my task send_batch_email_using_mailgun with some parameter.

client.on('connect', function() {
            client.call('send_batch_email_using_mailgun', {
                campaign_data: campaign_data,
                subject: subject,
                template: template
            }, {
                eta: new Date(Date.now() + 120000)
            });
        });

Here _send_batch_email_using_mailgun_ is the task which is defined below the code in the same file with some parameter. I want that my function _send_batch_email_using_mailgun_ should be called after a certain time. My code is not working I think I have to define my task function elsewhere but I don't know where to define them. Do I need to create my task in python file? If yes then how can I import them in my js file?

Upvotes: 1

Views: 931

Answers (2)

Gil Hiram
Gil Hiram

Reputation: 512

TL;DR

its a python function.
for more info see the docs

details

the lingo here is a bit confusing so i'll try to clear it up:
your celery client is actually connecting to the broker (which is the task queue). The broker will recieve a message with the function name and parameters you wish to run. The celery worker is a python process which pulls the messages from the broker and then executes the function you requested.

The implementation of send_batch_email_using_mailgun only needs to be known to the celery worker.

Upvotes: 0

Tamal Chowdhury
Tamal Chowdhury

Reputation: 542

You can start small and use the setInterval() api to make a scheduled task.

Let's say your task is to send email to all users.

Define it in a function like this:

function sendScheduledEmails() {
  // Get all the emails
  // Send emails with your provider
}

Test it if it works by calling the func, but just putting it and running your server once:

sendScheduledEmails() 

When you see your function is working, use the api:

setInterval(sendScheduledEmails, 120000)

Check if your app is doing the task within the intervals. Once you see it's working, you can look into other modules and further tweak it.

If you want to take action after something happens. Let's say you want to send email after a user is registered.

Then when the user registers, you can emit an event like this:

const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();

// User registered
// Redirect to success page

myEmitter.emit('userRegistered');

This will fire an event everytime that action happens, and you can listen to it.

Then in your code you can do:

myEmitter.on('userRegistered', function() {
  setInterval(sendScheduledEmails, 120000)
})

Upvotes: 1

Related Questions