I'm currently learning Django and Celery. So I have to do a feature to a Django projet, and I use Celery for that : I need to constantly check a list in my Redis
server.
And for that, I use Celery but I don't know how I can implement correctly that and I opted for a while True:
.
After the launching of my task with celery -A <my_project> worker -l INFO
, it's impossible to kill the task (the CTRL + C
doesn't work), or I don't found how I can. I conclude it's not a good method for doing what I want.
One more thing : I know the extension django-celery-beat
exists but I think it's "weird" to open another terminal for launch this extension. Already I have to launch my django server, then my worker (with celery) on two different terminals, I have to open a third to launch my scheduled task, I don't know if this is really weird to do that, but... I don't know, I think it's weird to do that but I don't know if it really is.
Here's my (unique) task in an app
in my django project :
from celery import shared_task
import redis, time
@shared_task
def watcher():
client = redis.Redis('localhost', port=6379, decode_responses=True)
queue = 'queue'
lock_key = 'watcher_key'
try:
client.delete(queue)
if client.get(lock_key):
print('The matchmaking is already launch.')
return
client.set(lock_key, 'true')
while True:
in_queue = client.lrange(queue, 0, -1)
print(f'\'{queue}\': {in_queue}')
time.sleep(1)
# don't even know what condition I can put for breaking the loop
except Exception as e:
print(f'ERROR REDIS: {e}')
return
finally:
client.delete(lock_key)
Problem : I don't know/found how I can constantly check a list in my Redis
server with THE good method, if the django-celery-beat
is the only solution, I'll go with it.
So, I invoke your help please !
Thank everyone in advance !
That's correct, we don't actually want run the command for the celery worker. Theoretically you could have it running in a Docker or Kubernetes container but you probably don't wanna do anything like that.
The best way to run the worker is to setup the schedule using Django's default administration. You're going to need to make sure to migrate the django-celery-beat
models so that you can setup different types of tasks in the adminstration.
To setup your default administration, please follow page 2 of the tutorial and refer to the Django admin site documentation as needed.
After you have it setup, you should be able to setup your tasks based on all sorts of events. By timers, specific times, or even sunrise.
To make sure the functions you setup in celery.py
can be used in the default administration, make sure to add the decorator @shared_task
. Here's an example:
# Runs once everyday to check if there are any subscriptions to be updated
# SecureSign.celery.task_check_free_trial
from celery import shared_task
@shared_task(expires=45)
def task_check_free_trial():
print('Checking Free Trial...')
morningCheckFreeTrial()
To setup a periodic task, login and add your task where the Model for periodic tasks shows and add in your desired period of time to rerun the task or select 'one-time task'. When putting in the celery task name, refer to it as follows:
DjangoMainAppNameOrFolderName.CeleryFile.Function
I like to comment this right above the function so you know what to put into the administration when you setup your tasks. As you can see, mine is named :
SecureSign.celery.task_check_free_trial
Good luck, let me know if you have any questions!