Skip to content

Commit

Permalink
feat(app-service): add start.sh to init packages on start;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Nov 16, 2021
1 parent 669354e commit 7d056cc
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 43 deletions.
1 change: 1 addition & 0 deletions packages/app-service/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ DB_URI=mongodb://root:password123@localhost:27017/?authSource=admin&replicaSet=l
SERVER_SALT=abcdefg1234567!@#$%^&sadfqwef&*^*#!@^
LOG_LEVEL=trace
ENABLE_CLOUD_FUNCTION_LOG = always
FLAGS=--max_old_space_size=256
21 changes: 20 additions & 1 deletion packages/app-service/package-lock.json

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

1 change: 1 addition & 0 deletions packages/app-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"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 Down
99 changes: 99 additions & 0 deletions packages/app-service/src/api/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { ObjectId } from 'bson'
import fse = require('fs-extra')
import path = require('path')
import { Constants } from '../constants'
import { DatabaseAgent } from '../lib/database'
import { execSync } from 'child_process'

/**
* 在 node_modules 中创建 云函数 sdk 包:@, 这个包是为了云函数IDE 加载类型提示文件而创建的,不可发布
*/
export function createCloudFunctionDeclarationPackage() {
const source = path.resolve(__dirname, '../../dist')
const target = path.resolve(__dirname, '../../node_modules/@')

fse.ensureDirSync(target)
fse.copySync(source, target)

console.log(`copy success: ${source} => ${target}`)

const packageJson = `
{
"name": "@",
"version": "0.0.0"
}
`
const pkgJsonPath = path.join(target, 'package.json')
fse.writeFileSync(pkgJsonPath, packageJson)

console.log(`write success: ${pkgJsonPath}`)
}

export function isCloudSdkPackageExists() {
const target = path.resolve(__dirname, '../../../node_modules/@')
const pkgJsonPath = path.join(target, 'package.json')
return fse.existsSync(pkgJsonPath)
}

export function initCloudSdkPackage() {
if (!isCloudSdkPackageExists()) {
createCloudFunctionDeclarationPackage()
}
}


interface AppConfigItem {
_id: ObjectId
key: string
value: {
name: string,
version: string
}[]
}

/**
* Get extra npm packages
* @returns
*/
export async function getExtraPackages() {
await DatabaseAgent.accessor.ready
const db = DatabaseAgent.db
const doc = await db.collection<AppConfigItem>(Constants.config_collection)
.findOne({ key: 'packages' })

return doc?.value ?? []
}

/**
* Install packages
* @param packages
* @returns
*/
export function installPackages(packages: { name: string, version: string }[]) {
if (!packages?.length) {
return
}

const names = packages
.map(pkg => {
return pkg.version ? `${pkg.name}@${pkg.version}` : `${pkg.name}`
})

const cmd_str = names.join(' ')
const r = execSync(`npm install ${cmd_str}`)
return r.toString()
}

/**
* Check if node module exists
* @param moduleName
* @returns
*/
export function moduleExists(mod: string) {
try {
require.resolve(mod)
return true
} catch (_err) {
return false
}
}
7 changes: 6 additions & 1 deletion packages/app-service/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export const Constants = {
/**
* collection name of cloud functions' log
*/
function_log_collection: "__function_logs"
function_log_collection: "__function_logs",

/**
* collection name of application configuration
*/
config_collection: '__config__'
}

deepFreeze(Constants)
2 changes: 1 addition & 1 deletion packages/app-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Config from './config'
import { router } from './router/index'
import { logger } from './lib/logger'
import { generateUUID } from './lib/utils/rand'
import { initCloudSdkPackage } from './lib/utils/init'
import { initCloudSdkPackage } from './api/init'
import { WebSocketAgent } from './lib/ws'

initCloudSdkPackage()
Expand Down
35 changes: 35 additions & 0 deletions packages/app-service/src/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { getExtraPackages, initCloudSdkPackage, installPackages, moduleExists } from "./api/init"


async function main() {
const packages = await getExtraPackages()
if (!packages.length) {
console.log('no extra packages found')
return 0
}

console.log('packages loaded: ', packages)

const not_exists = packages.filter(pkg => !moduleExists(pkg.name))
if (!not_exists.length) {
console.log('no new packages to be installed')
return 0
}

try {
const res = installPackages(packages)
console.log(res)

initCloudSdkPackage()
} catch (error) {
console.error(error)
return 1
}

return 0
}


main().then(code => {
process.exit(code)
})
38 changes: 0 additions & 38 deletions packages/app-service/src/lib/utils/init.ts

This file was deleted.

9 changes: 9 additions & 0 deletions packages/app-service/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

echo "****** init start ******"
node ./dist/init.js
echo "****** init end *******"

# source .env
echo "****** start service: node $FLAGS ./dist/index.js *******"
exec node $FLAGS ./dist/index.js
6 changes: 4 additions & 2 deletions packages/system-server/src/lib/service-driver/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ export class DockerContainerServiceDriver {

const container = await this.docker.createContainer({
Image: imageName,
Cmd: ['node', `--max_old_space_size=${max_old_space_size}`, './dist/index.js'],
// Cmd: ['node', `--max_old_space_size=${max_old_space_size}`, './dist/index.js'],
Cmd: ['sh', '/app/start.sh'],
name: `app_${app.appid}`,
Env: [
`DB=${app.config.db_name}`,
`DB_URI=${uri}`,
`LOG_LEVEL=${logLevel}`,
`ENABLE_CLOUD_FUNCTION_LOG=always`,
`SERVER_SECRET_SALT=${app.config.server_secret_salt}`
`SERVER_SECRET_SALT=${app.config.server_secret_salt}`,
`FLAGS=--max_old_space_size=${max_old_space_size}`
],
ExposedPorts: {
"8000/tcp": {}
Expand Down

0 comments on commit 7d056cc

Please sign in to comment.