Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery: why do I need a broker for periodic tasks?

I have a standalong script that scrapes a page, initiates a connection to a database, and writes database to it. I need it to execute periodically after x hours. I can make it with using a bash script, with the pseudocode:

while true
do
  python scraper.py
  sleep 60*60*x
done

From what I read about message brokers, they are used for sending "signals" from one running program to another, like HTTP in principle. Like I have a piece of code that accepts an email id from user, it sends signal with email-id to another piece of code that will send the email.

I need celery to run a periodic task on heroku. I already have a mongodb on a separate server. WHy do I need to run another server for rabbitmq or redis just for this? Can I use celery without the broker?

like image 770
yayu Avatar asked Oct 07 '14 07:10

yayu


2 Answers

Celery architecture is designed to scale and distribute tasks across several servers. For sites like yours it might be an overkill. Queue service is generally needed to maintain the task list and signal the status of finished tasks.

You might want to take a look in Huey instead. Huey is small-scale Celery "Clone" needing only Redis as an external dependency, not RabbitMQ. It's still using Redis queue mechanism to line the tasks in queue.

There also exists Advanced Python scheduler which does not need even Redis, but can hold the state of the queue in memory in-process.

Alternatively if you have very small amount of periodical tasks, no delayed tasks, I would just use Cron and pure Python scripts to run the tasks.

like image 123
Mikko Ohtamaa Avatar answered Sep 21 '22 18:09

Mikko Ohtamaa


As the Celery documentation explains:

Celery communicates via messages, usually using a broker to mediate between clients and workers. To initiate a task, a client adds a message to the queue, which the broker then delivers to a worker.

You can use your existing MongoDB database as broker. see Using MongoDB.

like image 26
Lukas Batteau Avatar answered Sep 17 '22 18:09

Lukas Batteau