From 2d2ce8c309278df01df5f4c04ea97ee63cc97912 Mon Sep 17 00:00:00 2001 From: Ben Allfree Date: Sat, 23 Nov 2024 20:37:25 -0800 Subject: [PATCH] instance idle ttl support --- .changeset/light-laws-peel.md | 5 +++ .../pockethost/src/common/schema/Instance.ts | 1 + .../1732422451_updated_instances.js | 31 +++++++++++++++++++ .../src/services/InstanceService/index.ts | 5 +-- 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 .changeset/light-laws-peel.md create mode 100644 packages/pockethost/src/mothership-app/pb_migrations/1732422451_updated_instances.js diff --git a/.changeset/light-laws-peel.md b/.changeset/light-laws-peel.md new file mode 100644 index 00000000..d52e4697 --- /dev/null +++ b/.changeset/light-laws-peel.md @@ -0,0 +1,5 @@ +--- +'pockethost': minor +--- + +Enh: Added custom instance idle TTL support diff --git a/packages/pockethost/src/common/schema/Instance.ts b/packages/pockethost/src/common/schema/Instance.ts index 63b198f8..0a6c064d 100644 --- a/packages/pockethost/src/common/schema/Instance.ts +++ b/packages/pockethost/src/common/schema/Instance.ts @@ -33,6 +33,7 @@ export type InstanceFields = BaseFields & { dev: boolean cname_active: boolean notifyMaintenanceMode: boolean + idleTtl: number } & TExtra export type WithUser = { diff --git a/packages/pockethost/src/mothership-app/pb_migrations/1732422451_updated_instances.js b/packages/pockethost/src/mothership-app/pb_migrations/1732422451_updated_instances.js new file mode 100644 index 00000000..6416e4f1 --- /dev/null +++ b/packages/pockethost/src/mothership-app/pb_migrations/1732422451_updated_instances.js @@ -0,0 +1,31 @@ +/// +migrate((db) => { + const dao = new Dao(db) + const collection = dao.findCollectionByNameOrId("etae8tuiaxl6xfv") + + // add + collection.schema.addField(new SchemaField({ + "system": false, + "id": "gbfsfi0g", + "name": "idleTtl", + "type": "number", + "required": false, + "presentable": false, + "unique": false, + "options": { + "min": null, + "max": null, + "noDecimal": false + } + })) + + return dao.saveCollection(collection) +}, (db) => { + const dao = new Dao(db) + const collection = dao.findCollectionByNameOrId("etae8tuiaxl6xfv") + + // remove + collection.schema.removeField("gbfsfi0g") + + return dao.saveCollection(collection) +}) diff --git a/packages/pockethost/src/services/InstanceService/index.ts b/packages/pockethost/src/services/InstanceService/index.ts index f8f4882b..bd347204 100644 --- a/packages/pockethost/src/services/InstanceService/index.ts +++ b/packages/pockethost/src/services/InstanceService/index.ts @@ -181,13 +181,14 @@ export const instanceService = mkSingleton( }) /** Idle check */ + const idleTtl = instance.idleTtl || DAEMON_PB_IDLE_TTL() const idleTid = setInterval(() => { const lastRequestAge = now() - lastRequest dbg( `idle check: ${openRequestCount} open requests, ${lastRequestAge}ms since last request`, ) - if (openRequestCount === 0 && lastRequestAge > DAEMON_PB_IDLE_TTL()) { - info(`idle for ${DAEMON_PB_IDLE_TTL()}, shutting down`) + if (openRequestCount === 0 && lastRequestAge > idleTtl) { + info(`idle for ${idleTtl}, shutting down`) userInstanceLogger.info( `Instance has been idle for ${DAEMON_PB_IDLE_TTL()}ms. Hibernating to conserve resources.`, )