|
| 1 | +import os from 'node:os' |
| 2 | +import { styleText } from 'node:util' |
1 | 3 | import type { CommonLogger } from '@naturalcycles/js-lib' |
2 | 4 | import { pDelay, setGlobalStringifyFunction } from '@naturalcycles/js-lib' |
3 | 5 | import { inspectStringifyFn } from '../string/inspect' |
@@ -43,6 +45,7 @@ const { DEBUG_RUN_SCRIPT } = process.env |
43 | 45 | * Set env DEBUG_RUN_SCRIPT for extra debugging. |
44 | 46 | */ |
45 | 47 | export function runScript(fn: (...args: any[]) => any, opt: RunScriptOptions = {}): void { |
| 48 | + logEnvironment() |
46 | 49 | setGlobalStringifyFunction(inspectStringifyFn) |
47 | 50 |
|
48 | 51 | const { logger = console, noExit, registerUncaughtExceptionHandlers = true } = opt |
@@ -86,3 +89,43 @@ export function runScript(fn: (...args: any[]) => any, opt: RunScriptOptions = { |
86 | 89 | } |
87 | 90 | })() |
88 | 91 | } |
| 92 | + |
| 93 | +function logEnvironment(): void { |
| 94 | + const { |
| 95 | + platform, |
| 96 | + arch, |
| 97 | + versions: { node }, |
| 98 | + env: { CPU_LIMIT, NODE_OPTIONS = 'not defined' }, |
| 99 | + } = process |
| 100 | + |
| 101 | + const cpuLimit = Number(CPU_LIMIT) || undefined |
| 102 | + const availableParallelism = os.availableParallelism?.() |
| 103 | + const cpus = os.cpus().length |
| 104 | + console.log( |
| 105 | + dimGrey( |
| 106 | + Object.entries({ |
| 107 | + node: `${node} ${platform} ${arch}`, |
| 108 | + NODE_OPTIONS, |
| 109 | + cpus, |
| 110 | + availableParallelism, |
| 111 | + cpuLimit, |
| 112 | + }) |
| 113 | + .map(([k, v]) => `${k}: ${v}`) |
| 114 | + .join(', '), |
| 115 | + ), |
| 116 | + ) |
| 117 | + |
| 118 | + if (!NODE_OPTIONS) { |
| 119 | + console.warn( |
| 120 | + `NODE_OPTIONS env variable is not defined. You may run into out-of-memory issues when running memory-intensive scripts. It's recommended to set it to:\n--max-old-space-size=12000`, |
| 121 | + ) |
| 122 | + } else if (NODE_OPTIONS.includes('max_old')) { |
| 123 | + console.warn( |
| 124 | + `It looks like you're using "max_old_space_size" syntax with underscores instead of dashes - it's WRONG and doesn't work in environment variables. Strongly advised to rename it to "max-old-space-size"`, |
| 125 | + ) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function dimGrey(s: string): string { |
| 130 | + return styleText(['dim', 'grey'], s) |
| 131 | +} |
0 commit comments