0

I'm trying to run a Python script on Railway.app, every 30 seconds, but it's not working. I tried with the codes below inside my project, but it seems to me that there is something wrong that doesn't work well. The main.py file runs normally once, but the cron function every 30 seconds is not working.

railway.json:

{
  "$schema": "https://railway.app/railway.schema.json",
  "build": {
    "builder": "NIXPACKS"
  },
  "deploy": {
    "startCommand": "uvicorn main:app --host 0.0.0.0 --port $PORT",
    "restartPolicyType": "ON_FAILURE",
    "restartPolicyMaxRetries": 10
  },
  "cron": {
    "job": "*/30 * * * * *",
    "command": "/bin/bash $APP_PATH/cron.sh"
  }
}

pyproject.toml:

[tool.poetry]
name = "railway-fastapi-template"
version = "0.1.0"
description = "This example starts up a FastAPI server"
authors = ["DeviousLab <deviouslab@gmail.com>"]
license = "MIT"

[tool.poetry.dependencies]
python = "^3.10"
fastapi = "^0.80.0"
uvicorn = "^0.18.3"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

cron.sh

cd /app

python main.py

main.py:

from fastapi import FastAPI
from pydantic import BaseModel
import datetime

hora = datetime.datetime.now()

string_hora = "Executando o script em: " + str(hora)

app = FastAPI()

class Msg(BaseModel):
    msg: str

@app.get("/")
async def root():
    return {"message": f"{string_hora}"}
  • Why are you trying to run a FastAPI application as a cron job? The whole point of a web server is that you start it and leave it running "forever". – M.O. Mar 30 '23 at 14:38
  • @M.O. I want to create some requests inside the main.py file that are updated when the API returns. – Lucas Thaynan Mar 30 '23 at 15:20

0 Answers0