diff --git a/Dockerfile b/Dockerfile index 30c7563..e4b7e7c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,6 @@ RUN apt-get update && \ COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt -COPY lib ./lib COPY scansmb.py . ENTRYPOINT [ "python", "./scansmb.py" ] diff --git a/lib/repeatedtimer.py b/lib/repeatedtimer.py deleted file mode 100644 index 2d76c70..0000000 --- a/lib/repeatedtimer.py +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env python3 -""" -From: https://stackoverflow.com/a/40965385 -Extended to prevent simultaneous runs -""" - -import threading -import time - - -class RepeatedTimer(object): - def __init__(self, interval, function, *args, **kwargs): - self._timer = None - self.interval = interval - self.function = function - self.args = args - self.kwargs = kwargs - self.is_running = False - self.next_call = time.time() - # Determine if we should actually run the method again - # The run function will set this to true after it is finished - self.run = True - self.start() - - def _run(self): - self.is_running = False - self.start() - if self.run: - self.run = False - self.function(*self.args, **self.kwargs) - self.run = True - - def start(self): - if not self.is_running: - self.next_call += self.interval - self._timer = threading.Timer( - self.next_call - time.time(), self._run) - self._timer.start() - self.is_running = True - - def stop(self): - self._timer.cancel() - self.is_running = False diff --git a/requirements.txt b/requirements.txt index b0f5b5b..f110cfa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ pysmbc -configargparse \ No newline at end of file +configargparse +apscheduler diff --git a/scansmb.py b/scansmb.py index 8788ea1..eebc4a3 100644 --- a/scansmb.py +++ b/scansmb.py @@ -6,13 +6,13 @@ import re from datetime import datetime import logging +from apscheduler.schedulers.background import BlockingScheduler import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication import configargparse import smbc -import lib.repeatedtimer as rt logging.basicConfig(level=logging.DEBUG) @@ -134,7 +134,9 @@ def main(): logger.info(parser.format_values()) loop(ctx, options) - timer = rt.RepeatedTimer(60, loop, ctx, options) + scheduler = BlockingScheduler() + scheduler.add_job(loop, 'interval', minutes=1, args=[ctx, options]) + scheduler.start() if __name__ == "__main__":