Skip to main content

max / balanced_breakfast

1.8 KB · 49 lines History Blame Raw
1 -- Feature: Circuit breaker
2 -- Circuit breaker: auto-disable feeds after too many consecutive failures.
3 ALTER TABLE feeds ADD COLUMN circuit_broken INTEGER NOT NULL DEFAULT 0;
4
5 -- Update sync triggers to include the new column.
6 -- Drop and recreate insert/update triggers so they capture circuit_broken.
7
8 DROP TRIGGER IF EXISTS sync_feeds_insert;
9 CREATE TRIGGER sync_feeds_insert AFTER INSERT ON feeds
10 WHEN (SELECT value FROM sync_state WHERE key = 'applying_remote') != '1'
11 BEGIN
12 INSERT INTO sync_changelog (table_name, op, row_id, data)
13 VALUES ('feeds', 'INSERT', NEW.id, json_object(
14 'id', NEW.id,
15 'busser_id', NEW.busser_id,
16 'name', NEW.name,
17 'config', NEW.config,
18 'enabled', NEW.enabled,
19 'last_fetch', NEW.last_fetch,
20 'created_at', NEW.created_at,
21 'updated_at', NEW.updated_at,
22 'consecutive_failures', NEW.consecutive_failures,
23 'last_error', NEW.last_error,
24 'last_success_at', NEW.last_success_at,
25 'circuit_broken', NEW.circuit_broken
26 ));
27 END;
28
29 DROP TRIGGER IF EXISTS sync_feeds_update;
30 CREATE TRIGGER sync_feeds_update AFTER UPDATE ON feeds
31 WHEN (SELECT value FROM sync_state WHERE key = 'applying_remote') != '1'
32 BEGIN
33 INSERT INTO sync_changelog (table_name, op, row_id, data)
34 VALUES ('feeds', 'UPDATE', NEW.id, json_object(
35 'id', NEW.id,
36 'busser_id', NEW.busser_id,
37 'name', NEW.name,
38 'config', NEW.config,
39 'enabled', NEW.enabled,
40 'last_fetch', NEW.last_fetch,
41 'created_at', NEW.created_at,
42 'updated_at', NEW.updated_at,
43 'consecutive_failures', NEW.consecutive_failures,
44 'last_error', NEW.last_error,
45 'last_success_at', NEW.last_success_at,
46 'circuit_broken', NEW.circuit_broken
47 ));
48 END;
49