refactor(app.queries): automatically inject db dependency
This commit is contained in:
parent
c736fff601
commit
900c31ec19
3 changed files with 37 additions and 15 deletions
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue