Flask and celery service
andremaytorena
PROOP

a year ago

Hey, I am trying to create a celery service so that I can run tasks in the background like sending emails or notifications, I looked at the django guide, but I am just a little confused on how it actually works.

Since a lot of the guides I see online are celery integrated into the actual Flask application so then you can run the celery function/task straight from the code. Does anyone know how I'd be able to set it up properly?

Solved

54 Replies

andremaytorena
PROOP

a year ago

f96fc6d1-3b42-4079-bec9-6c8bbc962296


a year ago

what specifically are you confused on that guide, id be happy to explain something you are stuck on in more depth


andremaytorena
PROOP

a year ago

I've set up the Redis instance, and then made a celery_app.py

from celery import Celery
from dotenv import load_dotenv
import os

load_dotenv()

def make_celery(app_name=__name__):
    return Celery(
        app_name,
        broker=os.environ['REDIS_URL'],
        backend=os.environ['REDIS_URL']
    )

celery = make_celery()

andremaytorena
PROOP

a year ago

I kinda just got this code online, haven't used celery before


a year ago

what command do you run locally to start celery?


andremaytorena
PROOP

a year ago

I was just running using python, that's how I run my flask dev server


a year ago

would I be correct in assuming you have celery running locally just fine?


andremaytorena
PROOP

a year ago

well this code all it does is just run and then stops, so no errors but not like it stays open


andremaytorena
PROOP

a year ago

So that's kind of what I'm unsure of


andremaytorena
PROOP

a year ago

would I also need to create another flask server to incorporate the celery service? but I guess that doesn't make sense


a year ago

nope, you'd have two services, one that runs only flask, and one that runs only celery


a year ago

but please get this working locally first, then once you got it working locally, I'll be here to help you run it on railway!


andremaytorena
PROOP

a year ago

cool


andremaytorena
PROOP

a year ago

Ok got it working on local


andremaytorena
PROOP

a year ago

Now trying to put it onto railway, and im getting this:
invalid type: string "celery -A tasks worker --loglevel=info", expected a map

that's the start command I'm using, or should I use this one:
celery -A liftoff worker -l info --concurrency=3

But then what would liftoff be?


a year ago

What command do you use locally to run just celery


andremaytorena
PROOP

a year ago

celery -A tasks worker --loglevel=info


andremaytorena
PROOP

a year ago

This one seems to work, so using it

1323411160107847700


a year ago

instead of a procfile, set the command in the service settings

1323411825471131600


andremaytorena
PROOP

a year ago

Just did, should I delete the procfile?

1323412150386954200


a year ago

yep


a year ago

you also need to attach a branch in the service settings

1323412309577830400


andremaytorena
PROOP

a year ago

Ok so done that, delete procfile and then added the command

1323413096865333200


andremaytorena
PROOP

a year ago

Also I noticed, when I push to github it's not pushing a new version onto railway with the /worker folder


andremaytorena
PROOP

a year ago

But the /app folder is pushign just fine when there is a change


a year ago

thats because you didnt have a branch connected up until a few minutes ago, that issue should be sorted out now


a year ago

when you run the celery command locally, do you do that from within the worker folder?


andremaytorena
PROOP

a year ago

yeah I do


a year ago

can you delete all the __pycache__ folders from your repo and then make sure they are being gitignored


andremaytorena
PROOP

a year ago

Deleted it and added to git ignore

1323416048829202400


a year ago

so now the issue is that isnt a typical python project layout, despite there being python files, nixpacks doesnt know that you want it to run python


mikexie360
PRO

a year ago

post the folder structure of the app, and the contents of the nixpacks?


a year ago

so add a requirements.txt file with all the dependencies you need, and we will see if that helps


a year ago

folder structure is as printed in that screenshot, and there is no nixpacks.toml file


andremaytorena
PROOP

a year ago

ok i believe this has worked

1323417570866495500


a year ago

sweet


andremaytorena
PROOP

a year ago

awesome ty, now just need to use it from my flask service


a year ago

now, you will absolutely need to set the concurrency, the default values will use a lot of memory


andremaytorena
PROOP

a year ago

ok let me do that now


andremaytorena
PROOP

a year ago

Any recommendations? or how should I know what to set it to


a year ago

set it to the numder of jobs you could have running at the same time, if you will only ever run one job at any one time, set it to 1


mikexie360
PRO

a year ago

would it be something like

celery -A app.celery_app worker --concurrency=1 --pool=prefork --loglevel=info --events

andremaytorena
PROOP

a year ago

what would happen if i try to run a job back to back? does that count as just 1?


andremaytorena
PROOP

a year ago

ty


a year ago

as long as they run back to back you're fine, since thats still 1 job at any one point in time


andremaytorena
PROOP

a year ago

ok cool


mikexie360
PRO

a year ago

I think it will do it one at a time, if you set the concurrency to one.

And it will do it depending on whats in the redis


a year ago

see the difference?

1323419416272306200


andremaytorena
PROOP

a year ago

oh yeah a lot nicer


a year ago

awsome, any other questions?


andremaytorena
PROOP

a year ago

that should be it for now!


andremaytorena
PROOP

a year ago

thank you


a year ago

no problem, ill mark this as solved, but feel free to open another thread if you need further help!


a year ago

!s


Status changed to Solved brody about 1 year ago


Loading...