Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add reporting for Healthchecks #89

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ Reporting
+++++++++

Yacron has builtin support for reporting jobs failure (more on that below) by
email, Sentry and shell command (additional reporting methods might be added in the future):
email, Sentry, Healthchecks, and shell command (additional reporting methods might be
added in the future):

.. code-block:: yaml

Expand Down Expand Up @@ -259,6 +260,8 @@ email, Sentry and shell command (additional reporting methods might be added in
shell:
shell: /bin/bash
command: ...
healthchecks:
ping_url: http://checks.foobar.com/ping/a9d7db3e-6c91-4527-a495-2a676163c314

Here, the ``onFailure`` object indicates that what to do when a job failure
is detected. In this case we ask for it to be reported both to sentry and by
Expand Down
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
Loading