Celery Task not working - redis

every time I try to use delay() nothing happens.
from celery import Celery
app = Celery('tasks', broker='redis://localhost:6379')
#app.task
def run(msg):
print(msg)
word="xxx"
run.delay(word)
I checked redis with ping and it seems to be working.
Or maybe I should use something different to run background threads in Flask app?

You need to start worker in order to pick up the task to process.
http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html#id9

Related

Airflow with Celery not able to connect

Every time I start airflow worker I keep getting this error
[2017-11-07 16:24:12,354: ERROR/MainProcess] consumer: Cannot connect to amqp://myuser:**#127.0.0.1:8793/myvhost: timed out.
Trying again in 26.00 seconds...
I have followed the instructions to install CeleryExecutors on Airflow as well as installing RabbitMQ using this documentation.
I have went and configured my airflow.cfg to reflect this by changing the celery_result_backend and broker_url to point to the right address (amqp://myuser:mypassword#localhost:8793/myvhost for example, from the documentation). I had this up and running at some point and when I changed the DAG directory. Changing the DAG directory shouldn't have an effect on it besides changing what's inside the DagBag.
Is there anything else I'm supposed to look at to debug and get the Celery Workers up and running?
Following the posts here and here, it was because I was running Celery4.1.0. I downgraded to Celery3.1.7 and it is now working.

Connect to MQ inside a Scrapy spider or how to create ClientCreator inside Scrapy spider

I want to solve this issue: connect to MQ inside a spider.
Im trying to call ClientCreator, but i dont know where to get "reactor" variable inside a CrawlSpider...
d = ClientCreator(reactor, AMQClient, delegate=delegate, vhost='/', spec=spec).connectTCP('localhost', 5672)
d.addCallback(self.gotConnection, 'guest', 'guest')
Why i am connecting to MQ inside a spider and dont use Pipeline? I have a loop running spider that reads some jobs from MQ. Pipelines provides a storage, but i need to read and proccess MQ jobs. I cant read jobs and that run spider, cause i need to proccess mq jobs while spider is already logged in on website.
Does somebody have any suggessions how to connect to MQ inside a spider not to store parsed items but to read jobs?
Disclaimer: I have not tried this at home.
I think your best bet is to just do:
from twisted.internet import reactor
and then use that reactor with the ClientCreator.
If you take a look at the example of running a spider from a script from the official documentation page you will see that its just the same reactor being used.

rufus-scheduler and delayed_job on Heroku: why use a worker dyno?

I'm developing a Rails 3.2.16 app and deploying to a Heroku dev account with one free web dyno and no worker dynos. I'm trying to determine if a (paid) worker dyno is really needed.
The app sends various emails. I use delayed_job_active_record to queue those and send them out.
I also need to check a notification count every minute. For that I'm using rufus-scheduler.
rufus-scheduler seems able to run a background task/thread within a Heroku web dyno.
On the other hand, everything I can find on delayed_job indicates that it requires a separate worker process. Why? If rufus-scheduler can run a daemon within a web dyno, why can't delayed_job do the same?
I've tested the following for running my every-minute task and working off delayed_jobs, and it seems to work within the single Heroku web dyno:
config/initializers/rufus-scheduler.rb
require 'rufus-scheduler'
require 'delayed/command'
s = Rufus::Scheduler.singleton
s.every '1m', :overlap => false do # Every minute
Rails.logger.info ">> #{Time.now}: rufus-scheduler task started"
# Check for pending notifications and queue to delayed_job
User.send_pending_notifications
# work off delayed_jobs without a separate worker process
Delayed::Worker.new.work_off
end
This seems so obvious that I'm wondering if I'm missing something? Is this an acceptable way to handle the delayed_job queue without the added complexity and expense of a separate worker process?
Update
As #jmettraux points out, Heroku will idle an inactive web dyno after an hour. I haven't set it up yet, but let's assume I'm using one of the various keep-alive methods to keep it from sleeping: Easy way to prevent Heroku idling?.
According to this
https://blog.heroku.com/archives/2013/6/20/app_sleeping_on_heroku
your dyno will go to sleep if he hasn't serviced requests for an hour. No dyno, no scheduling.
This could help as well: https://devcenter.heroku.com/articles/clock-processes-ruby

How to configure and run remote celery worker correctly?

I'm new to celery and may be doing something wrong, but I already
spent a lot of trying to figure out how to configure celery
correctly.
So, in my environment I have 2 remote servers; one is main (it has
public IP address and most of the stuff like database server, rabbitmq
server and web server running my web application is there) and another
is used for specific tasks which I want to asynchronously invoke from
the main server using celery.
I was planning to use RabbitMQ as a broker and as results back-end.
Celery config is very basic:
CELERY_IMPORTS = ("main.tasks", )
BROKER_HOST = "Public IP of my main server"
BROKER_PORT = 5672
BROKER_USER = "guest"
BROKER_PASSWORD = "guest"
BROKER_VHOST = "/"
CELERY_RESULT_BACKEND = "amqp"
When I'm running a worker on the main server tasks are executed just
fine, but when I'm running it on the remote server only a few tasks
are executed and then worker gets stuck not being able to executed any
task. When I restart the worker it executes a few more tasks and gets
stuck again. There is nothing special inside the task and I even tried
a test task that just adds 2 numbers. I tried to run the worker
differently (demonizing and not, setting different concurrency and
using celeryd_multi), nothing really helped.
What could be the reason? Did I miss something? Do I have to run
something on the main server other than the broker (RabbitMQ)? Or is
it a bug in the celery (I tried a few version: 2.2.4, 2.3.3 and dev,
but none of them worked)?
Hm... I've just reproduced the same problem on the local worker, so I
don't really know what it is... Is it required to restart celery
worker after every N tasks executed?
Any help will be very much appreciated :)
Don't know if you ended up solving the problem, but I had similar symptoms. Turned out that (for whatever reason) print statements from within tasks was causing tasks not to complete (maybe some sort of deadlock situation?). Only some of the tasks had print statements, so when these tasks executed eventually the number of workers (set by concurrency option) were all exhausted, which caused tasks to stop executing.
Try to set your celery config to
CELERYD_PREFETCH_MULTIPLIER = 1
CELERYD_MAX_TASKS_PER_CHILD = 1
docs

Heroku: What to do when your dyno/worker crashes?

I have a worker doing some processing 24/7. However, sometimes the code crashes and it needs to be restarted (even if I catch the exception, I have to restart the worker in order for it to work).
What do you do when this happens or am I doing something wrong and this shouldn't happen at all? Does your dynos/workers crash or it is just me?
thanks
Heroku is supposed to restart a worker every time it crashes. As far as I know, you don't have to select or configure anything. Whatever is in your jobs:work task will be executed as soon as it fails.
In the event that you are heavily dependent on background jobs in your web app. You could create a rake task that finds the last record to be updated and execute a background job to update it. Or perhaps automate the rake task to find the rest of the records that need updating, since the last crash.
Alternatively, you force worker restart manually as indicated in this article (using delayed_job):
heroku workers 0;
heroku workers 1;
Or perhaps you can restart a specific worker by doing (mentioned in this article):
heroku restart worker.1
By the way, try the 1.9 stack. Make sure your app is 1.9.2 compatible, before doing so. Hopefully crashes are less frequent there:
heroku stack:migrate bamboo-mri-1.9.2
In the event, that such issues still arise. Best to contact Heroku support. They are very responsive at what they do.
Latest command to restart a specific heroku web worker (2014):
heroku ps:restart web.1
(tested on Cedar stack)
At times, for instance in case of DB crashes, the worker may not restart automatically. you would need to do this.
heroku restart web.1
It worked for me.