Skip to content

Commit

Permalink
add reporting for Healthchecks
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesob committed Sep 18, 2023
1 parent e184236 commit defd05f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions yacron/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class ConfigError(Exception):
"shell": "/bin/sh",
"command": None,
},
"healthchecks": {
"ping_url": None,
},
}


Expand Down Expand Up @@ -173,6 +176,11 @@ class ConfigError(Exception):
"command": Str() | Seq(Str()),
}
),
Opt("healthchecks"): Map(
{
"ping_url": Str(),
}
),
}
)

Expand Down
27 changes: 27 additions & 0 deletions yacron/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import Any, Dict, List, Optional, Tuple
import subprocess

import aiohttp
import sentry_sdk
import sentry_sdk.utils

Expand Down Expand Up @@ -161,6 +162,31 @@ async def report(
body, level=config.get("level", "error")
)

class HealthchecksReporter(Reporter):
async def report(
self, success: bool, job: 'RunningJob', config: Dict[str, Any]
) -> None:
config = config['healthchecks']
# Exit early if not successful since Healthchecks only receives positive events.
if not success or not config['ping_url']:
return

url = config['ping_url']
bad_req = False
try:
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
txt = await resp.text()
if txt != 'OK':
logger.debug("Got unexpected response from %s: %r", url, txt)
bad_req = True
except Exception:
bad_req = True
logger.exception("Error GETing %s", config['ping_url'])

if bad_req:
return


class MailReporter(Reporter):
async def report(
Expand Down Expand Up @@ -327,6 +353,7 @@ class RunningJob:
SentryReporter(),
MailReporter(),
ShellReporter(),
HealthchecksReporter(),
] # type: List[Reporter]

def __init__(
Expand Down

0 comments on commit defd05f

Please sign in to comment.