diff --git a/server/src/initializer/initializer.service.ts b/server/src/initializer/initializer.service.ts index 91ebfcf..d01aea2 100644 --- a/server/src/initializer/initializer.service.ts +++ b/server/src/initializer/initializer.service.ts @@ -371,6 +371,12 @@ export class InitializerService { value: 'https://doc.laf.run/zh/', desc: 'laf doc site url', }, + { + public: false, + key: SettingKey.AppCreateTimeOut, + value: '15', + desc: 'timeout for application creation in minutes', + }, ]) this.logger.verbose('Created default settings') diff --git a/server/src/instance/instance-task.service.ts b/server/src/instance/instance-task.service.ts index 2e0212d..e19cd53 100644 --- a/server/src/instance/instance-task.service.ts +++ b/server/src/instance/instance-task.service.ts @@ -17,6 +17,7 @@ import { DedicatedDatabasePhase, DedicatedDatabaseState, } from 'src/database/entities/dedicated-database' +import { Setting, SettingKey } from 'src/setting/entities/setting' @Injectable() export class InstanceTaskService { @@ -96,6 +97,11 @@ export class InstanceTaskService { async handleStartingPhase() { const db = SystemDatabase.db + const appCreateTimeConf = await db.collection('Setting').findOne({ + key: SettingKey.AppCreateTimeOut, + public: false, + }) + const res = await db .collection('Application') .findOneAndUpdate( @@ -112,38 +118,42 @@ export class InstanceTaskService { const waitingTime = Date.now() - app.updatedAt.getTime() // if waiting time is more than 10 minutes, stop the application - if (waitingTime > 1000 * 60 * 10) { - await db.collection('Application').updateOne( - { appid: app.appid }, - { - $set: { - state: ApplicationState.Stopped, - phase: ApplicationPhase.Stopping, - lockedAt: TASK_LOCK_INIT_TIME, - updatedAt: new Date(), - }, - }, - ) + if (appCreateTimeConf?.value) { + const appCreateTimeOut = parseInt(appCreateTimeConf.value) * 60 * 1000 - // if databse operation success but runtime failed - await db - .collection('DedicatedDatabase') - .findOneAndUpdate( - { - appid: app.appid, - state: DedicatedDatabaseState.Running, - phase: DedicatedDatabasePhase.Started, - }, + if (waitingTime > appCreateTimeOut) { + await db.collection('Application').updateOne( + { appid: app.appid }, { $set: { - state: DedicatedDatabaseState.Stopped, - phase: DedicatedDatabasePhase.Stopping, + state: ApplicationState.Stopped, + phase: ApplicationPhase.Stopping, + lockedAt: TASK_LOCK_INIT_TIME, + updatedAt: new Date(), }, }, ) - this.logger.log(`${app.appid} updated to state Stopped due to timeout`) - return + // if databse operation success but runtime failed + await db + .collection('DedicatedDatabase') + .findOneAndUpdate( + { + appid: app.appid, + state: DedicatedDatabaseState.Running, + phase: DedicatedDatabasePhase.Started, + }, + { + $set: { + state: DedicatedDatabaseState.Stopped, + phase: DedicatedDatabasePhase.Stopping, + }, + }, + ) + + this.logger.log(`${app.appid} updated to state Stopped due to timeout`) + return + } } const appid = app.appid diff --git a/server/src/setting/entities/setting.ts b/server/src/setting/entities/setting.ts index 2199dc3..5b84f00 100644 --- a/server/src/setting/entities/setting.ts +++ b/server/src/setting/entities/setting.ts @@ -18,6 +18,8 @@ export enum SettingKey { LafWeChatUrl = 'laf_wechat_url', LafAboutUsUrl = 'laf_about_us_url', LafDocUrl = 'laf_doc_url', + + AppCreateTimeOut = 'app_create_timeout', } export class Setting {