Skip to content

Commit

Permalink
feat(server): add npm install flags to region; opt instance starting …
Browse files Browse the repository at this point in the history
…perf (#1133)
  • Loading branch information
maslow authored May 13, 2023
1 parent 14af9d5 commit 1e60429
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 71 deletions.
13 changes: 10 additions & 3 deletions runtimes/nodejs/init.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#!/bin/sh

# echo "****** init start ******"
set -e
node ./dist/init.js
# node ./dist/init.js

# skip init if $DEPENDENCIES is empty
if [ -z "$DEPENDENCIES" ]; then
echo "No dependencies to install."
exit 0
fi

echo "npm install $DEPENDENCIES $NPM_INSTALL_FLAGS"
npm install $DEPENDENCIES $NPM_INSTALL_FLAGS
cp -r /app/* /tmp/app
# echo "****** init end *******"
8 changes: 2 additions & 6 deletions runtimes/nodejs/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export default class Config {
return require('../package.json')?.version
}

static get APP_ID(): string {
return process.env.APP_ID
static get APPID(): string {
return process.env.APPID ?? process.env.APP_ID
}

static get NPM_INSTALL_FLAGS(): string {
Expand All @@ -83,8 +83,4 @@ export default class Config {
static get REQUEST_LIMIT_SIZE(): string {
return process.env.REQUEST_LIMIT_SIZE || '10mb'
}

static get PACKAGES(): string {
return process.env.PACKAGES || ''
}
}
3 changes: 2 additions & 1 deletion runtimes/nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ import { DatabaseAgent } from './db'
import xmlparser from 'express-xml-bodyparser'

// init static method of class
import './support/function-log'
import './support/cloud-sdk'
import { FunctionCache } from './support/function-engine/cache'
import { DatabaseChangeStream } from './support/db-change-stream'
import { InitHook } from './support/init-hook'
import { ensureCollectionIndexes } from './support/function-log'

const app = express()

DatabaseAgent.accessor.ready.then(() => {
ensureCollectionIndexes()
FunctionCache.initialize()
DatabaseChangeStream.initialize()
InitHook.invoke()
Expand Down
25 changes: 1 addition & 24 deletions runtimes/nodejs/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import { execSync } from 'child_process'
import Config from './config'

import { logger } from './support/logger'
import { FUNCTION_LOG_COLLECTION } from './constants'

async function main() {
try {
installPackages()

await ensureCollectionIndexes()
} catch (error) {
logger.error(error)
return 1
Expand All @@ -32,7 +29,7 @@ main()
* @returns
*/
export function installPackages() {
const deps = process.env.DEPENDENCIES || ''
const deps = process.env.DEPENDENCIES
if (!deps) {
return
}
Expand All @@ -56,23 +53,3 @@ export function moduleExists(mod: string) {
return false
}
}

/**
* Create necessary indexes of collections
* @param data
* @returns
*/
export async function ensureCollectionIndexes(): Promise<any> {
// init.ts should not import db globally, because init.ts would be referenced in build time
const { DatabaseAgent } = require('./db')
await DatabaseAgent.accessor.ready
const db = DatabaseAgent.db
await db.collection(FUNCTION_LOG_COLLECTION).createIndexes([
{
key: { created_at: 1 },
expireAfterSeconds: Config.FUNCTION_LOG_EXPIRED_TIME,
},
])

return true
}
2 changes: 1 addition & 1 deletion runtimes/nodejs/src/support/cloud-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function createCloudSdk() {
db: DatabaseAgent.accessor.db,
},
sockets: WebSocketAgent.clients,
appid: Config.APP_ID,
appid: Config.APPID,
get env() {
return {
...process.env,
Expand Down
19 changes: 19 additions & 0 deletions runtimes/nodejs/src/support/function-log.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Config from '../config'
import { FUNCTION_LOG_COLLECTION } from '../constants'
import { DatabaseAgent } from '../db'
import { FunctionConsole, FunctionContext } from './function-engine'
Expand All @@ -22,3 +23,21 @@ FunctionConsole.write = (message: string, ctx: FunctionContext) => {

db.collection<IFunctionLog>(FUNCTION_LOG_COLLECTION).insertOne(doc)
}


/**
* Create necessary indexes of collections
* @param data
* @returns
*/
export async function ensureCollectionIndexes(): Promise<any> {
const db = DatabaseAgent.db
await db.collection(FUNCTION_LOG_COLLECTION).createIndexes([
{
key: { created_at: 1 },
expireAfterSeconds: Config.FUNCTION_LOG_EXPIRED_TIME,
},
])

return true
}
5 changes: 2 additions & 3 deletions runtimes/nodejs/src/support/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Config from '../config'
import * as jwt from 'jsonwebtoken'
const DEFAULT_SALT = Config.SERVER_SECRET

/**
* Generate a JWT token
Expand All @@ -16,7 +15,7 @@ const DEFAULT_SALT = Config.SERVER_SECRET
* @returns
*/
export function getToken(payload: any, secret?: string): string {
return jwt.sign(payload, secret ?? DEFAULT_SALT)
return jwt.sign(payload, secret ?? Config.SERVER_SECRET)
}

/**
Expand All @@ -27,7 +26,7 @@ export function getToken(payload: any, secret?: string): string {
export function parseToken(token: string, secret?: string): any | null {
if (!token) return null
try {
const ret = jwt.verify(token, secret ?? DEFAULT_SALT)
const ret = jwt.verify(token, secret ?? Config.SERVER_SECRET)
return ret
} catch (error) {
return null
Expand Down
4 changes: 0 additions & 4 deletions runtimes/nodejs/start.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#!/bin/sh

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

# source .env
echo "****** start service: node $FLAGS --expose-internals --experimental-fetch ./dist/index.js *******"
exec node $FLAGS --expose-internals --experimental-fetch ./dist/index.js
15 changes: 8 additions & 7 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ model PersonalAccessToken {
// region schemas

type RegionClusterConf {
driver String // kubernetes
kubeconfig String?
driver String // kubernetes
kubeconfig String?
npmInstallFlags String @default("")
}

type RegionDatabaseConf {
Expand Down Expand Up @@ -737,9 +738,9 @@ model SmsVerifyCode {
}

model Setting {
id String @id @default(auto()) @map("_id") @db.ObjectId
key String @unique
value String
desc String
metadata Json? // extra meta data
id String @id @default(auto()) @map("_id") @db.ObjectId
key String @unique
value String
desc String
metadata Json? // extra meta data
}
4 changes: 3 additions & 1 deletion server/src/application/application-task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { BucketDomainService } from 'src/gateway/bucket-domain.service'

@Injectable()
export class ApplicationTaskService {
readonly lockTimeout = 60 // in second
readonly lockTimeout = 15 // in second
private readonly logger = new Logger(ApplicationTaskService.name)

constructor(
Expand Down Expand Up @@ -85,6 +85,7 @@ export class ApplicationTaskService {
lockedAt: { $lt: new Date(Date.now() - 1000 * this.lockTimeout) },
},
{ $set: { lockedAt: new Date() } },
{ sort: { lockedAt: 1, updatedAt: 1 } },
)

if (!res.value) return
Expand Down Expand Up @@ -180,6 +181,7 @@ export class ApplicationTaskService {
lockedAt: { $lt: new Date(Date.now() - 1000 * this.lockTimeout) },
},
{ $set: { lockedAt: new Date() } },
{ sort: { lockedAt: 1, updatedAt: 1 } },
)

if (!res.value) return
Expand Down
27 changes: 20 additions & 7 deletions server/src/instance/instance-task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { CronJobService } from 'src/trigger/cron-job.service'

@Injectable()
export class InstanceTaskService {
readonly lockTimeout = 10 // in second
readonly lockTimeout = 15 // in second
private readonly logger = new Logger(InstanceTaskService.name)

constructor(
Expand Down Expand Up @@ -50,7 +50,7 @@ export class InstanceTaskService {
// Phase `Started` -> `Starting`
this.handleRestartingState().catch((err) => {
this.logger.error('handleRestartingPhase error', err)
err?.response && this.logger.debug(err?.response?.data || err?.response)
this.logger.debug(err?.response?.toJSON() || JSON.stringify(err))
})
}

Expand Down Expand Up @@ -93,6 +93,7 @@ export class InstanceTaskService {
lockedAt: { $lt: new Date(Date.now() - 1000 * this.lockTimeout) },
},
{ $set: { lockedAt: new Date() } },
{ sort: { lockedAt: 1, updatedAt: 1 } },
)

if (!res.value) return
Expand Down Expand Up @@ -207,6 +208,7 @@ export class InstanceTaskService {
lockedAt: { $lt: new Date(Date.now() - 1000 * this.lockTimeout) },
},
{ $set: { lockedAt: new Date() } },
{ sort: { lockedAt: 1, updatedAt: 1 } },
)

if (!res.value) return
Expand Down Expand Up @@ -260,10 +262,13 @@ export class InstanceTaskService {
.findOneAndUpdate(
{
state: ApplicationState.Restarting,
phase: ApplicationPhase.Started,
phase: {
$in: [ApplicationPhase.Started, ApplicationPhase.Stopped],
},
lockedAt: { $lt: new Date(Date.now() - 1000 * this.lockTimeout) },
},
{ $set: { lockedAt: new Date() } },
{ sort: { lockedAt: 1, updatedAt: 1 } },
)

if (!res.value) return
Expand All @@ -273,7 +278,12 @@ export class InstanceTaskService {

// update application phase to `Starting`
await db.collection<Application>('Application').updateOne(
{ appid: app.appid, phase: ApplicationPhase.Started },
{
appid: app.appid,
phase: {
$in: [ApplicationPhase.Started, ApplicationPhase.Stopped],
},
},
{
$set: {
phase: ApplicationPhase.Starting,
Expand All @@ -289,9 +299,12 @@ export class InstanceTaskService {
* Relock application by appid, lockedTime is in milliseconds
*/
async relock(appid: string, lockedTime = 0) {
// if lockedTime greater than 3 minutes, set it to 3 minutes
if (lockedTime > 3 * 60 * 1000) {
lockedTime = 3 * 60 * 1000
if (lockedTime <= 2 * 60 * 1000) {
lockedTime = Math.ceil(lockedTime / 10)
}

if (lockedTime > 2 * 60 * 1000) {
lockedTime = this.lockTimeout * 1000
}

const db = SystemDatabase.db
Expand Down
Loading

0 comments on commit 1e60429

Please sign in to comment.