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

Feat: Probing with Queue #1214

Closed
wants to merge 8 commits 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
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"pino-pretty": "10.0.1",
"piscina": "^4.1.0",
"prom-client": "13.1.0",
"queue": "^6.0.2",
"redis": "^4.3.0",
"smtp-server": "^3.8.0",
"sqlite": "^4.0.21",
Expand Down
29 changes: 20 additions & 9 deletions src/components/probe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { differenceInSeconds } from 'date-fns'
import { getContext } from '../../context'
import type { Notification } from '@hyperjumptech/monika-notification'
import type { Probe } from '../../interfaces/probe'
import Queue from 'queue'
import {
getProbeContext,
getProbeState,
Expand All @@ -48,6 +49,13 @@ type doProbeParams = {
* @param {object} doProbeParams doProbe parameter
* @returns {Promise<void>} void
*/

const queue = new Queue({
concurrency: 1,
autostart: true,
timeout: 10_000,
})

export async function doProbe({
probe,
notifications,
Expand Down Expand Up @@ -77,15 +85,18 @@ export async function doProbe({
probe.recoveryThreshold || DEFAULT_RECOVERY_THRESHOLD
)

await retry(handleAll, {
maxAttempts,
backoff: new ExponentialBackoff({
initialDelay: getContext().flags.retryInitialDelayMs,
maxDelay: getContext().flags.retryMaxDelayMs,
}),
}).execute(({ attempt }) =>
Promise.all(probers.map(async (prober) => prober.probe(attempt)))
)
// Enqueue each prober into the prober queue with retry backoff
queue.push(async () => {
await retry(handleAll, {
maxAttempts,
backoff: new ExponentialBackoff({
initialDelay: getContext().flags.retryInitialDelayMs,
maxDelay: getContext().flags.retryMaxDelayMs,
}),
}).execute(({ attempt }) =>
Promise.all(probers.map(async (prober) => prober.probe(attempt)))
)
})

setProbeFinish(probe.id)
}, randomTimeoutMilliseconds)
Expand Down
3 changes: 1 addition & 2 deletions src/looper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ import type { Notification } from '@hyperjumptech/monika-notification'

import { AbortSignal } from 'node-abort-controller'
import { v4 as uuid } from 'uuid'

import type { Probe, ProbeAlert } from '../interfaces/probe'

import { doProbe } from '../components/probe'
import { getContext } from '../context'
import { log } from '../utils/pino'
import {
Expand All @@ -42,6 +40,7 @@ import {
DEFAULT_INCIDENT_THRESHOLD,
DEFAULT_RECOVERY_THRESHOLD,
} from '../components/config/validation/validator/default-values'
import { doProbe } from '../components/probe'

let checkSTUNinterval: NodeJS.Timeout

Expand Down
Loading