Bluesky app fork with some witchin' additions 馃挮
at main 1.3 kB view raw
1import cluster, {Worker} from 'node:cluster' 2 3import {envInt} from '@atproto/common' 4 5import {CardService, envToCfg, httpLogger, readEnv} from './index.js' 6 7async function main() { 8 const env = readEnv() 9 const cfg = envToCfg(env) 10 const card = await CardService.create(cfg) 11 await card.start() 12 httpLogger.info('card service is running') 13 process.on('SIGTERM', async () => { 14 httpLogger.info('card service is stopping') 15 await card.destroy() 16 httpLogger.info('card service is stopped') 17 if (cluster.isWorker) process.exit(0) 18 }) 19} 20 21const workerCount = envInt('CARD_CLUSTER_WORKER_COUNT') 22 23if (workerCount) { 24 if (cluster.isPrimary) { 25 httpLogger.info(`primary ${process.pid} is running`) 26 const workers = new Set<Worker>() 27 for (let i = 0; i < workerCount; ++i) { 28 workers.add(cluster.fork()) 29 } 30 let teardown = false 31 cluster.on('exit', worker => { 32 workers.delete(worker) 33 if (!teardown) { 34 workers.add(cluster.fork()) // restart on crash 35 } 36 }) 37 process.on('SIGTERM', () => { 38 teardown = true 39 httpLogger.info('disconnecting workers') 40 workers.forEach(w => w.kill('SIGTERM')) 41 }) 42 } else { 43 httpLogger.info(`worker ${process.pid} is running`) 44 main() 45 } 46} else { 47 main() // non-clustering 48}