Django using Celery not performing my task

140
March 21, 2021, at 03:40 AM

I have been trying for the past 3 days to get django to perfom asynchronous tasks on a periodic schedule. Came across Celery which seemed like a perfect match for my problem but the more I read online the more problems there seemed to exist that I had prepared. Anyway I was able to vaguely glue up together pieces of information to finally come up with a "somewhat solution" because there were so many deprecated modules and methods it was hard to find a working example from start to finish. So I am trying to create a task which will create a certain model object periodically (let's say 30 seconds for sake of the argument). So I installed redis as the "broker", (so I saw fit in other discussions) and managed to set up my task up and running but the problem is that even when it executes, no new model is created, I suspect that it might be cause of maybe a misconfiguration between the local and the settings time-zone but frankly I'm not sure, supposedly it should be able to create new instance when run through django-celery-beat tables in the admin panel

my __init__.py:
from __future__ import absolute_import, unicode_literals 
from .celery import app as celery_app 
__all__ = ('celery_app',)

celery.py in the base directory:
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'firstApp.settings')
app = Celery('myProject')

app.config_from_object('django.conf:settings')
app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

my settings.py configuration:
BROKER_URL = 'redis://localhost:6379'
CELERY_TIMEZONE = 'EUROPE/London'
CELERY_BEAT_SCHEDULE = {
    'Every Minute' : {
        'task' : 'main.tasks.Check_State_Up',
        'schedule' : 30,
         
    }
}
note, 'main' is the name of my app
my tasks.py in main app:
from celery import shared_task 
from django.shortcuts import get_object_or_404
import random 
from .models import CheckUp, Company

@shared_task
def Check_State_Up():
    parentInstance = get_object_or_404(ParentModel, symbol='Symbol')
    def create(parent):
        Widget.objects.create(parent=parent, randomValue=random.randint(100,250))
    create(parentInstance)
Note, Parent model has a name and symbol value and widget has a parent, (foreign key) and a random value let's keep in mind that this function works totally fine when called in shell, I just posted it for demostration

Can anyone help me understand what is going wrong here?

Also my logs from celery

celery -A myProject beat -l Info 
celery beat v5.0.5 (singularity) is starting.
__    -    ... __   -        _
LocalTime -> 2021-03-20 18:17:58
Configuration ->
    . broker -> redis://localhost:6379//
    . loader -> celery.loaders.app.AppLoader
    . scheduler -> celery.beat.PersistentScheduler
    . db -> celerybeat-schedule
    . logfile -> [stderr]@%INFO
    . maxinterval -> 5.00 minutes (300s)
[2021-03-20 18:17:58,033: INFO/MainProcess] beat: Starting...
it also stops and starting...

celery -A myProject worker -l info

 -------------- celery@myDevice v5.0.5 (singularity)
--- ***** -----
-- ******* ---- Windows-10-10.0.18362-SP0 2021-03-20 18:17:50
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app:         myProject:0x1502743f3d0
- ** ---------- .> transport:   redis://localhost:6379//
- ** ---------- .> results:     disabled://
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery

[tasks]
  . myProject.celery.debug_task
  . main.tasks.Check_State_Up
[2021-03-20 18:17:51,102: INFO/MainProcess] Connected to redis://localhost:6379//
[2021-03-20 18:17:51,130: INFO/MainProcess] mingle: searching for neighbors
[2021-03-20 18:17:51,904: INFO/SpawnPoolWorker-1] child process 1852 calling self.run()
[2021-03-20 18:17:51,922: INFO/SpawnPoolWorker-3] child process 8424 calling self.run()
[2021-03-20 18:17:51,955: INFO/SpawnPoolWorker-2] child process 8600 calling self.run()
[2021-03-20 18:17:52,002: INFO/SpawnPoolWorker-4] child process 14840 calling self.run()
note

the child process 1000 calling self.run() executes only when initated from the admin panel manually, not when started. I hope this is thorough enough to understand. Thank you.

ALSO:
Django==3.1.7
django-celery-beat==2.2.0
celery==5.0.5
redis==3.5.3
READ ALSO
Nextcloud: upload large files

Nextcloud: upload large files

I have a Nextcloud 18 on Apache/PHP 73, behind a reverse proxy (out of my control)

186
Getting the contents of more than one DB table in Spring

Getting the contents of more than one DB table in Spring

I'm trying make an endpoint to get the content of two independent tables from my DB using Spring but I've got no idea how to do it

141
How to rearrange DataTables controls

How to rearrange DataTables controls

Hi I want to rearrange the positions of elements of datatables and also want to add one button

113