Flask and celery service
andremaytorena
PROOP

2 years 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

2 years ago

f96fc6d1-3b42-4079-bec9-6c8bbc962296


2 years ago

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


andremaytorena
PROOP

2 years 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

2 years ago

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


2 years ago

what command do you run locally to start celery?


andremaytorena
PROOP

2 years ago

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


2 years ago

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


andremaytorena
PROOP

2 years ago

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


andremaytorena
PROOP

2 years ago

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


andremaytorena
PROOP

2 years ago

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


2 years ago

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


2 years 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

2 years ago

cool


andremaytorena
PROOP

2 years ago

Ok got it working on local


andremaytorena
PROOP

2 years 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?


2 years ago

What command do you use locally to run just celery


andremaytorena
PROOP

2 years ago

celery -A tasks worker --loglevel=info


andremaytorena
PROOP

2 years ago

This one seems to work, so using it

1323411160107847711


2 years ago

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

1323411825471131648


andremaytorena
PROOP

2 years ago

Just did, should I delete the procfile?

1323412150386954271


2 years ago

yep


2 years ago

you also need to attach a branch in the service settings

1323412309577830461


andremaytorena
PROOP

2 years ago

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

1323413096865333289


andremaytorena
PROOP

2 years ago

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


andremaytorena
PROOP

2 years ago

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


2 years ago

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


2 years ago

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


andremaytorena
PROOP

2 years ago

yeah I do


2 years ago

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


andremaytorena
PROOP

2 years ago

Deleted it and added to git ignore

1323416048829202503


2 years 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

2 years ago

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


2 years ago

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


2 years ago

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


andremaytorena
PROOP

2 years ago

ok i believe this has worked

1323417570866495508


2 years ago

sweet


andremaytorena
PROOP

2 years ago

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


2 years ago

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


andremaytorena
PROOP

2 years ago

ok let me do that now


andremaytorena
PROOP

2 years ago

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


2 years 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

2 years ago

would it be something like

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

andremaytorena
PROOP

2 years ago

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


andremaytorena
PROOP

2 years ago

ty


2 years 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

2 years ago

ok cool


mikexie360
PRO

2 years 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


2 years ago

see the difference?

1323419416272306176


andremaytorena
PROOP

2 years ago

oh yeah a lot nicer


2 years ago

awsome, any other questions?


andremaytorena
PROOP

2 years ago

that should be it for now!


andremaytorena
PROOP

2 years ago

thank you


2 years ago

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


2 years ago

!s


Status changed to Solved brody over 1 year ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...