Skip to content

Commit

Permalink
fix: imports
Browse files Browse the repository at this point in the history
  • Loading branch information
xgui3783 committed Aug 23, 2024
1 parent 5ae5843 commit 140f015
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/common/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .decorators import data_decorator, router_decorator, async_router_decorator, name_to_fns_map
from .siibra_api_typing import ROLE_TYPE
from .logger import logger, access_logger
from .logger import logger as general_logger, access_logger
from .exceptions import *
from .storage import get_filename
40 changes: 40 additions & 0 deletions api/common/timer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from threading import Timer
from typing import List, Callable

class RepeatTimer(Timer):
"""RepeatTimer
Expand All @@ -8,3 +9,42 @@ class RepeatTimer(Timer):
def run(self):
while not self.finished.wait(self.interval):
self.function(*self.args, **self.kwargs)

class Cron:
def __init__(self) -> None:
self._minutely_fns: List[Callable] = []
self._ten_minutely_fns: List[Callable] = []

self._timers: List[RepeatTimer] = [
RepeatTimer(60, self._run_minutely),
RepeatTimer(600, self._run_ten_minutely)
]

def _run_minutely(self):
for fn in self._minutely_fns:
fn()

def _run_ten_minutely(self):
for fn in self._ten_minutely_fns:
fn()

def minutely(self, fn: Callable):
self._minutely_fns.append(fn)
return fn

def ten_minutely(self, fn: Callable):
self._ten_minutely_fns.append(fn)
return fn

def run_all(self):
self._run_ten_minutely()
self._run_minutely()

def start(self):
for timer in self._timers:
timer.start()

def stop(self):
"""On terminate"""
for timer in self._timers:
timer.cancel()

0 comments on commit 140f015

Please sign in to comment.