From 900c31ec198c091c22f7fd0aee72dfab88f6336d Mon Sep 17 00:00:00 2001 From: Nicolas Meienberger Date: Sun, 12 Nov 2023 12:29:29 +0100 Subject: [PATCH] refactor(app.queries): automatically inject db dependency --- .env.example | 2 +- src/server/queries/apps/apps.queries.ts | 4 +-- src/server/services/apps/apps.service.ts | 46 +++++++++++++++++------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/.env.example b/.env.example index bf6fb542..72d92d40 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,4 @@ -APPS_REPO_ID=7a92c8307e0a8074763c80be1fcfa4f87da6641daea9211aea6743b0116aba3b +APPS_REPO_ID=29ca930bfdaffa1dfabf5726336380ede7066bc53297e3c0c868b27c97282903 APPS_REPO_URL=https://github.com/runtipi/runtipi-appstore TZ=Etc/UTC INTERNAL_IP=localhost diff --git a/src/server/queries/apps/apps.queries.ts b/src/server/queries/apps/apps.queries.ts index 1a75360f..ecea36fc 100644 --- a/src/server/queries/apps/apps.queries.ts +++ b/src/server/queries/apps/apps.queries.ts @@ -1,11 +1,11 @@ import { and, asc, eq, ne, notInArray } from 'drizzle-orm'; -import { Database } from '@/server/db'; +import { Database, db } from '@/server/db'; import { appTable, NewApp, AppStatus } from '../../db/schema'; export class AppQueries { private db; - constructor(p: Database) { + constructor(p: Database = db) { this.db = p; } diff --git a/src/server/services/apps/apps.service.ts b/src/server/services/apps/apps.service.ts index 62e741b8..adb79e6a 100644 --- a/src/server/services/apps/apps.service.ts +++ b/src/server/services/apps/apps.service.ts @@ -6,6 +6,7 @@ import { Database } from '@/server/db'; import { AppInfo } from '@runtipi/shared'; import { EventDispatcher } from '@/server/core/EventDispatcher/EventDispatcher'; import { castAppConfig } from '@/lib/helpers/castAppConfig'; +import { revalidatePath } from 'next/cache'; import { checkAppRequirements, getAvailableApps, getAppInfo, getUpdateInfo } from './apps.helpers'; import { getConfig } from '../../core/TipiConfig'; import { Logger } from '../../core/Logger'; @@ -32,7 +33,7 @@ const filterApps = (apps: AppInfo[]): AppInfo[] => apps.sort(sortApps).filter(fi export class AppServiceClass { private queries; - constructor(p: Database) { + constructor(p?: Database) { this.queries = new AppQueries(p); } @@ -76,29 +77,34 @@ export class AppServiceClass { * This function starts an app specified by its appName, regenerates its environment file and checks for missing requirements. * It updates the app's status in the database to 'starting' and 'running' if the start process is successful, otherwise it updates the status to 'stopped'. * - * @param {string} appName - The name of the app to start + * @param {string} id - The name of the app to start * @throws {Error} - If the app is not found or the start process fails. */ - public startApp = async (appName: string) => { - const app = await this.queries.getApp(appName); + public startApp = async (id: string) => { + const app = await this.queries.getApp(id); if (!app) { - throw new TranslatedError('server-messages.errors.app-not-found', { id: appName }); + throw new TranslatedError('server-messages.errors.app-not-found', { id }); } - await this.queries.updateApp(appName, { status: 'starting' }); + await this.queries.updateApp(id, { status: 'starting' }); + + revalidatePath('/apps'); + revalidatePath(`/app/${id}`); + revalidatePath(`/app-store/${id}`); + const eventDispatcher = new EventDispatcher('startApp'); - const { success, stdout } = await eventDispatcher.dispatchEventAsync({ type: 'app', command: 'start', appid: appName, form: castAppConfig(app.config) }); + const { success, stdout } = await eventDispatcher.dispatchEventAsync({ type: 'app', command: 'start', appid: id, form: castAppConfig(app.config) }); await eventDispatcher.close(); if (success) { - await this.queries.updateApp(appName, { status: 'running' }); + await this.queries.updateApp(id, { status: 'running' }); } else { - await this.queries.updateApp(appName, { status: 'stopped' }); - Logger.error(`Failed to start app ${appName}: ${stdout}`); - throw new TranslatedError('server-messages.errors.app-failed-to-start', { id: appName }); + await this.queries.updateApp(id, { status: 'stopped' }); + Logger.error(`Failed to start app ${id}: ${stdout}`); + throw new TranslatedError('server-messages.errors.app-failed-to-start', { id }); } - const updatedApp = await this.queries.getApp(appName); + const updatedApp = await this.queries.getApp(id); return updatedApp; }; @@ -164,6 +170,10 @@ export class AppServiceClass { isVisibleOnGuestDashboard, }); + revalidatePath('/apps'); + revalidatePath(`/app/${id}`); + revalidatePath(`/app-store/${id}`); + // Run script const eventDispatcher = new EventDispatcher('installApp'); const { success, stdout } = await eventDispatcher.dispatchEventAsync({ type: 'app', command: 'install', appid: id, form }); @@ -263,6 +273,10 @@ export class AppServiceClass { // Run script await this.queries.updateApp(id, { status: 'stopping' }); + revalidatePath('/apps'); + revalidatePath(`/app/${id}`); + revalidatePath(`/app-store/${id}`); + const eventDispatcher = new EventDispatcher('stopApp'); const { success, stdout } = await eventDispatcher.dispatchEventAsync({ type: 'app', command: 'stop', appid: id, form: castAppConfig(app.config) }); await eventDispatcher.close(); @@ -297,6 +311,10 @@ export class AppServiceClass { await this.queries.updateApp(id, { status: 'uninstalling' }); + revalidatePath('/apps'); + revalidatePath(`/app/${id}`); + revalidatePath(`/app-store/${id}`); + const eventDispatcher = new EventDispatcher('uninstallApp'); const { success, stdout } = await eventDispatcher.dispatchEventAsync({ type: 'app', command: 'uninstall', appid: id, form: castAppConfig(app.config) }); await eventDispatcher.close(); @@ -348,6 +366,10 @@ export class AppServiceClass { await this.queries.updateApp(id, { status: 'updating' }); + revalidatePath('/apps'); + revalidatePath(`/app/${id}`); + revalidatePath(`/app-store/${id}`); + const eventDispatcher = new EventDispatcher('updateApp'); const { success, stdout } = await eventDispatcher.dispatchEventAsync({ type: 'app', command: 'update', appid: id, form: castAppConfig(app.config) }); await eventDispatcher.close();