Skip to content

Commit

Permalink
feat: add vlmcsd plugin with conditional loading and process manage
Browse files Browse the repository at this point in the history
  • Loading branch information
ikxin committed Jan 6, 2025
1 parent a8ba728 commit 55de1c3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NUXT_PUBLIC_API_URL=https://kms.ikxin.com
NUXT_PUBLIC_I18N_BASE_URL=https://kms.ikxin.com
ENABLE_VLMCSD=false
29 changes: 29 additions & 0 deletions server/plugins/vlmcsd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { spawn } from 'child_process'
import { join } from 'path'
import { arch, platform } from 'os'
import { existsSync } from 'fs'

export default defineNitroPlugin(nitro => {
if (process.env.ENABLE_VLMCSD !== 'true') {
console.log('Vlmcsd is disabled')
return
}

const vlmcsdPath = join(
process.cwd(),
'binaries',
`vlmcsd-${platform()}-${arch()}`
)

if (!existsSync(vlmcsdPath)) {
console.log('Vlmcsd binary not found')
return
}

const vlmcsd = spawn(vlmcsdPath, ['-D'], { stdio: 'inherit' })

nitro.hooks.hookOnce('close', () => {
vlmcsd.removeAllListeners()
vlmcsd.kill()
})
})
36 changes: 18 additions & 18 deletions server/tasks/monitor.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
export default defineTask({
meta: {
name: "monitor",
description: "Run KMS server monitoring.",
name: 'monitor',
description: 'Run KMS server monitoring.',
},
async run() {
const results = await Promise.all(
monitorList.map(async (host) => {
let monitorData = await storage.getItem<MonitorData[]>(`${host}.json`);
monitorList.map(async host => {
let monitorData = await storage.getItem<MonitorData[]>(`${host}.json`)

if (!Array.isArray(monitorData)) {
monitorData = [];
monitorData = []
}

if (monitorData.length >= 100) {
monitorData.shift();
monitorData.shift()
}

const { status, delay } = await runVlmcs({ host });
const { status, delay } = await runVlmcs({ host })

monitorData.push({
status,
time: Date.now(),
delay,
});
})

await storage.setItem<MonitorData[]>(`${host}.json`, monitorData);
await storage.setItem<MonitorData[]>(`${host}.json`, monitorData)

return { host, status };
return { host, status }
})
);
)

const count = results.filter((item) => {
const count = results.filter(item => {
if (!item.status) {
console.log("Monitor failed:", item.host);
console.log('Monitor failed:', item.host)
}
return item.status;
}).length;
return item.status
}).length

console.log(
new Date().toISOString(),
`Monitor task completed, successful: ${count}`
);
)

return { result: "Done" };
return { result: 'Done' }
},
});
})

0 comments on commit 55de1c3

Please sign in to comment.