Skip to content

Commit

Permalink
fix(server): change app create timeout logic (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
HUAHUAI23 authored Nov 26, 2024
1 parent dd6ef3f commit bfb0ddf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
6 changes: 6 additions & 0 deletions server/src/initializer/initializer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
60 changes: 35 additions & 25 deletions server/src/instance/instance-task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -96,6 +97,11 @@ export class InstanceTaskService {
async handleStartingPhase() {
const db = SystemDatabase.db

const appCreateTimeConf = await db.collection<Setting>('Setting').findOne({
key: SettingKey.AppCreateTimeOut,
public: false,
})

const res = await db
.collection<Application>('Application')
.findOneAndUpdate(
Expand All @@ -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>('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>('DedicatedDatabase')
.findOneAndUpdate(
{
appid: app.appid,
state: DedicatedDatabaseState.Running,
phase: DedicatedDatabasePhase.Started,
},
if (waitingTime > appCreateTimeOut) {
await db.collection<Application>('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>('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
Expand Down
2 changes: 2 additions & 0 deletions server/src/setting/entities/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit bfb0ddf

Please sign in to comment.