mirror of
https://github.com/pockethost/pockethost.git
synced 2025-07-09 22:32:31 +00:00
chore: instanceService refactor
This commit is contained in:
parent
93353f8570
commit
337c2818a7
@ -3,7 +3,7 @@ import { DEBUG, PH_BIN_CACHE, PUBLIC_PB_SUBDOMAIN } from './constants'
|
||||
import { clientService } from './db/PbClient'
|
||||
import { createBackupService } from './services/BackupService'
|
||||
import { ftpService } from './services/FtpService/FtpService'
|
||||
import { createInstanceService } from './services/InstanceService'
|
||||
import { instanceService } from './services/InstanceService'
|
||||
import { pocketbase } from './services/PocketBaseService'
|
||||
import { createProxyService } from './services/ProxyService'
|
||||
import { rpcService } from './services/RpcService'
|
||||
@ -36,9 +36,8 @@ global.EventSource = require('eventsource')
|
||||
|
||||
ftpService({})
|
||||
await rpcService({})
|
||||
const instanceService = await createInstanceService({})
|
||||
await instanceService({})
|
||||
const proxyService = await createProxyService({
|
||||
instanceManager: instanceService,
|
||||
coreInternalUrl: url,
|
||||
})
|
||||
const backupService = await createBackupService()
|
||||
@ -50,7 +49,7 @@ global.EventSource = require('eventsource')
|
||||
info(`Shutting down`)
|
||||
ftpService().shutdown()
|
||||
proxyService.shutdown()
|
||||
instanceService.shutdown()
|
||||
;(await instanceService()).shutdown()
|
||||
;(await rpcService()).shutdown()
|
||||
pbService.shutdown()
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import {
|
||||
InstanceId,
|
||||
InstanceStatus,
|
||||
logger,
|
||||
mkSingleton,
|
||||
RpcCommands,
|
||||
} from '@pockethost/common'
|
||||
import { forEachRight, map } from '@s-libs/micro-dash'
|
||||
@ -36,8 +37,9 @@ type InstanceApi = {
|
||||
|
||||
export type InstanceServiceConfig = {}
|
||||
|
||||
export type InstanceServiceApi = AsyncReturnType<typeof createInstanceService>
|
||||
export const createInstanceService = async (config: InstanceServiceConfig) => {
|
||||
export type InstanceServiceApi = AsyncReturnType<typeof instanceService>
|
||||
export const instanceService = mkSingleton(
|
||||
async (config: InstanceServiceConfig) => {
|
||||
const { dbg, raw, error, warn } = logger().create('InstanceService')
|
||||
const client = await clientService()
|
||||
|
||||
@ -180,7 +182,9 @@ export const createInstanceService = async (config: InstanceServiceConfig) => {
|
||||
{
|
||||
tm.repeat(
|
||||
safeCatch(`idleCheck`, async () => {
|
||||
raw(`${subdomain} idle check: ${openRequestCount} open requests`)
|
||||
raw(
|
||||
`${subdomain} idle check: ${openRequestCount} open requests`
|
||||
)
|
||||
if (
|
||||
openRequestCount === 0 &&
|
||||
lastRequest + DAEMON_PB_IDLE_TTL < now()
|
||||
@ -191,7 +195,9 @@ export const createInstanceService = async (config: InstanceServiceConfig) => {
|
||||
await _api.shutdown()
|
||||
return false
|
||||
} else {
|
||||
raw(`${openRequestCount} requests remain open on ${subdomain}`)
|
||||
raw(
|
||||
`${openRequestCount} requests remain open on ${subdomain}`
|
||||
)
|
||||
}
|
||||
return true
|
||||
}),
|
||||
@ -238,3 +244,4 @@ export const createInstanceService = async (config: InstanceServiceConfig) => {
|
||||
const maintenance = async (instanceId: InstanceId) => {}
|
||||
return { getInstance, shutdown, maintenance }
|
||||
}
|
||||
)
|
||||
|
@ -7,20 +7,22 @@ import {
|
||||
PUBLIC_APP_PROTOCOL,
|
||||
PUBLIC_PB_SUBDOMAIN,
|
||||
} from '../constants'
|
||||
import { InstanceServiceApi } from './InstanceService'
|
||||
import { instanceService } from './InstanceService'
|
||||
|
||||
export type ProxyServiceApi = AsyncReturnType<typeof createProxyService>
|
||||
|
||||
export type ProxyServiceConfig = {
|
||||
coreInternalUrl: string
|
||||
instanceManager: InstanceServiceApi
|
||||
}
|
||||
export const createProxyService = async (config: ProxyServiceConfig) => {
|
||||
const { dbg, error, info } = logger().create('ProxyService')
|
||||
|
||||
const { instanceManager, coreInternalUrl } = config
|
||||
const { coreInternalUrl } = config
|
||||
|
||||
const proxy = httpProxy.createProxyServer({})
|
||||
|
||||
const { getInstance } = await instanceService()
|
||||
|
||||
const server = createServer(async (req, res) => {
|
||||
dbg(`Incoming request ${req.headers.host}/${req.url}`)
|
||||
|
||||
@ -48,7 +50,7 @@ export const createProxyService = async (config: ProxyServiceConfig) => {
|
||||
return
|
||||
}
|
||||
|
||||
const instance = await instanceManager.getInstance(subdomain)
|
||||
const instance = await getInstance(subdomain)
|
||||
if (!instance) {
|
||||
throw new Error(
|
||||
`${host} not found. Please check the instance URL and try again, or create one at ${PUBLIC_APP_PROTOCOL}://${PUBLIC_APP_DOMAIN}.`
|
||||
@ -83,7 +85,6 @@ export const createProxyService = async (config: ProxyServiceConfig) => {
|
||||
resolve()
|
||||
})
|
||||
server.closeAllConnections()
|
||||
instanceManager.shutdown()
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user