-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (59 loc) · 1.77 KB
/
server.js
File metadata and controls
71 lines (59 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const redis = require("./redis");
const express = require("express");
const pool = require("./db");
const app = express();
const PORT = 3000;
app.use(express.json());
app.get("/", (req, res) => {
res.send("Distributed Task Queue API is running");
});
app.post("/tasks", async (req, res) => {
try {
const { type, payload, priority } = req.body;
const result = await pool.query(
`INSERT INTO tasks
(type, payload, status, priority, retry_count, max_retries, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, NOW(), NOW())
RETURNING *`,
[type, JSON.stringify(payload), "PENDING", priority, 0, 3]
);
// push task id into Redis queue
const queueName =
priority === "HIGH" ? "queue:high" :
priority === "MEDIUM" ? "queue:medium" :
"queue:low";
await redis.lpush(queueName, result.rows[0].id);
res.status(201).json({
message: "Task saved & queued successfully",
task: result.rows[0]
});
} catch (error) {
console.error("Error creating task:", error);
res.status(500).json({ error: "Failed to create task" });
}
});
app.get("/tasks", async (req, res) => {
try {
const result = await pool.query("SELECT * FROM tasks ORDER BY id ASC");
res.json(result.rows);
} catch (error) {
console.error("Error fetching tasks:", error);
res.status(500).json({ error: "Failed to fetch tasks" });
}
});
app.get("/stats", async (req, res) => {
try {
const result = await pool.query(`
SELECT status, COUNT(*)
FROM tasks
GROUP BY status
`);
res.json(result.rows);
} catch (error) {
console.error("Error fetching stats:", error);
res.status(500).json({ error: "Failed to fetch stats" });
}
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});