Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
94751c12ff | ||
|
|
a6eb41f996 |
@@ -96,8 +96,8 @@ async function buendleZugang(clientId, subs, label, zeitSuffix) {
|
||||
}
|
||||
|
||||
/** Bündelt alle offenen Datensätze (optional nur ein Zugang), gruppiert je Zugang. */
|
||||
async function buendleAlle({ clientId = null, label, zeitSuffix = '' }) {
|
||||
const alle = await fetchBatchPending(clientId);
|
||||
async function buendleAlle({ clientId = null, ids = null, label, zeitSuffix = '' }) {
|
||||
const alle = await fetchBatchPending(clientId, ids);
|
||||
const proZugang = new Map();
|
||||
for (const s of alle) {
|
||||
const k = String(s.api_client);
|
||||
@@ -117,12 +117,13 @@ async function buendleAlle({ clientId = null, label, zeitSuffix = '' }) {
|
||||
async function buildBundles(req, res) {
|
||||
try {
|
||||
const clientId = (req.body && req.body.client_id != null && String(req.body.client_id).trim()) ? String(req.body.client_id).trim() : null;
|
||||
const ids = (req.body && Array.isArray(req.body.ids) && req.body.ids.length) ? req.body.ids : null;
|
||||
const label = (req.body && req.body.label) ? String(req.body.label) : berlinParts().date;
|
||||
// Manuelle Bündelung mit Uhrzeit-Suffix, damit sie den Cron-Tageslauf (nur Datum) nicht überschreibt.
|
||||
const zeitSuffix = (req.body && req.body.time_suffix !== undefined)
|
||||
? String(req.body.time_suffix)
|
||||
: `${String(berlinParts().hour).padStart(2, '0')}${String(berlinParts().minute).padStart(2, '0')}`;
|
||||
const ergebnis = await buendleAlle({ clientId, label, zeitSuffix });
|
||||
const ergebnis = await buendleAlle({ clientId, ids, label, zeitSuffix });
|
||||
return res.json({ ok: true, bundles: ergebnis });
|
||||
} catch (err) {
|
||||
console.error('[batch] build fehlgeschlagen:', err);
|
||||
|
||||
@@ -84,18 +84,21 @@ app.listen(config.port, () => {
|
||||
refreshRenderFormats();
|
||||
setInterval(refreshRenderFormats, 60 * 60 * 1000);
|
||||
|
||||
// Batch-Bündelung: täglich um 03:00 (Europe/Berlin) den Vortag bündeln + FTP.
|
||||
// Sa+So werden gemeinsam am Montag verarbeitet (Sonntag-Lauf wird übersprungen).
|
||||
// Minuten-Tick; einmal pro Tag im 3-Uhr-Fenster (idempotent über letzterBatchTag).
|
||||
let letzterBatchTag = null;
|
||||
setInterval(() => {
|
||||
try {
|
||||
const { date, hour } = berlinParts();
|
||||
if (hour !== 3 || letzterBatchTag === date) return;
|
||||
letzterBatchTag = date; // markiert den Lauf für heute – auch bei Fehler kein Doppel-Lauf.
|
||||
runScheduledBatch().catch((e) => console.error('[batch-cron] Fehler:', e.message));
|
||||
} catch (e) { console.error('[batch-cron] Tick-Fehler:', e.message); }
|
||||
}, 60 * 1000);
|
||||
// Batch-FTP-Bündelung nur, wenn ausdrücklich aktiviert (Standardweg ist die
|
||||
// Übernahme in die Produktion, nicht FTP). SKRIFT_BATCH_FTP_CRON=true schaltet
|
||||
// den täglichen 03:00-Lauf (Europe/Berlin) wieder ein; Sa+So am Montag.
|
||||
if (String(process.env.SKRIFT_BATCH_FTP_CRON || '').toLowerCase() === 'true') {
|
||||
let letzterBatchTag = null;
|
||||
setInterval(() => {
|
||||
try {
|
||||
const { date, hour } = berlinParts();
|
||||
if (hour !== 3 || letzterBatchTag === date) return;
|
||||
letzterBatchTag = date;
|
||||
runScheduledBatch().catch((e) => console.error('[batch-cron] Fehler:', e.message));
|
||||
} catch (e) { console.error('[batch-cron] Tick-Fehler:', e.message); }
|
||||
}, 60 * 1000);
|
||||
console.log('Batch-FTP-Cron: aktiv (03:00 Europe/Berlin)');
|
||||
}
|
||||
});
|
||||
|
||||
// Graceful shutdown
|
||||
|
||||
@@ -112,9 +112,10 @@ async function fetchRenderFormats() {
|
||||
|
||||
/** Offene (pending) Batch-Übermittlungen MIT Druck-PDF, optional je Zugang.
|
||||
* Text-only-Datensätze (ohne PDF) werden bewusst NICHT gebündelt. */
|
||||
async function fetchBatchPending(clientId) {
|
||||
const filter = 'filter[status][_eq]=pending&filter[pdf_file][_nnull]=true'
|
||||
+ (clientId != null ? `&filter[api_client][_eq]=${encodeURIComponent(clientId)}` : '');
|
||||
async function fetchBatchPending(clientId, ids) {
|
||||
let filter = 'filter[status][_eq]=pending&filter[pdf_file][_nnull]=true';
|
||||
if (clientId != null) filter += `&filter[api_client][_eq]=${encodeURIComponent(clientId)}`;
|
||||
if (Array.isArray(ids) && ids.length) filter += `&filter[id][_in]=${ids.map(encodeURIComponent).join(',')}`;
|
||||
const fields = 'id,api_client,firma,vorname,nachname,zeile1,zeile2,zeile3,plz,stadt,land,pdf_file';
|
||||
return (await directusFetch(`/items/batch_submissions?${filter}&sort=date_created&limit=-1&fields=${fields}`)) || [];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user