Skip to content

Commit

Permalink
remove CELERY_ABOUT_TO_SHUTDOWN_FLAG
Browse files Browse the repository at this point in the history
  • Loading branch information
jokester committed Mar 3, 2024
1 parent 40a45ba commit e8369f7
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 32 deletions.
29 changes: 0 additions & 29 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os

from celery import Celery
from celery.signals import worker_shutting_down
from flask import Flask, g, request
from flask_apikit import APIKit
from flask_babel import Babel
Expand Down Expand Up @@ -118,18 +117,7 @@ def create_app():
return app


CELERY_ABOUT_TO_SHUTDOWN_FLAG = "CELERY_ABOUT_TO_SHUTDOWN_FLAG"


def delete_about_to_shutdown_flag():
try:
os.rmdir(CELERY_ABOUT_TO_SHUTDOWN_FLAG)
except Exception:
pass


def create_celery() -> Celery:
delete_about_to_shutdown_flag()
# 为celery创建app
app = Flask(__name__)
app.config.from_envvar(
Expand Down Expand Up @@ -166,23 +154,6 @@ def create_celery() -> Celery:
celery = create_celery()


def create_about_to_shutdown_flag():
try:
os.mkdir(CELERY_ABOUT_TO_SHUTDOWN_FLAG)
except Exception:
pass


@worker_shutting_down.connect
def when_shutdown(**kwargs):
create_about_to_shutdown_flag()


def about_to_shutdown():
"""检测 Celery 是否将要关闭"""
return os.path.isdir(CELERY_ABOUT_TO_SHUTDOWN_FLAG)


@babel.localeselector
def get_locale():
current_user = g.get("current_user")
Expand Down
6 changes: 3 additions & 3 deletions app/tasks/ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from uuid import uuid4

import requests
from app import celery, gs_vision, oss, about_to_shutdown
from app import celery, gs_vision, oss
from app.constants.file import FileType, ImageOCRPercent, ParseErrorType, ParseStatus
from app.models import connect_db
from app.tasks import SyncResult
Expand Down Expand Up @@ -416,6 +416,8 @@ def inc(self, id, error_name, step=1):


def merge_and_ocr(parsing_images, /, *, parse_alone=False):
if 2 > 1:
raise NotImplementedError("OCR under reconstruction")
if not parsing_images:
return
oss_file_prefix = celery.conf.app_config["OSS_FILE_PREFIX"]
Expand Down Expand Up @@ -532,8 +534,6 @@ def merge_and_ocr(parsing_images, /, *, parse_alone=False):
parse_error_type=ParseErrorType.IMAGE_OCR_SERVER_DISCONNECT,
)
continue
if about_to_shutdown() is True:
raise AboutToShutdownError
for image_data in merged_images_data:
image = image_data["image"]
image.update(image_ocr_percent=ImageOCRPercent.LABELING)
Expand Down

0 comments on commit e8369f7

Please sign in to comment.