Modulo makers3d desarrollado con codex V 0.0.1
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
FROM node:22-bookworm-slim AS build
|
||||
WORKDIR /app
|
||||
COPY package.json tsconfig.base.json ./
|
||||
COPY apps/worker/package.json apps/worker/package.json
|
||||
RUN npm install
|
||||
COPY . .
|
||||
RUN npm run build -w @makers3d/worker
|
||||
|
||||
FROM node:22-bookworm-slim
|
||||
WORKDIR /app
|
||||
COPY --from=build /app/package.json ./
|
||||
COPY --from=build /app/node_modules ./node_modules
|
||||
COPY --from=build /app/apps/worker/dist ./apps/worker/dist
|
||||
ENV NODE_ENV=production
|
||||
CMD ["node", "apps/worker/dist/index.js"]
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "@makers3d/worker",
|
||||
"version": "0.1.0",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"build": "tsc -p tsconfig.json",
|
||||
"start": "node dist/index.js",
|
||||
"dev": "tsx watch src/index.ts",
|
||||
"typecheck": "tsc -p tsconfig.json --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"pg": "^8.16.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/pg": "^8.15.6"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import { Pool } from "pg";
|
||||
|
||||
const databaseUrl = process.env.DATABASE_URL;
|
||||
if (!databaseUrl) {
|
||||
throw new Error("DATABASE_URL is required");
|
||||
}
|
||||
|
||||
const pool = new Pool({ connectionString: databaseUrl });
|
||||
|
||||
async function processJobs(): Promise<void> {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
await client.query("begin");
|
||||
const result = await client.query<{
|
||||
id: string;
|
||||
type: string;
|
||||
}>(
|
||||
`
|
||||
select id, type
|
||||
from jobs
|
||||
where status = 'pending'
|
||||
order by created_at asc
|
||||
limit 5
|
||||
for update skip locked
|
||||
`
|
||||
);
|
||||
|
||||
for (const job of result.rows) {
|
||||
await client.query(`update jobs set status = 'processing', attempts = attempts + 1, updated_at = now() where id = $1`, [job.id]);
|
||||
await client.query(`update jobs set status = 'done', updated_at = now() where id = $1`, [job.id]);
|
||||
console.log(`Processed job ${job.id} (${job.type})`);
|
||||
}
|
||||
|
||||
await client.query("commit");
|
||||
} catch (error) {
|
||||
await client.query("rollback");
|
||||
console.error("Worker cycle failed", error);
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
async function loop() {
|
||||
while (true) {
|
||||
await processJobs();
|
||||
await new Promise((resolve) => setTimeout(resolve, 5000));
|
||||
}
|
||||
}
|
||||
|
||||
loop().catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist",
|
||||
"rootDir": "src",
|
||||
"declaration": true
|
||||
},
|
||||
"include": ["src/**/*.ts"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user