Skip to content

Commit 5477994

Browse files
authored
fix(server): cannot restart stopped app (#1129)
1 parent de080a8 commit 5477994

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

server/src/application/application.controller.ts

+38-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import { ApplicationService } from './application.service'
1818
import { FunctionService } from '../function/function.service'
1919
import { StorageService } from 'src/storage/storage.service'
2020
import { RegionService } from 'src/region/region.service'
21-
import { SubscriptionPhase } from '@prisma/client'
21+
import {
22+
ApplicationPhase,
23+
ApplicationState,
24+
SubscriptionPhase,
25+
} from '@prisma/client'
2226

2327
@ApiTags('Application')
2428
@Controller('applications')
@@ -139,6 +143,39 @@ export class ApplicationController {
139143
return ResponseUtil.error('subscription has expired, you can not update')
140144
}
141145

146+
// check: only running application can restart
147+
if (
148+
dto.state === ApplicationState.Restarting &&
149+
app.state !== ApplicationState.Running &&
150+
app.phase !== ApplicationPhase.Started
151+
) {
152+
return ResponseUtil.error(
153+
'The application is not running, can not restart it',
154+
)
155+
}
156+
157+
// check: only running application can stop
158+
if (
159+
dto.state === ApplicationState.Stopped &&
160+
app.state !== ApplicationState.Running &&
161+
app.phase !== ApplicationPhase.Started
162+
) {
163+
return ResponseUtil.error(
164+
'The application is not running, can not stop it',
165+
)
166+
}
167+
168+
// check: only stopped application can start
169+
if (
170+
dto.state === ApplicationState.Running &&
171+
app.state !== ApplicationState.Stopped &&
172+
app.phase !== ApplicationPhase.Stopped
173+
) {
174+
return ResponseUtil.error(
175+
'The application is not stopped, can not start it',
176+
)
177+
}
178+
142179
// update app
143180
const res = await this.appService.update(appid, dto)
144181
if (res === null) {

server/src/instance/instance.service.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class InstanceService {
2626
private readonly storageService: StorageService,
2727
private readonly databaseService: DatabaseService,
2828
private readonly prisma: PrismaService,
29-
) { }
29+
) {}
3030

3131
async create(app: Application) {
3232
const appid = app.appid
@@ -176,18 +176,23 @@ export class InstanceService {
176176
},
177177
})
178178
const { deployment } = await this.get(app)
179-
deployment.spec = await this.makeDeploymentSpec(app, deployment.spec.template.metadata.labels)
179+
deployment.spec = await this.makeDeploymentSpec(
180+
app,
181+
deployment.spec.template.metadata.labels,
182+
)
180183
const region = await this.regionService.findByAppId(app.appid)
181184
const appsV1Api = this.clusterService.makeAppsV1Api(region)
182185
const namespace = GetApplicationNamespaceByAppId(app.appid)
183-
const res = await appsV1Api.replaceNamespacedDeployment(app.appid, namespace, deployment)
186+
const res = await appsV1Api.replaceNamespacedDeployment(
187+
app.appid,
188+
namespace,
189+
deployment,
190+
)
184191

185192
this.logger.log(`restart k8s deployment ${res.body?.metadata?.name}`)
186-
187193
}
188194

189195
async makeDeploymentSpec(app: any, labels: any): Promise<V1DeploymentSpec> {
190-
191196
// prepare params
192197
const limitMemory = app.bundle.resource.limitMemory
193198
const limitCpu = app.bundle.resource.limitCPU
@@ -227,8 +232,9 @@ export class InstanceService {
227232
},
228233
{ name: 'DEPENDENCIES', value: dependencies_string },
229234
{
230-
name: 'RESTART_AT', value: new Date().getTime().toString(),
231-
}
235+
name: 'RESTART_AT',
236+
value: new Date().getTime().toString(),
237+
},
232238
]
233239

234240
// merge env from app configuration, override if exists

0 commit comments

Comments
 (0)