| 
									
										
										
										
											2025-09-07 17:03:07 +02:00
										 |  |  | const Redis = require("ioredis"); | 
					
						
							| 
									
										
										
										
											2025-09-13 15:52:14 +02:00
										 |  |  | 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; | 
					
						
							| 
									
										
										
										
											2025-09-06 11:45:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Verbindung zu Redis herstellen
 | 
					
						
							|  |  |  | const redis = new Redis({ | 
					
						
							|  |  |  |   host: "data_jobs",   // Dein Redis-Containername
 | 
					
						
							|  |  |  |   port: 6379 | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-13 15:52:14 +02:00
										 |  |  | startWorker(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-06 11:45:17 +02:00
										 |  |  | // Endlosschleife: Immer auf neue Jobs warten
 | 
					
						
							|  |  |  | async function startWorker() { | 
					
						
							|  |  |  |   while (true) { | 
					
						
							|  |  |  |     // BLPOP = Warte auf Job in der Queue "grist:jobs"
 | 
					
						
							| 
									
										
										
										
											2025-09-13 15:52:14 +02:00
										 |  |  |     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); | 
					
						
							| 
									
										
										
										
											2025-09-06 11:45:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-13 15:52:14 +02:00
										 |  |  |     // Ergebnis verpacken in ein JSON
 | 
					
						
							|  |  |  |     const freight = JSON.stringify({ | 
					
						
							|  |  |  |       job_ok: true, | 
					
						
							|  |  |  |       client, | 
					
						
							|  |  |  |       data | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2025-09-06 11:45:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-13 15:52:14 +02:00
										 |  |  |     // Ergebnis in Fertig Queue schreiben. Von da holt es sich der Client.
 | 
					
						
							|  |  |  |     await redis.rpush("grist:results", freight); | 
					
						
							|  |  |  |     console.log (freight); | 
					
						
							| 
									
										
										
										
											2025-09-06 11:45:17 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-13 15:52:14 +02:00
										 |  |  | 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
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 |