我尝试在 Railway.app 上每 30 秒运行一次 Python 脚本,但它不起作用。我在我的项目中尝试使用下面的代码,但在我看来,有一些问题不能很好地工作。 main.py 文件正常运行一次,但是每 30 秒的 cron 功能不起作用。
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 <[email protected]>"]
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}"}
根据他们的文档允许的最小时间间隔是15分钟。
连续执行 cron 作业的最短时间不能少于 15 分钟。