Cron Job不在Docker Fastapi Docker应用程序中运行

发布于 2025-02-07 15:49:07 字数 1921 浏览 0 评论 0原文

我正在为Homelab制作一个应用程序,在那里我有一个Intel NUC,我正在用于某些网络刮擦任务。我的家庭网络可以通过192.xxx.x.xx访问NUC。

在nuc上,我已经设置了nginx,以向docker容器代理http请求。在那个容器中,我有一个基本fastapi应用程序来处理请求。

app.main.py

import os
from pathlib import Path
from fastapi import FastAPI

app = FastAPI()
cron_path = Path(os.getcwd(), "app", "cron.log")

@app.get("/cron")
def cron():
    with cron_path.open("rt") as cron:
        return {"cron_state": cron.read().split("\n")}

app.cronjob.py

import os
from pathlib import Path
from datetime import datetime

cron_path = Path(os.getcwd(), "app", "cron.log")

def append_time():
    with cron_path.open("rt") as filein:
        text = filein.read()
        text += f"\n{datetime.utcnow().strftime('%Y-%m%dT%H:%M:%SZ')}"
        
    with cron_path.open("wt") as fileout:
        fileout.write(text)



if __name__ == "__main__":
    append_time()

cron-job

* * * * * python3 /code/app/cronjob.py
# An empty line is required at the end of this file for a valid cron file.

dockerfile

FROM python:3.10-slim-buster

# 
WORKDIR /code

COPY ./cron-job /etc/cron.d/cron-job
COPY ./app /code/app
COPY ./requirements.txt /code/requirements.txt

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/cron-job
#Install Cron
RUN apt-get update
RUN apt-get -y install cron
# Apply cron job
RUN crontab /etc/cron.d/cron-job

# 
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

# 
EXPOSE 8080

CMD crontab ; uvicorn app.main:app --host 0.0.0.0 --port 8080

我可以毫无问题访问该应用程序,但是我似乎无法在Fastapi运行时运行Cron的工作。我想做的是做一些更适合纯Python解决方案的事情,例如Fastapi_utils.tasks 导入repote_every,或者我缺少一些东西。

I'm working on an app for my homelab, where I have an Intel NUC I'm using for some web scraping tasks. The NUC is accessible to my home network via 192.xxx.x.xx.

On the NUC I've set up nginx to proxy incoming http request to a docker container. In that container I've got a basic fastapi app to handle the request.

app.main.py

import os
from pathlib import Path
from fastapi import FastAPI

app = FastAPI()
cron_path = Path(os.getcwd(), "app", "cron.log")

@app.get("/cron")
def cron():
    with cron_path.open("rt") as cron:
        return {"cron_state": cron.read().split("\n")}

app.cronjob.py

import os
from pathlib import Path
from datetime import datetime

cron_path = Path(os.getcwd(), "app", "cron.log")

def append_time():
    with cron_path.open("rt") as filein:
        text = filein.read()
        text += f"\n{datetime.utcnow().strftime('%Y-%m%dT%H:%M:%SZ')}"
        
    with cron_path.open("wt") as fileout:
        fileout.write(text)



if __name__ == "__main__":
    append_time()

cron-job

* * * * * python3 /code/app/cronjob.py
# An empty line is required at the end of this file for a valid cron file.

Dockerfile

FROM python:3.10-slim-buster

# 
WORKDIR /code

COPY ./cron-job /etc/cron.d/cron-job
COPY ./app /code/app
COPY ./requirements.txt /code/requirements.txt

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/cron-job
#Install Cron
RUN apt-get update
RUN apt-get -y install cron
# Apply cron job
RUN crontab /etc/cron.d/cron-job

# 
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

# 
EXPOSE 8080

CMD crontab ; uvicorn app.main:app --host 0.0.0.0 --port 8080

I can access the the app without issues, but I can't seem to get the cron job to run while fastapi is running. Is what I'm attemping to do something better suited for pure python solution like from fastapi_utils.tasks import repeat_every or is there something I'm missing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文