Commit 25715fb8 authored by Gerion Entrup's avatar Gerion Entrup
Browse files

add schedu.py

implements a scheduler to call certain actions periodic
parent 0e1017b6
......@@ -15,7 +15,7 @@ from sqlalchemy.pool import StaticPool
from collector import Collector
from translator import Translator
from walker import Walker
from schedu import Schedu
class Main:
......@@ -69,8 +69,8 @@ class Main:
collector = Collector(self.session)
collector.start()
walker = Walker(self.session, arg.sourcedir)
walker.start()
schedu = Schedu(self.session, arg.sourcedir)
schedu.start()
#from model import Recording
#from mbdata.models import Release, ReleaseGroup, Track, Artist, ArtistCredit, ArtistCreditName, Medium
......
import threading
import time
import sched
import logging
import settings
from plumber import Action, Actions, Plumber
from walker import Walker
class Schedu(threading.Thread):
def __init__(self, session, sourcedir):
super().__init__(target=self)
self._session = session
self._sourcedir = sourcedir
self._plumber = Plumber(session)
self._plumber.start()
self._sched = sched.scheduler()
self._init_schedule()
def _scan(session, sourcedir):
walker = Walker(session, sourcedir)
walker.start()
walker.join()
def _clean():
Actions.put(Action.check_recordings)
def _repeat_every(self, delay, prio, action,
args=(), kwargs={}, blocking=False):
def wrapped_action(schd, delay, prio, action, args, kwargs, blocking):
logging.getLogger('schedu').info("execute: " + action.__name__)
t = time.time()
action(*args, **kwargs)
t = time.time() - t if blocking else 0
schd.enter(max(0, delay-t), prio, wrapped_action,
argument=(schd, delay, prio, action,
args, kwargs, blocking))
self._sched.enter(0, prio, wrapped_action,
argument=(self._sched, delay, prio, action,
args, kwargs, blocking))
def _init_schedule(self):
self._repeat_every(settings.scan_interval, 2, Schedu._scan,
args=(self._session, self._sourcedir),
blocking=True)
self._repeat_every(settings.clean_interval, 1, Schedu._clean)
def run(self):
self._sched.run()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment