fix(server): cannot restart stopped app (#1129)

This commit is contained in:
maslow 2023-05-12 19:08:25 +08:00 committed by GitHub
parent de080a888b
commit 5477994ea1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 8 deletions

View File

@ -18,7 +18,11 @@ import { ApplicationService } from './application.service'
import { FunctionService } from '../function/function.service'
import { StorageService } from 'src/storage/storage.service'
import { RegionService } from 'src/region/region.service'
import { SubscriptionPhase } from '@prisma/client'
import {
ApplicationPhase,
ApplicationState,
SubscriptionPhase,
} from '@prisma/client'
@ApiTags('Application')
@Controller('applications')
@ -139,6 +143,39 @@ export class ApplicationController {
return ResponseUtil.error('subscription has expired, you can not update')
}
// check: only running application can restart
if (
dto.state === ApplicationState.Restarting &&
app.state !== ApplicationState.Running &&
app.phase !== ApplicationPhase.Started
) {
return ResponseUtil.error(
'The application is not running, can not restart it',
)
}
// check: only running application can stop
if (
dto.state === ApplicationState.Stopped &&
app.state !== ApplicationState.Running &&
app.phase !== ApplicationPhase.Started
) {
return ResponseUtil.error(
'The application is not running, can not stop it',
)
}
// check: only stopped application can start
if (
dto.state === ApplicationState.Running &&
app.state !== ApplicationState.Stopped &&
app.phase !== ApplicationPhase.Stopped
) {
return ResponseUtil.error(
'The application is not stopped, can not start it',
)
}
// update app
const res = await this.appService.update(appid, dto)
if (res === null) {

View File

@ -26,7 +26,7 @@ export class InstanceService {
private readonly storageService: StorageService,
private readonly databaseService: DatabaseService,
private readonly prisma: PrismaService,
) { }
) {}
async create(app: Application) {
const appid = app.appid
@ -176,18 +176,23 @@ export class InstanceService {
},
})
const { deployment } = await this.get(app)
deployment.spec = await this.makeDeploymentSpec(app, deployment.spec.template.metadata.labels)
deployment.spec = await this.makeDeploymentSpec(
app,
deployment.spec.template.metadata.labels,
)
const region = await this.regionService.findByAppId(app.appid)
const appsV1Api = this.clusterService.makeAppsV1Api(region)
const namespace = GetApplicationNamespaceByAppId(app.appid)
const res = await appsV1Api.replaceNamespacedDeployment(app.appid, namespace, deployment)
const res = await appsV1Api.replaceNamespacedDeployment(
app.appid,
namespace,
deployment,
)
this.logger.log(`restart k8s deployment ${res.body?.metadata?.name}`)
}
async makeDeploymentSpec(app: any, labels: any): Promise<V1DeploymentSpec> {
// prepare params
const limitMemory = app.bundle.resource.limitMemory
const limitCpu = app.bundle.resource.limitCPU
@ -227,8 +232,9 @@ export class InstanceService {
},
{ name: 'DEPENDENCIES', value: dependencies_string },
{
name: 'RESTART_AT', value: new Date().getTime().toString(),
}
name: 'RESTART_AT',
value: new Date().getTime().toString(),
},
]
// merge env from app configuration, override if exists