Writing custom remote commands for Celery workers
There are a lot of celery commands allowing you to monitor task/worker statuses. It’s really easy to extend that list with your custom command and we’ll do that in order to get some extra monitoring for any task launched. I suppose that you already have Flower configured and running and it runs on each of your workers hosted on AWS. You can definitely update the code below according to your needs and make it work for your case as well. So what we are trying to accomplish: we want to be able go to the Flower UI task tracking page right away after triggering task execution. For that we will implement a custom worker control command which will respond with its IP address (assuming dynamic infrastructure, so you cannot just hardcode it within config/inventory file). In order to retrieve instance’s IP address we will use instance metadata endpoint. This url
http://169.254.169.254/latest/meta-data/public-ipv4 allows us to retrieve external IP address of an instance from which this request is being executed. Wrapping this up as a custom command function
The only helper function we need then is the one which will generate a url for the status page. First we broadcast our custom command to first worker we found active and based on its response we format a url template with proper IP address and task ID.
Note that this code works even with Flower Basic Auth being enabled. Just make sure you store this credentials within local config/environment variable.
Now let’s see usage of this in an example
from tasks import my_task
NOTE: Chrome and other browsers dropped support for embedded credentials in URIs to comply with RFC 3986, so pasting URL in your address bar will not automatically log you in (in case you are using basic auth), but you can easily embed this url into a template and allow users to click on that with seamless login.
If you are using Django framework you often trigger background tasks on some user actions. It’s pretty convenient to provide a link for monitoring task status back to them via flash message. You can do it easily like this
from django.contrib import messages
That’s it for today, have a great weekend and see you soon.