CPUモニタリングによるスマートなタスクスケジューリング
Translating with AI…
Translating with AI…
待機してアイドル状態。その後、実行。
あなたは何か重要な作業をしている。そしてバックグラウンドでビルドが開始される。CPUが100%に急上昇。エディタがフリーズ。あなたの忍耐が尽きる。
もっと良い方法があります。
重いタスクがあなたのアクティブな作業と競合します:
直ちに実行することは:
CLIツールで、CPUが閾値以下になるのを待ってから実行します:
npx cpu-wait -- npm run build
システムがアイドル状態のときにビルドが開始されます。
┌─────────────────────────────────────────────────────────┐
│ CPU使用率の推移 │
│ │
│ 100% ─ ▓▓▓▓▓▓▓▓ │
│ 80% ─ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ─ ─ ─ 閾値 ─ ─ ─ ─ ─ │
│ 60% ─ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ │
│ 40% ─ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ │
│ 20% ─ ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░ │
│ 0% ─────────────────────────────────────▶ │
│ ↑ │
│ コマンドが実行される │
└─────────────────────────────────────────────────────────┘
npx cpu-wait -- npm run build
デフォルト: CPUが60秒間< 80%になるのを待ちます。
# CPUが50%以下になるのを待つ
npx cpu-wait --threshold 50 -- npm run build
# CPUが30秒間低い状態になるのを待つ
npx cpu-wait --duration 30 -- ./heavy-script.sh
npx cpu-wait \
--threshold 40 \
--duration 120 \
--interval 2 \
--verbose \
-- npm run test
| フラグ | デフォルト | 説明 |
|---|---|---|
-t, --threshold |
80 | CPU使用率の閾値 (%) |
-d, --duration |
60 | CPUが閾値以下でいるべき時間 (秒) |
-i, --interval |
5 | チェック間隔 (秒) |
-v, --verbose |
off | 詳細な進行状況を表示 |
-q, --quiet |
off | 出力をすべて抑制 |
# 開発中: コーディングを妨げずにビルド
npx cpu-wait -- npm run build
# 本当にアイドルの際にのみバックアップを実行
npx cpu-wait --threshold 30 --duration 120 -- ./backup.sh
# 他の作業と競合しない
npx cpu-wait --threshold 50 -- ffmpeg -i input.mp4 -c:v libx264 output.mp4
# リソースが空いている時に全パイプラインを実行
npx cpu-wait -- bash -c "npm test && npm run build && npm run deploy"
{
"scripts": {
"build:when-idle": "npx cpu-wait -- npm run build",
"test:when-idle": "npx cpu-wait --threshold 70 -- npm test"
}
}
Node.jsアプリケーションでライブラリとして使用:
import { waitCpu } from 'cpu-wait';
// デフォルトで待機
await waitCpu();
console.log('CPUが使用可能!');
// 重いコードを実行
await processData();
await waitCpu({
threshold: 50,
duration: 30,
interval: 2,
onProgress: (msg) => console.log(msg)
});
import { waitCpu } from 'cpu-wait';
async function scheduleHeavyWork() {
console.log('最適な条件を待っています...');
await waitCpu({
threshold: 60,
duration: 45,
onProgress: (message) => {
updateStatusBar(`[CPU] ${message}`);
}
});
console.log('重い作業を開始します...');
await runExpensiveOperation();
}
# GitHub Actions: エージェント容量の待機
steps:
- run: npx cpu-wait -- npm run build
name: エージェント準備完了時にビルド
# ターミナル1: アクティブな開発
vim src/app.ts
# ターミナル2: テストは入力停止を待つ
npx cpu-wait --threshold 60 -- npm test
// システムを圧迫せずにファイルを処理
for (const file of files) {
await waitCpu({ threshold: 70, duration: 10 });
await processFile(file);
}
# トラフィックが少ないときにのみメンテナンスを実行
npx cpu-wait --threshold 20 --duration 300 -- ./maintenance.sh
// CPUに配慮したカスタムジョブキュー
const queue = new JobQueue();
queue.process(async (job) => {
await waitCpu({ threshold: 60 });
return await executeJob(job);
});
niceはプロセスの優先度を下げますが、並行実行を防ぎません。重いタスクは即座に実行され、ただ遅くなります。cpu-waitはリソースが空くまで実行を完全に延期します。
ジョブキュー(Bull、Agenda)はインフラを追加します。RedisやMongoDBの依存関係があります。設定オーバーヘッド。cpu-waitは、セットアップ不要の単一のコマンドです。
オフピークにスケジュールすることで予測可能な負荷に対処できますが、
cpu-waitは時間でなく実際のシステム状態に対応します。
ツールはすべてのコアのCPU使用率をサンプリングします:
総使用率 = (user + system + nice) / (user + system + nice + idle) × 100
サンプリングは設定された間隔で行われ(デフォルトは5秒)、平均値が閾値と比較されます。
以下で動作:
同じAPI、プラットフォームに関わらず同じ動作。
モニタリング自体は軽量です:
# グローバル
npm install -g cpu-wait
# または直接使用
npx cpu-wait -- your-command
コンピュータは人間を待つべきです、その逆ではありません。
重いタスクは重要ですが、緊急ではないことが多いです。適切な時を待たせましょう。あなたのアクティブな作業が常に優先されます。
インストール: npm install -g cpu-wait
GitHub: github.com/snomiao/cpu-wait
Snowstar Miaoは開発者のワークフローを尊重するツールを作成します。