Skip to content

feat: Bot Startup Issues and Add Health Check with Smoke Test#275

Closed
Suvam-paul145 wants to merge 1 commit intoGauravKarakoti:mainfrom
Suvam-paul145:main
Closed

feat: Bot Startup Issues and Add Health Check with Smoke Test#275
Suvam-paul145 wants to merge 1 commit intoGauravKarakoti:mainfrom
Suvam-paul145:main

Conversation

@Suvam-paul145
Copy link

@Suvam-paul145 Suvam-paul145 commented Feb 18, 2026

This PR resolves bot startup failures caused by merge conflicts and improves observability by adding a health check endpoint and an automated smoke test. The changes ensure reliable startup sequencing and provide a clear signal of application readiness.

Problem Statement

  • Merge conflicts in bot/src/bot.ts caused unstable startup behavior.
  • There was no reliable way to determine whether the bot was fully initialized.
  • Manual verification of startup health was error-prone and inconsistent.

Solution

  • Cleaned up merge conflicts and unified startup logic.
  • Introduced a global readiness flag to track application state.
  • Added a health check endpoint that reflects the real startup status.
  • Implemented an automated smoke test to validate startup end-to-end.

Changes Made

1. Startup Fixes

  • Removed merge conflict markers from bot/src/bot.ts.
  • Consolidated startup logic to guarantee orderMonitor starts before the bot launches.
  • Added a global isReady flag to represent initialization state.

2. Health Check Endpoint

  • Endpoint: GET /health
  • Behavior:
    • Returns 503 Service Unavailable with {"status":"starting"} during initialization.
    • Returns 200 OK with {"status":"ok"} once startup completes successfully.

3. Smoke Test Script

  • File: bot/scripts/smoke-test.ts
  • Command: npm run test:smoke
  • Logic:
    • Spawns the bot process.
    • Polls the /health endpoint.
    • Passes when 200 OK is received or fails after a 30s timeout.

Startup & Health Flow

flowchart LR
    A[Bot Process Starts] --> B[Initialize orderMonitor]
    B --> C[Bot Startup Logic]
    C --> D{isReady?}
    D -- No --> E[/health → 503 starting/]
    D -- Yes --> F[/health → 200 ok/]
    
closes issues #211 
Loading

Copilot AI review requested due to automatic review settings February 18, 2026 06:44
@vercel
Copy link
Contributor

vercel bot commented Feb 18, 2026

@Suvam-paul145 is attempting to deploy a commit to the Gaurav's projects Team on Vercel.

A member of the Team first needs to authorize it.

@netlify
Copy link

netlify bot commented Feb 18, 2026

Deploy Preview for swapsmithminiapp canceled.

Name Link
🔨 Latest commit 9ff1713
🔍 Latest deploy log https://app.netlify.com/projects/swapsmithminiapp/deploys/69955fe0a2d01c0007703392

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes bot startup sequencing issues and adds an HTTP readiness signal (/health) plus a smoke-test script to validate end-to-end startup.

Changes:

  • Adds a global readiness flag and a /health endpoint that reports starting vs ok.
  • Consolidates startup flow to start orderMonitor before launching the bot, failing fast on startup errors.
  • Introduces a Node/TS smoke test that spawns the bot and polls /health until ready.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
bot/src/bot.ts Removes conflict artifacts, enforces startup order, and adds /health readiness endpoint.
bot/scripts/smoke-test.ts New smoke test script that spawns the bot and polls /health for readiness.
bot/package.json Adds test:smoke script entry.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +62 to +71
// Cleanup
console.log('\n🛑 Stopping bot process...');
botProcess.kill('SIGTERM');

// Windows might need stronger kill
if (process.platform === 'win32') {
spawn('taskkill', ['/pid', botProcess.pid!.toString(), '/f', '/t']);
}

process.exit(healthCheckPassed ? 0 : 1);
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleanup sends SIGTERM and then immediately calls process.exit(...) without waiting for the spawned process (and any child processes created by npm run dev / ts-node-dev --respawn) to actually exit. This can leave orphaned processes and port contention in CI; wait for the child exit event with a short timeout, then force-kill the full process tree if needed before exiting the smoke test.

Copilot uses AI. Check for mistakes.
Comment on lines +36 to +56
const botProcess = spawn('npm', ['run', 'dev'], {
cwd: path.join(__dirname, '..'),
stdio: 'inherit',
shell: true,
env: { ...process.env, PORT: String(PORT) }
});

let healthCheckPassed = false;

// Give it a moment to initialize spawn
await sleep(2000);

for (let i = 0; i < MAX_RETRIES; i++) {
process.stdout.write('.');
if (await checkHealth()) {
console.log('\n✅ Health check passed!');
healthCheckPassed = true;
break;
}
await sleep(CHECK_INTERVAL);
}
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The polling loop doesn't check whether botProcess has already exited (e.g., startup crash). In that case the script waits the full timeout and reports a generic health timeout, which hides the real failure mode and slows CI; attach an exit/close handler and fail fast if the child exits before the health check passes.

Copilot uses AI. Check for mistakes.
Comment on lines 477 to +491
app.listen(PORT, () => console.log(`🌍 Server running on port ${PORT}`));

(async () => {
<<<<<<< HEAD
await orderMonitor.loadPendingOrders();
orderMonitor.start();
bot.launch();
logger.info('🤖 Bot running');
})();

try {
await orderMonitor.loadPendingOrders();
orderMonitor.start();
console.log('👀 Order Monitor started');

await bot.launch();
console.log('🤖 Bot launched successfully');

isReady = true;
} catch (e) {
console.error('⚠️ Failed to start order monitor:', e);
console.error('⚠️ Failed to start:', e);
process.exit(1);
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Startup logging here uses console.log/console.error even though the codebase already has a shared Winston logger (used broadly, e.g. src/services/order-monitor.ts). For consistent log formatting/levels (and to avoid missing logs in environments where stdout isn't captured), switch these startup messages to logger.info/logger.error (including the app.listen callback and the startup try/catch).

Copilot uses AI. Check for mistakes.
Comment on lines +14 to +15
"db:push": "drizzle-kit push",
"test:smoke": "ts-node scripts/smoke-test.ts"
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test:smoke script invokes ts-node, but ts-node is not listed as a direct dependency/devDependency in this package.json. Relying on transitive hoisting (via ts-node-dev) is brittle and can break in different npm install/hoist layouts; add ts-node explicitly (or change the script to use an existing direct tool like ts-node-dev/npx ts-node).

Copilot uses AI. Check for mistakes.
@GauravKarakoti
Copy link
Owner

Go through the copilot suggestions

@Suvam-paul145
Copy link
Author

yes @GauravKarakoti as I have have not vercel Authorization that's why it fails, other than it is ready for merge

@GauravKarakoti
Copy link
Owner

Ahh actually @Suvam-paul145 i just cross checked #211 was not assigned to you... please get assigned to issue before raising a PR for them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants