Skip to content

Commit

Permalink
feature: Hide cron feature
Browse files Browse the repository at this point in the history
  • Loading branch information
PindleskinY committed Nov 20, 2020
1 parent 11c2ea4 commit 1fa9180
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/flask_state/controller/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
from ..services.host_status import query_flask_state_host, record_flask_state_host
from ..utils.auth import auth_method, auth_user
from ..utils.constants import HttpMethod
from ..utils.cron import Cron
from ..utils.file_lock import Lock
from ..utils.format_conf import format_address
from ..utils.logger import DefaultLogger, logger
from .response_methods import make_response_content


def init_app(app, interval=180, log_instance=None):
def init_app(app, interval=60, log_instance=None):
"""
Plugin entry
:param app: Flask app
Expand All @@ -40,16 +39,20 @@ def init_app(app, interval=180, log_instance=None):
init_redis(app)
model_init_app(app)

step = int(interval / 60) if int(interval) > 60 else 1
minutes_array = [i for i in range(0, 60, step)]
minutes = ""
for i in minutes_array:
minutes += str(i) + ","
if not isinstance(interval, int):
raise TypeError(
ErrorMsg.DATA_TYPE_ERROR.get_msg(
".The target type is {}, not {}".format(int.__name__, type(interval).__name__)
)
)

# Timing recorder
t = threading.Thread(
target=record_timer,
args=(app, minutes[:-1]),
args=(
app,
interval,
),
)
t.setDaemon(True)
t.start()
Expand All @@ -75,18 +78,22 @@ def init_db(app):
)


def record_timer(app, minutes="0-59", days="1-31", hours="0-23", second="0"):
def record_timer(app, interval):
app.lock_flask_state = Lock.get_file_lock()
with app.app_context():
try:
current_app.lock_flask_state.acquire()
logger.info(InfoMsg.ACQUIRED_LOCK.get_msg(". process ID: %d" % os.getpid()))

s = sched.scheduler(time.time, time.sleep)
cron = Cron(days=days, hours=hours, minutes=minutes, second=second)
in_time = time.time()
target_time = int(int((time.time()) / 60 + 1) * 60)
time.sleep(60 - in_time % 60)
record_flask_state_host(interval)
while True:
target_time = cron.get()
s.enterabs(target_time, 1, record_flask_state_host, (60,))
target_time += interval
now_time = time.time()
s.enter(target_time - now_time, 1, record_flask_state_host, (interval,))
s.run()
except BlockingIOError:
pass
Expand Down

0 comments on commit 1fa9180

Please sign in to comment.