Skip to content

Commit

Permalink
feat(app-service): support gracefully exit;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Nov 17, 2021
1 parent 7d056cc commit 393caa2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 32 deletions.
34 changes: 8 additions & 26 deletions packages/app-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/app-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"express": "^4.17.1",
"fs-extra": "^9.1.0",
"jsonwebtoken": "^8.5.1",
"laf-client-sdk": "^0.6.16",
"lodash": "^4.17.21",
"log4js": "^6.3.0",
"mongodb": "^4.1.3",
Expand All @@ -47,7 +46,7 @@
"devDependencies": {
"@types/dotenv": "^8.2.0",
"@types/ejs": "^3.0.7",
"@types/express": "^4.17.11",
"@types/express": "^4.17.13",
"@types/fs-extra": "^9.0.8",
"@types/jsonwebtoken": "^8.5.1",
"@types/lodash": "^4.14.171",
Expand Down
17 changes: 14 additions & 3 deletions packages/app-service/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-11-12 14:55:11
* @LastEditTime: 2021-11-17 17:04:35
* @Description:
*/

Expand All @@ -11,10 +11,10 @@ import Config from './config'
import { router } from './router/index'
import { logger } from './lib/logger'
import { generateUUID } from './lib/utils/rand'
import { initCloudSdkPackage } from './api/init'
import { WebSocketAgent } from './lib/ws'
import { DatabaseAgent } from './lib/database'
import { SchedulerInstance } from './lib/scheduler'

initCloudSdkPackage()

/**
* Just for generating declaration type files for `@/cloud-sdk` which used in cloud function
Expand Down Expand Up @@ -63,3 +63,14 @@ server.on('upgrade', (req, socket, head) => {
})
})


process.on('SIGTERM', gracefullyExit)
process.on('SIGINT', gracefullyExit)

async function gracefullyExit() {
SchedulerInstance.destroy()
DatabaseAgent.accessor.close()
server.close(async () => {
logger.info('process gracefully exited!')
})
}
4 changes: 3 additions & 1 deletion packages/app-service/src/lib/scheduler/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @Author: Maslow<wangfugen@126.com>
* @Date: 2021-07-30 10:30:29
* @LastEditTime: 2021-09-10 00:08:05
* @LastEditTime: 2021-11-17 14:11:16
* @Description:
*/

Expand Down Expand Up @@ -40,6 +40,8 @@ accessor.ready.then(async () => {
const db = accessor.db
const stream = db.watch([], { fullDocument: 'updateLookup' })
stream.on("change", (doc) => { DatabaseChangeEventCallBack(doc) })
process.on('SIGINT', () => stream.close())
process.on('SIGTERM', () => stream.close())

// emit `App:ready` event
SchedulerInstance.emit('App:ready')
Expand Down

0 comments on commit 393caa2

Please sign in to comment.