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