Pular para o conteúdo principal

🔗 Integration Examples

Here are examples of integrating Monitron heartbeats with various languages and frameworks.


🐘 PHP / Laravel

Laravel Scheduled Command

// app/Console/Kernel.php
$schedule->command('reports:generate')
->daily()
->after(function () {
Http::get('https://your-monitron.com/heartbeat/TOKEN');
})
->onFailure(function () {
Http::post('https://your-monitron.com/heartbeat/TOKEN/fail');
});

Plain PHP

<?php
// Your script logic
$success = do_backup();

if ($success) {
file_get_contents('https://your-monitron.com/heartbeat/TOKEN');
} else {
$ctx = stream_context_create(['http' => ['method' => 'POST']]);
file_get_contents('https://your-monitron.com/heartbeat/TOKEN/fail', false, $ctx);
}

🟢 Node.js

const https = require('https');

async function myJob() {
try {
await doSomething();
// Report success
https.get('https://your-monitron.com/heartbeat/TOKEN');
} catch (err) {
// Report failure
https.request('https://your-monitron.com/heartbeat/TOKEN/fail', { method: 'POST' }).end();
throw err;
}
}

🐍 Python

import requests

def my_job():
try:
do_something()
requests.get('https://your-monitron.com/heartbeat/TOKEN', timeout=10)
except Exception as e:
requests.post('https://your-monitron.com/heartbeat/TOKEN/fail', timeout=10)
raise

if __name__ == '__main__':
my_job()

🦀 Rust

fn main() {
match run_my_job() {
Ok(_) => {
reqwest::blocking::get("https://your-monitron.com/heartbeat/TOKEN").ok();
}
Err(e) => {
reqwest::blocking::Client::new()
.post("https://your-monitron.com/heartbeat/TOKEN/fail")
.send().ok();
panic!("{}", e);
}
}
}

🐳 Docker

# In your Docker entrypoint or health check
HEALTHCHECK --interval=5m --timeout=3s \
CMD curl -fs https://your-monitron.com/heartbeat/TOKEN || exit 1

☸️ Kubernetes CronJob

apiVersion: batch/v1
kind: CronJob
metadata:
name: my-job
spec:
schedule: "0 2 * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: my-job
image: my-image
command:
- /bin/sh
- -c
- |
/app/my-script.sh && \
curl -fsS https://your-monitron.com/heartbeat/TOKEN || \
curl -fsS -X POST https://your-monitron.com/heartbeat/TOKEN/fail
restartPolicy: OnFailure

💡 Dicas

  • Always set a timeout on your HTTP requests to Monitron (5-10 seconds). You don't want your job hanging because Monitron is slow.
  • Use --retry with curl to handle transient network issues.
  • Fire-and-forget: The heartbeat ping doesn't need to succeed for your job to be considered successful. Use || true in bash to prevent curl failures from failing your job.