chore: initial commit — monorepo ReadaBook (API NestJS, web PWA, Docker)
This commit is contained in:
10
apps/api/src/jobs/jobs.module.ts
Normal file
10
apps/api/src/jobs/jobs.module.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { DatabaseModule } from "../database/database.module.js";
|
||||
import { JobsService } from "./jobs.service.js";
|
||||
|
||||
@Module({
|
||||
imports: [DatabaseModule],
|
||||
providers: [JobsService],
|
||||
exports: [JobsService]
|
||||
})
|
||||
export class JobsModule {}
|
||||
46
apps/api/src/jobs/jobs.service.ts
Normal file
46
apps/api/src/jobs/jobs.service.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
import { DatabaseService } from "../database/database.service.js";
|
||||
import { jobs } from "../database/schema.js";
|
||||
|
||||
@Injectable()
|
||||
export class JobsService {
|
||||
constructor(private readonly database: DatabaseService) {}
|
||||
|
||||
create(type: string, detail?: string) {
|
||||
const now = this.database.now();
|
||||
return this.database.db
|
||||
.insert(jobs)
|
||||
.values({ type, status: "queued", detail: detail ?? null, error: null, createdAt: now, updatedAt: now })
|
||||
.returning()
|
||||
.get();
|
||||
}
|
||||
|
||||
markRunning(id: number, detail?: string): void {
|
||||
this.database.db
|
||||
.update(jobs)
|
||||
.set({ status: "running", detail: detail ?? null, updatedAt: this.database.now() })
|
||||
.where(eq(jobs.id, id))
|
||||
.run();
|
||||
}
|
||||
|
||||
markSucceeded(id: number, detail?: string): void {
|
||||
this.database.db
|
||||
.update(jobs)
|
||||
.set({ status: "succeeded", detail: detail ?? null, error: null, updatedAt: this.database.now() })
|
||||
.where(eq(jobs.id, id))
|
||||
.run();
|
||||
}
|
||||
|
||||
markFailed(id: number, error: unknown): void {
|
||||
this.database.db
|
||||
.update(jobs)
|
||||
.set({ status: "failed", error: error instanceof Error ? error.message : String(error), updatedAt: this.database.now() })
|
||||
.where(eq(jobs.id, id))
|
||||
.run();
|
||||
}
|
||||
|
||||
list(limit = 50) {
|
||||
return this.database.db.select().from(jobs).orderBy(desc(jobs.createdAt)).limit(limit).all();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user