• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hirosystems / stacks-blockchain-api / 4863643345

pending completion
4863643345

Pull #1630

github

GitHub
Merge b9b14018f into a6c8f16b6
Pull Request #1630: Logging migration to Pino library

2140 of 3228 branches covered (66.29%)

131 of 231 new or added lines in 41 files covered. (56.71%)

38 existing lines in 2 files now uncovered.

7490 of 9606 relevant lines covered (77.97%)

1789.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/shutdown-handler.ts
NEW
1
import { resolveOrTimeout } from './helpers';
×
NEW
2
import { logger } from './logger';
×
3

4
const SHUTDOWN_SIGNALS = ['SIGINT', 'SIGTERM'] as const;
×
5

6
type ShutdownHandler = () => void | PromiseLike<void>;
7
type ShutdownConfig = {
8
  name: string;
9
  handler: ShutdownHandler;
10
  forceKillable: boolean;
11
  forceKillHandler?: ShutdownHandler;
12
};
13

14
const shutdownConfigs: ShutdownConfig[] = [];
×
15

16
let isShuttingDown = false;
×
17

18
async function startShutdown() {
19
  if (isShuttingDown) {
×
20
    return;
×
21
  }
22
  isShuttingDown = true;
×
23
  const timeoutMs = parseInt(process.env['STACKS_SHUTDOWN_FORCE_KILL_TIMEOUT'] ?? '60') * 1000;
×
24
  let errorEncountered = false;
×
25
  for (const config of shutdownConfigs) {
×
26
    try {
×
27
      logger.info(`Closing ${config.name}...`);
×
28
      const gracefulShutdown = await resolveOrTimeout(
×
29
        Promise.resolve(config.handler()),
30
        timeoutMs,
31
        !config.forceKillable,
32
        () =>
NEW
33
          logger.error(
×
34
            `${config.name} is taking longer than expected to shutdown, possibly hanging indefinitely`
35
          )
36
      );
37
      if (!gracefulShutdown) {
×
38
        if (config.forceKillable && config.forceKillHandler) {
×
39
          await Promise.resolve(config.forceKillHandler());
×
40
        }
NEW
41
        logger.error(
×
42
          `${config.name} was force killed after taking longer than ${timeoutMs}ms to shutdown`
43
        );
44
      } else {
45
        logger.info(`${config.name} closed`);
×
46
      }
47
    } catch (error) {
48
      errorEncountered = true;
×
NEW
49
      logger.error(`Error running ${config.name} shutdown handler`, error);
×
50
    }
51
  }
52
  if (errorEncountered) {
×
53
    process.exit(1);
×
54
  } else {
55
    logger.info('App shutdown successful.');
×
56
    process.exit();
×
57
  }
58
}
59

60
let shutdownSignalsRegistered = false;
×
61
function registerShutdownSignals() {
62
  if (shutdownSignalsRegistered) {
×
63
    return;
×
64
  }
65
  shutdownSignalsRegistered = true;
×
66

67
  SHUTDOWN_SIGNALS.forEach(sig => {
×
68
    process.once(sig, () => {
×
69
      logger.info(`Shutting down... received signal: ${sig}`);
×
70
      void startShutdown();
×
71
    });
72
  });
73
  process.once('unhandledRejection', error => {
×
NEW
74
    logger.error(error, 'unhandledRejection');
×
75
    logger.error(`Shutting down... received unhandledRejection.`);
×
76
    void startShutdown();
×
77
  });
78
  process.once('uncaughtException', error => {
×
NEW
79
    logger.error(`Received uncaughtException: ${error}`, error);
×
80
    logger.error(`Shutting down... received uncaughtException.`);
×
81
    void startShutdown();
×
82
  });
83
  process.once('beforeExit', () => {
×
84
    logger.info(`Shutting down... received beforeExit.`);
×
85
    void startShutdown();
×
86
  });
87
}
88

89
export function registerShutdownConfig(...configs: ShutdownConfig[]) {
×
90
  registerShutdownSignals();
×
91
  shutdownConfigs.push(...configs);
×
92
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc