mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-10-30 23:25:24 +00:00
feat(automations): add per-host maintenance mode and recurring schedules (#1521)
This commit is contained in:
1 parent
fd91d4925a
commit
99cffc9ba6
26 files changed
+1813
-6
No files matched your search
@@ -9,6 +9,9 @@ import { registerRoutes } from "./routes.js";
|
||||
import { createScheduler, STARTUP_DELAY_MS, TICK_MS } from "./scheduler.js";
|
||||
import { createAutomationsService } from "./service.js";
|
||||
import { createTriggers } from "./triggers.js";
|
||||
import { createMaintenanceRepository } from "./maintenance-repository.js";
|
||||
import { createMaintenanceService } from "./maintenance-service.js";
|
||||
import { registerMaintenanceRoutes } from "./maintenance-routes.js";
|
||||
|
||||
export type { AutomationsAccessV1, AutomationSummary } from "./service.js";
|
||||
|
||||
@@ -17,9 +20,30 @@ export async function activate(ctx: PluginContext) {
|
||||
const channels = await ctx.notify.channels();
|
||||
return channels.map((channel) => channel.id);
|
||||
});
|
||||
const maintenance = createMaintenanceService(
|
||||
ctx,
|
||||
await createMaintenanceRepository(ctx.db),
|
||||
async (userId, hostId) => {
|
||||
for (const state of await repository.listOpenBreaches()) {
|
||||
if (Number(state.stateKey.split(":")[0]) !== hostId) continue;
|
||||
const automation = await repository.findById(state.automationId);
|
||||
if (automation?.userId === userId)
|
||||
await repository.clearBreach(state.automationId, state.stateKey);
|
||||
}
|
||||
},
|
||||
);
|
||||
ctx.events.on("user.data_wiped", (payload) => {
|
||||
const userId = (payload as { userId?: string } | undefined)?.userId;
|
||||
if (userId) return maintenance.wipeUser(userId);
|
||||
});
|
||||
const deps = createDeps(ctx.services);
|
||||
const engine = new AutomationEngine(ctx, repository, deps);
|
||||
const triggers = createTriggers(repository, engine, ctx.log);
|
||||
const triggers = createTriggers(
|
||||
repository,
|
||||
engine,
|
||||
ctx.log,
|
||||
maintenance.isMaintaining,
|
||||
);
|
||||
triggers.subscribe(ctx.events);
|
||||
|
||||
const viewers = createHeadlessViewers(ctx, deps, () =>
|
||||
@@ -40,15 +64,22 @@ export async function activate(ctx: PluginContext) {
|
||||
repository,
|
||||
engine,
|
||||
log: ctx.log,
|
||||
reconcile: [() => viewers.reconcile(), (now) => docker.reconcile(now)],
|
||||
reconcile: [
|
||||
() => maintenance.tick(),
|
||||
() => viewers.reconcile(),
|
||||
(now) => docker.reconcile(now),
|
||||
],
|
||||
isMaintaining: maintenance.isMaintaining,
|
||||
});
|
||||
ctx.schedule.after(STARTUP_DELAY_MS, () => scheduler.tick());
|
||||
ctx.schedule.every(TICK_MS, () => scheduler.tick());
|
||||
|
||||
const router = ctx.http.router<Router>({ public: ["/webhook/:token"] });
|
||||
registerMaintenanceRoutes(router, ctx, maintenance);
|
||||
registerRoutes(
|
||||
// The webhook route authenticates on its own per-automation token rather
|
||||
// than a session, which is the point of an inbound webhook.
|
||||
ctx.http.router<Router>({ public: ["/webhook/:token"] }),
|
||||
router,
|
||||
{ ctx, repository, engine, deps },
|
||||
);
|
||||
|
||||
|
||||
Reference in new issue
Block a user