Search code examples
pythoncelery

Celery / Flask update_state not changing state


My flask config looks like this

CELERY_BROKER_URL = "redis://127.0.0.1:6379/0"    
CELERY_RESULT_BACKEND = "redis://127.0.0.1:6379/0"    
CELERY_CACHE_BACKEND = "redis://127.0.0.1:6379/0"    
BROKER_URL = "redis://127.0.0.1:6379/0"    
RESULT_BACKEND = "redis://127.0.0.1:6379/0"    
CACHE_BACKEND = "redis://127.0.0.1:6379/0"

I've got so many as I'm really not sure if I should be doing the prefix CELERY_ or not or if i need the CACHE_BACKEND at all.

@celery.task(bind=True, name="apptask")
def apptask(self, oi):
    for x in range(40):
        sleep(1)
        self.update_state("Running", meta={"test":1, "val": oi})
    return current_app.name

This is my celery task

And this is the route that calls the task.

@users.route("/test", methods=["GET"])
def test():

    task = apptask.apply_async(args=["d"])
    ts = apptask.AsyncResult(task.id)
    print(ts.info)
    print(ts.status)
    

Celery is running and picks up the task call, the task executes and suceeds but I can never get to the meta data and the status doesn't appear to update with my custom state.

Status is anyways "PENDING" or "SUCCESS" and never "RUNNING" also ts.info is None.

Does anyone know why this might be? Am I missing a config, has something been set up wrong etc?


Solution

  • I've realised I missed out the kwarg state from the update_state call. Which must have been throwing an error or having unintended side effects.

    self.update_state(state="Running", meta={"test":1, "val": oi})

    This has fixed it.