Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): cannot restart stopped app #1129

Merged
merged 1 commit into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion server/src/application/application.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 13 additions & 7 deletions server/src/instance/instance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down