Skip to content

Commit

Permalink
cron job: check site reachable
Browse files Browse the repository at this point in the history
  • Loading branch information
Her Email committed Nov 10, 2023
1 parent 6793d54 commit 1d2b881
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions common/management/commands/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from catalog.jobs import * # noqa
from common.models import JobManager
from mastodon.jobs import * # noqa
from users.jobs import * # noqa


Expand Down
40 changes: 40 additions & 0 deletions mastodon/jobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from datetime import timedelta

from django.utils import timezone
from loguru import logger

from common.models import BaseJob, JobManager
from mastodon.api import detect_server_info
from mastodon.models import MastodonApplication


@JobManager.register
class MastodonSiteCheck(BaseJob):
max_unreachable_days = 31

def run(self):
logger.info("Mastodon Site Check start.")
count_checked = 0
count_unreachable = 0
count_disabled = 0
for site in MastodonApplication.objects.exclude(disabled=True):
domain = None
count_checked += 1
try:
domain, api_domain, v = detect_server_info(site.domain_name)
site.last_reachable_date = timezone.now()
except:
logger.warning(f"Failed to detect server info for {site.domain_name}")
count_unreachable += 1
if site.last_reachable_date is None:
site.last_reachable_date = timezone.now() - timedelta(days=1)
if timezone.now() > site.last_reachable_date + timedelta(
days=self.max_unreachable_days
):
site.disabled = True
count_disabled += 1
finally:
site.save(update_fields=["last_reachable_date", "disabled"])
logger.info(
f"Mastodon Site Check finished, {count_checked} checked, {count_unreachable} unreachable, {count_disabled} disabled."
)

0 comments on commit 1d2b881

Please sign in to comment.