54 lines
1.4 KiB
JavaScript
54 lines
1.4 KiB
JavaScript
const Redis = require("ioredis");
|
|
var data; //Die Daten von Grist.
|
|
|
|
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
|
|
const GRIST_URL = process.env.GRIST_BASE_URL;
|
|
const API_KEY = process.env.GRIST_API_KEY;
|
|
|
|
// Verbindung zu Redis herstellen
|
|
const redis = new Redis({
|
|
host: "data_jobs", // Dein Redis-Containername
|
|
port: 6379
|
|
});
|
|
|
|
startWorker();
|
|
|
|
// Endlosschleife: Immer auf neue Jobs warten
|
|
async function startWorker() {
|
|
while (true) {
|
|
// BLPOP = Warte auf Job in der Queue "grist:jobs"
|
|
const reply = await redis.blpop("grist:jobs", 0);
|
|
const payload = JSON.parse(reply[1]);
|
|
const { client, path, row, meta } = payload;
|
|
|
|
const data = await getFromGrist(path, row);
|
|
|
|
// Ergebnis verpacken in ein JSON
|
|
const freight = JSON.stringify({
|
|
job_ok: true,
|
|
client,
|
|
data
|
|
});
|
|
|
|
// Ergebnis in Fertig Queue schreiben. Von da holt es sich der Client.
|
|
await redis.rpush("grist:results", freight);
|
|
console.log (freight);
|
|
}
|
|
}
|
|
|
|
async function getFromGrist(path, row) {
|
|
//
|
|
const [docId, , tableId] = path.split("/");
|
|
const base = `${GRIST_URL}/api/docs/${docId}/tables/${tableId}/records`;
|
|
|
|
const url = row > 0 ? `${base}/${row}` : base;
|
|
|
|
const res = await fetch(url, {
|
|
headers: { "Authorization": `Bearer ${API_KEY}` }
|
|
});
|
|
|
|
return res.json(); // -> JSON mit Records
|
|
}
|
|
|
|
|