Skip to content

Commit 7d056cc

Browse files
committed
feat(app-service): add start.sh to init packages on start;
1 parent 669354e commit 7d056cc

File tree

10 files changed

+176
-43
lines changed

10 files changed

+176
-43
lines changed

packages/app-service/.env.template

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ DB_URI=mongodb://root:password123@localhost:27017/?authSource=admin&replicaSet=l
22
SERVER_SALT=abcdefg1234567!@#$%^&sadfqwef&*^*#!@^
33
LOG_LEVEL=trace
44
ENABLE_CLOUD_FUNCTION_LOG = always
5+
FLAGS=--max_old_space_size=256

packages/app-service/package-lock.json

+20-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/app-service/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"express": "^4.17.1",
3434
"fs-extra": "^9.1.0",
3535
"jsonwebtoken": "^8.5.1",
36+
"laf-client-sdk": "^0.6.16",
3637
"lodash": "^4.17.21",
3738
"log4js": "^6.3.0",
3839
"mongodb": "^4.1.3",

packages/app-service/src/api/init.ts

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { ObjectId } from 'bson'
2+
import fse = require('fs-extra')
3+
import path = require('path')
4+
import { Constants } from '../constants'
5+
import { DatabaseAgent } from '../lib/database'
6+
import { execSync } from 'child_process'
7+
8+
/**
9+
* 在 node_modules 中创建 云函数 sdk 包:@, 这个包是为了云函数IDE 加载类型提示文件而创建的,不可发布
10+
*/
11+
export function createCloudFunctionDeclarationPackage() {
12+
const source = path.resolve(__dirname, '../../dist')
13+
const target = path.resolve(__dirname, '../../node_modules/@')
14+
15+
fse.ensureDirSync(target)
16+
fse.copySync(source, target)
17+
18+
console.log(`copy success: ${source} => ${target}`)
19+
20+
const packageJson = `
21+
{
22+
"name": "@",
23+
"version": "0.0.0"
24+
}
25+
`
26+
const pkgJsonPath = path.join(target, 'package.json')
27+
fse.writeFileSync(pkgJsonPath, packageJson)
28+
29+
console.log(`write success: ${pkgJsonPath}`)
30+
}
31+
32+
export function isCloudSdkPackageExists() {
33+
const target = path.resolve(__dirname, '../../../node_modules/@')
34+
const pkgJsonPath = path.join(target, 'package.json')
35+
return fse.existsSync(pkgJsonPath)
36+
}
37+
38+
export function initCloudSdkPackage() {
39+
if (!isCloudSdkPackageExists()) {
40+
createCloudFunctionDeclarationPackage()
41+
}
42+
}
43+
44+
45+
interface AppConfigItem {
46+
_id: ObjectId
47+
key: string
48+
value: {
49+
name: string,
50+
version: string
51+
}[]
52+
}
53+
54+
/**
55+
* Get extra npm packages
56+
* @returns
57+
*/
58+
export async function getExtraPackages() {
59+
await DatabaseAgent.accessor.ready
60+
const db = DatabaseAgent.db
61+
const doc = await db.collection<AppConfigItem>(Constants.config_collection)
62+
.findOne({ key: 'packages' })
63+
64+
return doc?.value ?? []
65+
}
66+
67+
/**
68+
* Install packages
69+
* @param packages
70+
* @returns
71+
*/
72+
export function installPackages(packages: { name: string, version: string }[]) {
73+
if (!packages?.length) {
74+
return
75+
}
76+
77+
const names = packages
78+
.map(pkg => {
79+
return pkg.version ? `${pkg.name}@${pkg.version}` : `${pkg.name}`
80+
})
81+
82+
const cmd_str = names.join(' ')
83+
const r = execSync(`npm install ${cmd_str}`)
84+
return r.toString()
85+
}
86+
87+
/**
88+
* Check if node module exists
89+
* @param moduleName
90+
* @returns
91+
*/
92+
export function moduleExists(mod: string) {
93+
try {
94+
require.resolve(mod)
95+
return true
96+
} catch (_err) {
97+
return false
98+
}
99+
}

packages/app-service/src/constants.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ export const Constants = {
1717
/**
1818
* collection name of cloud functions' log
1919
*/
20-
function_log_collection: "__function_logs"
20+
function_log_collection: "__function_logs",
21+
22+
/**
23+
* collection name of application configuration
24+
*/
25+
config_collection: '__config__'
2126
}
2227

2328
deepFreeze(Constants)

packages/app-service/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Config from './config'
1111
import { router } from './router/index'
1212
import { logger } from './lib/logger'
1313
import { generateUUID } from './lib/utils/rand'
14-
import { initCloudSdkPackage } from './lib/utils/init'
14+
import { initCloudSdkPackage } from './api/init'
1515
import { WebSocketAgent } from './lib/ws'
1616

1717
initCloudSdkPackage()

packages/app-service/src/init.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { getExtraPackages, initCloudSdkPackage, installPackages, moduleExists } from "./api/init"
2+
3+
4+
async function main() {
5+
const packages = await getExtraPackages()
6+
if (!packages.length) {
7+
console.log('no extra packages found')
8+
return 0
9+
}
10+
11+
console.log('packages loaded: ', packages)
12+
13+
const not_exists = packages.filter(pkg => !moduleExists(pkg.name))
14+
if (!not_exists.length) {
15+
console.log('no new packages to be installed')
16+
return 0
17+
}
18+
19+
try {
20+
const res = installPackages(packages)
21+
console.log(res)
22+
23+
initCloudSdkPackage()
24+
} catch (error) {
25+
console.error(error)
26+
return 1
27+
}
28+
29+
return 0
30+
}
31+
32+
33+
main().then(code => {
34+
process.exit(code)
35+
})

packages/app-service/src/lib/utils/init.ts

-38
This file was deleted.

packages/app-service/start.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
3+
echo "****** init start ******"
4+
node ./dist/init.js
5+
echo "****** init end *******"
6+
7+
# source .env
8+
echo "****** start service: node $FLAGS ./dist/index.js *******"
9+
exec node $FLAGS ./dist/index.js

packages/system-server/src/lib/service-driver/container.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ export class DockerContainerServiceDriver {
3232

3333
const container = await this.docker.createContainer({
3434
Image: imageName,
35-
Cmd: ['node', `--max_old_space_size=${max_old_space_size}`, './dist/index.js'],
35+
// Cmd: ['node', `--max_old_space_size=${max_old_space_size}`, './dist/index.js'],
36+
Cmd: ['sh', '/app/start.sh'],
3637
name: `app_${app.appid}`,
3738
Env: [
3839
`DB=${app.config.db_name}`,
3940
`DB_URI=${uri}`,
4041
`LOG_LEVEL=${logLevel}`,
4142
`ENABLE_CLOUD_FUNCTION_LOG=always`,
42-
`SERVER_SECRET_SALT=${app.config.server_secret_salt}`
43+
`SERVER_SECRET_SALT=${app.config.server_secret_salt}`,
44+
`FLAGS=--max_old_space_size=${max_old_space_size}`
4345
],
4446
ExposedPorts: {
4547
"8000/tcp": {}

0 commit comments

Comments
 (0)