From bafe1d54380790937446d68307c0ec3e41d1049e Mon Sep 17 00:00:00 2001 From: Cyberes Date: Tue, 17 Sep 2024 21:13:16 -0600 Subject: [PATCH] use systemd watchdog --- archiver.py | 5 ++++- iviewer/__init__.py | 0 requirements.txt | 3 ++- systemd/imaparchiver.service | 3 ++- systemd/imapviewer.service | 2 +- 5 files changed, 9 insertions(+), 4 deletions(-) delete mode 100644 iviewer/__init__.py diff --git a/archiver.py b/archiver.py index c6e88e0..220f57e 100755 --- a/archiver.py +++ b/archiver.py @@ -7,6 +7,7 @@ from pathlib import Path import humanize import yaml +from sdnotify import SystemdNotifier from iarchiver.database import EmailDatabase from iarchiver.email import unix_timestamp_since_to_imap_timestamp @@ -33,6 +34,7 @@ def main(args): attachments_dir = Path(config['attachments_path']) attachments_dir.mkdir(parents=True, exist_ok=True) + notifier = SystemdNotifier() database = EmailDatabase(Path(config['database_path'])) mail = MailConnection(config['server'], config['username'], config['password'], attachments_dir, save_inline_attachments=save_inline_attachments) mail.load_folders() @@ -43,12 +45,12 @@ def main(args): num_folders_to_sync = len(mail.folder_structure) logger.info(f'Syncing {num_folders_to_sync} folders...') + last_refresh_imap_date = None if args.full: last_refresh = None logger.info('Performing full sync...') else: last_refresh = database.have_we_done_a_full_sync_at_all() - last_refresh_imap_date = None if last_refresh: last_refresh_imap_date = unix_timestamp_since_to_imap_timestamp(last_refresh) logger.info(f'Syncing emails received after {last_refresh_imap_date}') @@ -76,6 +78,7 @@ def main(args): new_emails += 1 if len(attachments): new_attachments += 1 + notifier.notify("WATCHDOG=1") elapsed = datetime.now() - sync_start_time database.finish_sync('refresh' if not did_full_sync else 'full', new_emails, new_attachments, int(elapsed.total_seconds())) diff --git a/iviewer/__init__.py b/iviewer/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/requirements.txt b/requirements.txt index 2732923..0926c72 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,5 @@ humanize==4.9.0 mmh3==4.1.0 flask==3.0.2 python-magic==0.4.27 -flask_httpauth==4.8.0 \ No newline at end of file +flask_httpauth==4.8.0 +sdnotify==0.3.2 \ No newline at end of file diff --git a/systemd/imaparchiver.service b/systemd/imaparchiver.service index ac99a9f..c8213ca 100644 --- a/systemd/imaparchiver.service +++ b/systemd/imaparchiver.service @@ -8,7 +8,8 @@ User=emailsync Group=emailsync ExecStart=/srv/email/imap-archiver/venv/bin/python3 /srv/email/imap-archiver/run.py --config /srv/email/imap-archiver/config.yml SyslogIdentifier=imap-archiver -Restart=on-failure +Restart=always +WatchdogSec=60 [Install] WantedBy=multi-user.target diff --git a/systemd/imapviewer.service b/systemd/imapviewer.service index e2963a3..fe8b05b 100644 --- a/systemd/imapviewer.service +++ b/systemd/imapviewer.service @@ -9,7 +9,7 @@ Group=emailsync EnvironmentFile=/etc/secrets/imapviewer ExecStart=/srv/email/imap-archiver/venv/bin/python3 /srv/email/imap-archiver/server.py SyslogIdentifier=imap-viewer -Restart=on-failure +Restart=always [Install] WantedBy=multi-user.target