source: Klonkt/src/services/Scheduler.js@ a8399a6

main
Last change on this file since a8399a6 was 834bcc3, checked in by Robin Genis <roboburr@…>, 3 months ago

i18n: translate Dutch code comments to English across src/

Comments in routes/services/views/config/middleware/assets translated to
English for the public repo. A few dev-facing throw/console message strings
were Englished too. No user-facing UI strings or i18n dictionary values changed
(src/services/i18n.js untouched). Logic unchanged.

Co-Authored-By: Claude <noreply@…>

  • Property mode set to 100644
File size: 1.5 KB
Line 
1/**
2 * Scheduler — release planning (premium #3).
3 *
4 * Scheduled posts have status 'scheduled' + publish_at (future). A lightweight
5 * timer flips them to 'published' once publish_at is reached. This means public
6 * queries (status='published') need NO changes — a scheduled post simply isn't
7 * 'published' yet and therefore invisible until that moment.
8 */
9
10import db from '../config/database.js';
11import HtmlSanitizerService from './HtmlSanitizerService.js';
12
13export function flipScheduledPosts() {
14 try {
15 const due = db.prepare(`
16 SELECT p.id, p.title, p.content, u.username
17 FROM posts p JOIN users u ON u.id = p.author_id
18 WHERE p.status = 'scheduled' AND p.publish_at IS NOT NULL AND datetime(p.publish_at) <= datetime('now')
19 `).all();
20 if (!due.length) return 0;
21 const upd = db.prepare(
22 "UPDATE posts SET status = 'published', published_at = COALESCE(published_at, publish_at, CURRENT_TIMESTAMP) WHERE id = ?"
23 );
24 const fts = db.prepare('INSERT INTO posts_fts(content, title, author, post_id) VALUES (?, ?, ?, ?)');
25 for (const p of due) {
26 upd.run(p.id);
27 try { fts.run(HtmlSanitizerService.toPlainText(p.content || ''), p.title || '', p.username || '', p.id); } catch { /* FTS failure is non-fatal */ }
28 }
29 return due.length;
30 } catch { return 0; }
31}
32
33let _timer = null;
34export function startScheduler() {
35 flipScheduledPosts(); // run immediately on boot
36 if (_timer) return;
37 _timer = setInterval(flipScheduledPosts, 60 * 1000); // every minute
38 if (_timer.unref) _timer.unref();
39}
Note: See TracBrowser for help on using the repository browser.