From 9f9e60336540c042c63296e815eed91c8a27ba8c Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Sun, 7 Jul 2019 16:16:48 +0800 Subject: [PATCH] fixed https://github.com/tikazyq/crawlab/issues/78 --- crawlab/app.py | 2 +- crawlab/requirements.txt | 1 + crawlab/tasks/scheduler.py | 18 ++++++++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/crawlab/app.py b/crawlab/app.py index 493c9bed..484e9234 100644 --- a/crawlab/app.py +++ b/crawlab/app.py @@ -103,4 +103,4 @@ if not os.path.exists(PROJECT_LOGS_FOLDER): if __name__ == '__main__': # run app instance - app.run(host=FLASK_HOST, port=FLASK_PORT, threaded=True) + app.run(host=FLASK_HOST, port=FLASK_PORT) diff --git a/crawlab/requirements.txt b/crawlab/requirements.txt index 67c8d0db..282c4cc2 100644 --- a/crawlab/requirements.txt +++ b/crawlab/requirements.txt @@ -14,3 +14,4 @@ eventlet Celery Flower redis +gunicorn diff --git a/crawlab/tasks/scheduler.py b/crawlab/tasks/scheduler.py index 16597f67..b9fcb140 100644 --- a/crawlab/tasks/scheduler.py +++ b/crawlab/tasks/scheduler.py @@ -1,3 +1,6 @@ +import atexit +import fcntl + import requests from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.jobstores.mongodb import MongoDBJobStore @@ -65,8 +68,19 @@ class Scheduler(object): print(f'running: {self.scheduler.running}') def run(self): - self.update() - self.scheduler.start() + f = open("scheduler.lock", "wb") + try: + fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB) + self.update() + self.scheduler.start() + except: + pass + + def unlock(): + fcntl.flock(f, fcntl.LOCK_UN) + f.close() + + atexit.register(unlock) scheduler = Scheduler()