use systemd watchdog

This commit is contained in:
Cyberes 2024-09-17 21:13:16 -06:00
parent 5a52fdca9c
commit bafe1d5438
5 changed files with 9 additions and 4 deletions

View File

@ -7,6 +7,7 @@ from pathlib import Path
import humanize import humanize
import yaml import yaml
from sdnotify import SystemdNotifier
from iarchiver.database import EmailDatabase from iarchiver.database import EmailDatabase
from iarchiver.email import unix_timestamp_since_to_imap_timestamp 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 = Path(config['attachments_path'])
attachments_dir.mkdir(parents=True, exist_ok=True) attachments_dir.mkdir(parents=True, exist_ok=True)
notifier = SystemdNotifier()
database = EmailDatabase(Path(config['database_path'])) database = EmailDatabase(Path(config['database_path']))
mail = MailConnection(config['server'], config['username'], config['password'], attachments_dir, save_inline_attachments=save_inline_attachments) mail = MailConnection(config['server'], config['username'], config['password'], attachments_dir, save_inline_attachments=save_inline_attachments)
mail.load_folders() mail.load_folders()
@ -43,12 +45,12 @@ def main(args):
num_folders_to_sync = len(mail.folder_structure) num_folders_to_sync = len(mail.folder_structure)
logger.info(f'Syncing {num_folders_to_sync} folders...') logger.info(f'Syncing {num_folders_to_sync} folders...')
last_refresh_imap_date = None
if args.full: if args.full:
last_refresh = None last_refresh = None
logger.info('Performing full sync...') logger.info('Performing full sync...')
else: else:
last_refresh = database.have_we_done_a_full_sync_at_all() last_refresh = database.have_we_done_a_full_sync_at_all()
last_refresh_imap_date = None
if last_refresh: if last_refresh:
last_refresh_imap_date = unix_timestamp_since_to_imap_timestamp(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}') logger.info(f'Syncing emails received after {last_refresh_imap_date}')
@ -76,6 +78,7 @@ def main(args):
new_emails += 1 new_emails += 1
if len(attachments): if len(attachments):
new_attachments += 1 new_attachments += 1
notifier.notify("WATCHDOG=1")
elapsed = datetime.now() - sync_start_time 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())) database.finish_sync('refresh' if not did_full_sync else 'full', new_emails, new_attachments, int(elapsed.total_seconds()))

View File

View File

@ -4,4 +4,5 @@ humanize==4.9.0
mmh3==4.1.0 mmh3==4.1.0
flask==3.0.2 flask==3.0.2
python-magic==0.4.27 python-magic==0.4.27
flask_httpauth==4.8.0 flask_httpauth==4.8.0
sdnotify==0.3.2

View File

@ -8,7 +8,8 @@ User=emailsync
Group=emailsync Group=emailsync
ExecStart=/srv/email/imap-archiver/venv/bin/python3 /srv/email/imap-archiver/run.py --config /srv/email/imap-archiver/config.yml ExecStart=/srv/email/imap-archiver/venv/bin/python3 /srv/email/imap-archiver/run.py --config /srv/email/imap-archiver/config.yml
SyslogIdentifier=imap-archiver SyslogIdentifier=imap-archiver
Restart=on-failure Restart=always
WatchdogSec=60
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View File

@ -9,7 +9,7 @@ Group=emailsync
EnvironmentFile=/etc/secrets/imapviewer EnvironmentFile=/etc/secrets/imapviewer
ExecStart=/srv/email/imap-archiver/venv/bin/python3 /srv/email/imap-archiver/server.py ExecStart=/srv/email/imap-archiver/venv/bin/python3 /srv/email/imap-archiver/server.py
SyslogIdentifier=imap-viewer SyslogIdentifier=imap-viewer
Restart=on-failure Restart=always
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target