-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
181 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const JOBS_PORT = parseInt(process.env['NANGO_JOBS_PORT'] || '') || 3005; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import os from 'os'; | ||
import fs from 'fs'; | ||
import { stringifyError, type NangoProps } from '@nangohq/shared'; | ||
import * as superjson from 'superjson'; | ||
import { fetch } from 'undici'; | ||
import { getLogger } from '@nangohq/utils/dist/logger.js'; | ||
|
||
const MEMORY_WARNING_PERCENTAGE_THRESHOLD = 75; | ||
const logger = getLogger('Runner'); | ||
|
||
export class RunnerMonitor { | ||
private runnerId: string; | ||
private tracked: Map<number, NangoProps> = new Map<number, NangoProps>(); | ||
private jobsServiceUrl: string = ''; | ||
private persistServiceUrl: string = ''; | ||
private idleMaxDurationMs = parseInt(process.env['IDLE_MAX_DURATION_MS'] || '') || 0; | ||
private lastIdleTrackingDate = Date.now(); | ||
private lastMemoryReportDate: Date | null = null; | ||
private idleInterval: NodeJS.Timeout | null = null; | ||
private memoryInterval: NodeJS.Timeout | null = null; | ||
|
||
constructor({ runnerId, jobsServiceUrl, persistServiceUrl }: { runnerId: string; jobsServiceUrl: string; persistServiceUrl: string }) { | ||
this.runnerId = runnerId; | ||
this.jobsServiceUrl = jobsServiceUrl; | ||
this.persistServiceUrl = persistServiceUrl; | ||
if (this.jobsServiceUrl.length > 0) { | ||
this.memoryInterval = this.checkMemoryUsage(); | ||
this.idleInterval = this.checkIdle(); | ||
} | ||
process.on('SIGTERM', this.onExit.bind(this)); | ||
} | ||
|
||
private onExit(): void { | ||
if (this.idleInterval) { | ||
clearInterval(this.idleInterval); | ||
} | ||
if (this.memoryInterval) { | ||
clearInterval(this.memoryInterval); | ||
} | ||
} | ||
|
||
track(nangoProps: NangoProps): void { | ||
if (nangoProps.syncJobId) { | ||
this.lastIdleTrackingDate = Date.now(); | ||
this.tracked.set(nangoProps.syncJobId, nangoProps); | ||
} | ||
} | ||
|
||
untrack(nangoProps: NangoProps): void { | ||
if (nangoProps.syncJobId) { | ||
this.tracked.delete(nangoProps.syncJobId); | ||
} | ||
} | ||
|
||
private checkMemoryUsage(): NodeJS.Timeout { | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
return setInterval(async () => { | ||
const rss = process.memoryUsage().rss; | ||
const total = getTotalMemoryInBytes(); | ||
const memoryUsagePercentage = (rss / total) * 100; | ||
if (memoryUsagePercentage > MEMORY_WARNING_PERCENTAGE_THRESHOLD) { | ||
await this.reportHighMemoryUsage(memoryUsagePercentage); | ||
} | ||
}, 1000); | ||
} | ||
|
||
private async reportHighMemoryUsage(memoryUsagePercentage: number): Promise<void> { | ||
// only report if it has been more than 30 seconds since the last report | ||
if (this.lastMemoryReportDate) { | ||
const now = new Date(); | ||
const diffInSecs = (now.getTime() - this.lastMemoryReportDate.getTime()) / 1000; | ||
if (diffInSecs < 30) { | ||
return; | ||
} | ||
} | ||
this.lastMemoryReportDate = new Date(); | ||
for (const { environmentId, activityLogId } of this.tracked.values()) { | ||
if (!environmentId || !activityLogId) { | ||
continue; | ||
} | ||
await httpSend({ | ||
method: 'post', | ||
url: `${this.persistServiceUrl}/environment/${environmentId}/log`, | ||
data: JSON.stringify({ | ||
activityLogId: activityLogId, | ||
level: 'warn', | ||
msg: `Memory usage of nango scripts is high: ${memoryUsagePercentage.toFixed(2)}% of the total available memory.` | ||
}) | ||
}); | ||
} | ||
} | ||
|
||
private checkIdle(): NodeJS.Timeout { | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
return setInterval(async () => { | ||
if (this.tracked.size == 0) { | ||
const idleTimeMs = Date.now() - this.lastIdleTrackingDate; | ||
if (idleTimeMs > this.idleMaxDurationMs) { | ||
logger.info(`Runner '${this.runnerId}' idle for more than ${this.idleMaxDurationMs}ms`); | ||
await httpSend({ | ||
method: 'post', | ||
url: `${this.jobsServiceUrl}/idle`, | ||
data: superjson.stringify({ | ||
runnerId: this.runnerId, | ||
idleTimeMs | ||
}) | ||
}); | ||
this.lastIdleTrackingDate = Date.now(); | ||
} | ||
} | ||
}, 10000); | ||
} | ||
} | ||
|
||
function getRenderTotalMemoryInBytes(): number { | ||
const memoryMaxFile = '/sys/fs/cgroup/memory.max'; | ||
try { | ||
const output = fs.readFileSync(memoryMaxFile, 'utf-8'); | ||
const memoryLimitInBytes = parseInt(output.trim(), 10); | ||
return memoryLimitInBytes; | ||
} catch (error) { | ||
logger.warn(`Error reading memory limit from ${memoryMaxFile}: ${stringifyError(error)}`); | ||
return 0; | ||
} | ||
} | ||
|
||
function getTotalMemoryInBytes(): number { | ||
// when running inside a container, os.totalmem() returns the total memory of the system, not the memory limit of the container | ||
// see: https://github.com/nodejs/node/issues/51095 | ||
// process.constrainedMemory() is supposed to return the memory limit of the container but it doesn't work on Render | ||
// so we need to use a workaround to get the memory limit of the container on Render | ||
return process.constrainedMemory() || getRenderTotalMemoryInBytes() || os.totalmem(); | ||
} | ||
|
||
async function httpSend({ method, url, data }: { method: string; url: string; data: string }): Promise<void> { | ||
try { | ||
const res = await fetch(url, { | ||
method: method, | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json' | ||
}, | ||
body: data | ||
}); | ||
if (res.status !== 200) { | ||
logger.error(`Error sending '${data}' to '${url}': ${JSON.stringify(await res.json())}`); | ||
} | ||
} catch (err) { | ||
logger.error(`Error sending '${data}' to '${url}': ${stringifyError(err)}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters