Skip to content

Commit

Permalink
插件崩溃后会周期性的检查插件状态并自动恢复插件
Browse files Browse the repository at this point in the history
  • Loading branch information
Val-istar-Guo committed Sep 1, 2024
1 parent 9da187f commit 98808ee
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-feet-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opendoc/backend": minor
---

插件崩溃后会周期性的检查插件状态并自动恢复插件
2 changes: 1 addition & 1 deletion app/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"ws": "^8.18.0"
},
"devDependencies": {
"@buka/eslint-config": "^2.0.1",
"@buka/eslint-config": "^2.0.2",
"@nestjs/cli": "^10.4.5",
"@nestjs/schematics": "^10.1.4",
"@nestjs/testing": "^10.4.1",
Expand Down
10 changes: 5 additions & 5 deletions app/backend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 33 additions & 9 deletions app/backend/src/modules/plugin/plugin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { PluginCommandMessage } from './dto/plugin-command-message.dto'
import { PluginMetadata } from './dto/plugin-command-message/plugin-metadata.dto'
import { PluginEventMessageDataMap } from './types/plugin-event-message-data-map'
import { PluginConfig } from '~/config/plugin.config'
import { pe } from '~/utils/pe'


@Injectable()
Expand All @@ -47,7 +48,7 @@ export class PluginService implements OnModuleInit, OnApplicationShutdown {
private readonly pluginOptionRepo: EntityRepository<PluginOption>,
) {}

async onModuleInit() {
async onModuleInit(): Promise<void> {
await this.consistency()
}

Expand All @@ -58,7 +59,7 @@ export class PluginService implements OnModuleInit, OnApplicationShutdown {
*/
@Cron('*/10 * * * * *')
@EnsureRequestContext()
private async consistency() {
private async consistency(): Promise<void> {
const plugins = await this.pluginRepo.find({
status: PluginStatus.ENABLED,
})
Expand All @@ -80,7 +81,7 @@ export class PluginService implements OnModuleInit, OnApplicationShutdown {
}
}

onApplicationShutdown() {
onApplicationShutdown(): void {
for (const ws of this.webSocketMap.values()) {
ws.close()
}
Expand All @@ -96,27 +97,50 @@ export class PluginService implements OnModuleInit, OnApplicationShutdown {
ws.on('message', (data: Buffer) => {
const message: PluginCommandMessage = JSON.parse(data.toString())

this.logger.debug(`!!!!!!! plugin.command.${message.command} !!!!!!!`,)
this.logger.debug(`!!!!!!! plugin.command.${message.command} !!!!!!!`)
this.eventEmitter.emit(
`plugin.command.${message.command}`,
new PluginCommandEvent(
wrap(plugin).serialize(),
message.command,
message.data
)
message.data,
),
)
})

this.webSocketMap.set(plugin.id, ws)

break
} catch (e) {
} catch (err) {
plugin.status = PluginStatus.BREAKDOWN
this.logger.error(`Cannot connect to plugin: ${plugin.url}`)
this.logger.error(`Cannot connect to plugin ${plugin.url}: ${pe(err)}`)
}
}
}

@Cron('0 * * * * *')
@EnsureRequestContext()
async recoverBreakdownPlugins(): Promise<void> {
const plugins = await this.pluginRepo.find({
status: PluginStatus.BREAKDOWN,
})
if (plugins.length === 0) return

this.logger.debug(`Recover ${plugins.length} breakdown plugins`)

for (const plugin of plugins) {
try {
const [ws] = await this.webSocketService.connect(plugin.url)
ws.close()
plugin.status = PluginStatus.ENABLED
} catch (err) {
continue
}
}

await this.em.persistAndFlush(plugins)
}

async queryAll(dto: QueryPluginsDTO): Promise<ResponseOfQueryPluginsDTO> {
const qb = this.pluginRepo.createQueryBuilder('plugin')
.leftJoinAndSelect('plugin.options', 'options')
Expand Down Expand Up @@ -253,7 +277,7 @@ export class PluginService implements OnModuleInit, OnApplicationShutdown {
await this.webSocketService.send(ws, event, fullData)
}

async updateMetadata(pluginId: string, metadata: PluginMetadata) {
async updateMetadata(pluginId: string, metadata: PluginMetadata): Promise<void> {
const plugin = await this.pluginRepo.findOneOrFail(pluginId)

this.validateApiVersion(metadata.apiVersion)
Expand Down
4 changes: 2 additions & 2 deletions app/backend/src/modules/plugin/web-socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class WebSocketService {
reject(new BadRequestException('Timeout'))
}, ttl)

function onMessage(data: Buffer, isBuffer: boolean) {
function onMessage(data: Buffer, isBuffer: boolean): void {
if (!isBuffer) {
const message = <PluginCommandMessage>JSON.parse(data.toString())
if (message.command === PluginCommandName.JOIN) {
Expand All @@ -57,7 +57,7 @@ export class WebSocketService {
}
}

function onError(err: Error) {
function onError(err: Error): void {
logger.error(`Plugin ${url} is breakdown`)
clearTimeout(ttlHandler)
ws.close()
Expand Down
2 changes: 1 addition & 1 deletion app/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"ws": "^8.18.0"
},
"devDependencies": {
"@buka/eslint-config": "^2.0.1",
"@buka/eslint-config": "^2.0.2",
"@nestjs/cli": "^10.4.5",
"@nestjs/schematics": "^10.1.4",
"@nestjs/testing": "^10.4.1",
Expand Down
10 changes: 5 additions & 5 deletions app/compiler/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"postinstall": "nuxt prepare"
},
"devDependencies": {
"@buka/eslint-config": "^2.0.1",
"@buka/eslint-config": "^2.0.2",
"@catppuccin/tailwindcss": "^0.1.6",
"@nuxt/devtools": "^1.4.1",
"@nuxt/eslint": "^0.5.4",
Expand Down
44 changes: 32 additions & 12 deletions app/frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/register/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"test:update": "jest --updateSnapshot"
},
"devDependencies": {
"@buka/eslint-config": "^2.0.1",
"@buka/eslint-config": "^2.0.2",
"@jest/globals": "^29.7.0",
"@types/fs-extra": "^11.0.4",
"@types/node": "^20.16.2",
Expand Down
10 changes: 5 additions & 5 deletions pkg/register/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"test:update": "jest --updateSnapshot"
},
"devDependencies": {
"@buka/eslint-config": "^2.0.1",
"@buka/eslint-config": "^2.0.2",
"@jest/globals": "^29.7.0",
"@types/fs-extra": "^11.0.4",
"@types/node": "^20.16.2",
Expand Down
Loading

0 comments on commit 98808ee

Please sign in to comment.