tina4ruby 3.0.0 → 3.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +105 -16
- data/lib/tina4/cli.rb +154 -27
- data/lib/tina4/database.rb +101 -0
- data/lib/tina4/dev_mailbox.rb +1 -1
- data/lib/tina4/frond.rb +166 -5
- data/lib/tina4/gallery/queue/meta.json +1 -1
- data/lib/tina4/gallery/queue/src/routes/api/gallery_queue.rb +314 -16
- data/lib/tina4/messenger.rb +111 -33
- data/lib/tina4/orm.rb +134 -12
- data/lib/tina4/queue.rb +125 -5
- data/lib/tina4/rack_app.rb +18 -5
- data/lib/tina4/response_cache.rb +446 -29
- data/lib/tina4/templates/errors/404.twig +2 -2
- data/lib/tina4/templates/errors/500.twig +1 -1
- data/lib/tina4/version.rb +1 -1
- data/lib/tina4.rb +63 -2
- metadata +29 -1
data/lib/tina4/frond.rb
CHANGED
|
@@ -56,21 +56,65 @@ module Tina4
|
|
|
56
56
|
# Fragment cache: key => [html, expires_at]
|
|
57
57
|
@fragment_cache = {}
|
|
58
58
|
|
|
59
|
+
# Token pre-compilation cache
|
|
60
|
+
@compiled = {} # {template_name => [tokens, mtime]}
|
|
61
|
+
@compiled_strings = {} # {md5_hash => tokens}
|
|
62
|
+
|
|
59
63
|
# Built-in global functions
|
|
60
64
|
register_builtin_globals
|
|
61
65
|
end
|
|
62
66
|
|
|
63
|
-
# Render a template file with data.
|
|
67
|
+
# Render a template file with data. Uses token caching for performance.
|
|
64
68
|
def render(template, data = {})
|
|
65
69
|
context = @globals.merge(stringify_keys(data))
|
|
66
|
-
|
|
67
|
-
|
|
70
|
+
|
|
71
|
+
path = File.join(@template_dir, template)
|
|
72
|
+
raise "Template not found: #{path}" unless File.exist?(path)
|
|
73
|
+
|
|
74
|
+
debug_mode = ENV.fetch("TINA4_DEBUG", "").downcase == "true"
|
|
75
|
+
cached = @compiled[template]
|
|
76
|
+
|
|
77
|
+
if cached
|
|
78
|
+
if debug_mode
|
|
79
|
+
# Dev mode: check if file changed
|
|
80
|
+
mtime = File.mtime(path)
|
|
81
|
+
if cached[1] == mtime
|
|
82
|
+
return execute_cached(cached[0], context)
|
|
83
|
+
end
|
|
84
|
+
else
|
|
85
|
+
# Production: skip mtime check, cache is permanent
|
|
86
|
+
return execute_cached(cached[0], context)
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Cache miss — load, tokenize, cache
|
|
91
|
+
source = File.read(path, encoding: "utf-8")
|
|
92
|
+
mtime = File.mtime(path)
|
|
93
|
+
tokens = tokenize(source)
|
|
94
|
+
@compiled[template] = [tokens, mtime]
|
|
95
|
+
execute_with_tokens(source, tokens, context)
|
|
68
96
|
end
|
|
69
97
|
|
|
70
|
-
# Render a template string directly.
|
|
98
|
+
# Render a template string directly. Uses token caching for performance.
|
|
71
99
|
def render_string(source, data = {})
|
|
72
100
|
context = @globals.merge(stringify_keys(data))
|
|
73
|
-
|
|
101
|
+
|
|
102
|
+
key = Digest::MD5.hexdigest(source)
|
|
103
|
+
cached_tokens = @compiled_strings[key]
|
|
104
|
+
|
|
105
|
+
if cached_tokens
|
|
106
|
+
return execute_cached(cached_tokens, context)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
tokens = tokenize(source)
|
|
110
|
+
@compiled_strings[key] = tokens
|
|
111
|
+
execute_cached(tokens, context)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Clear all compiled template caches.
|
|
115
|
+
def clear_cache
|
|
116
|
+
@compiled.clear
|
|
117
|
+
@compiled_strings.clear
|
|
74
118
|
end
|
|
75
119
|
|
|
76
120
|
# Register a custom filter.
|
|
@@ -198,6 +242,35 @@ module Tina4
|
|
|
198
242
|
# Execution
|
|
199
243
|
# -----------------------------------------------------------------------
|
|
200
244
|
|
|
245
|
+
def execute_cached(tokens, context)
|
|
246
|
+
# Check if first non-text token is an extends block
|
|
247
|
+
tokens.each do |ttype, raw|
|
|
248
|
+
next if ttype == TEXT && raw.strip.empty?
|
|
249
|
+
if ttype == BLOCK
|
|
250
|
+
content, _, _ = strip_tag(raw)
|
|
251
|
+
if content.start_with?("extends ")
|
|
252
|
+
# Extends requires source-based execution for block extraction
|
|
253
|
+
source = tokens.map { |_, v| v }.join
|
|
254
|
+
return execute(source, context)
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
break
|
|
258
|
+
end
|
|
259
|
+
render_tokens(tokens, context)
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def execute_with_tokens(source, tokens, context)
|
|
263
|
+
# Handle extends first
|
|
264
|
+
if source =~ /\{%-?\s*extends\s+["'](.+?)["']\s*-?%\}/
|
|
265
|
+
parent_name = Regexp.last_match(1)
|
|
266
|
+
parent_source = load_template(parent_name)
|
|
267
|
+
child_blocks = extract_blocks(source)
|
|
268
|
+
return render_with_blocks(parent_source, context, child_blocks)
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
render_tokens(tokens, context)
|
|
272
|
+
end
|
|
273
|
+
|
|
201
274
|
def execute(source, context)
|
|
202
275
|
# Handle extends first
|
|
203
276
|
if source =~ /\{%-?\s*extends\s+["'](.+?)["']\s*-?%\}/
|
|
@@ -290,6 +363,12 @@ module Tina4
|
|
|
290
363
|
when "cache"
|
|
291
364
|
result, i = handle_cache(tokens, i, context)
|
|
292
365
|
output << result
|
|
366
|
+
when "spaceless"
|
|
367
|
+
result, i = handle_spaceless(tokens, i, context)
|
|
368
|
+
output << result
|
|
369
|
+
when "autoescape"
|
|
370
|
+
result, i = handle_autoescape(tokens, i, context)
|
|
371
|
+
output << result
|
|
293
372
|
when "block", "endblock", "extends"
|
|
294
373
|
i += 1
|
|
295
374
|
else
|
|
@@ -496,6 +575,13 @@ module Tina4
|
|
|
496
575
|
return truthy?(cond) ? eval_expr(ternary[2], context) : eval_expr(ternary[3], context)
|
|
497
576
|
end
|
|
498
577
|
|
|
578
|
+
# Jinja2-style inline if: value if condition else other_value
|
|
579
|
+
inline_if = expr.match(/\A(.+?)\s+if\s+(.+?)\s+else\s+(.+)\z/)
|
|
580
|
+
if inline_if
|
|
581
|
+
cond = eval_expr(inline_if[2], context)
|
|
582
|
+
return truthy?(cond) ? eval_expr(inline_if[1], context) : eval_expr(inline_if[3], context)
|
|
583
|
+
end
|
|
584
|
+
|
|
499
585
|
# Null coalescing: value ?? "default"
|
|
500
586
|
if expr.include?("??")
|
|
501
587
|
left, _, right = expr.partition("??")
|
|
@@ -1121,6 +1207,81 @@ module Tina4
|
|
|
1121
1207
|
[rendered, i]
|
|
1122
1208
|
end
|
|
1123
1209
|
|
|
1210
|
+
def handle_spaceless(tokens, start, context)
|
|
1211
|
+
body_tokens = []
|
|
1212
|
+
i = start + 1
|
|
1213
|
+
depth = 0
|
|
1214
|
+
while i < tokens.length
|
|
1215
|
+
if tokens[i][0] == BLOCK
|
|
1216
|
+
tc, _, _ = strip_tag(tokens[i][1])
|
|
1217
|
+
tag = tc.split[0] || ""
|
|
1218
|
+
if tag == "spaceless"
|
|
1219
|
+
depth += 1
|
|
1220
|
+
body_tokens << tokens[i]
|
|
1221
|
+
elsif tag == "endspaceless"
|
|
1222
|
+
if depth == 0
|
|
1223
|
+
i += 1
|
|
1224
|
+
break
|
|
1225
|
+
end
|
|
1226
|
+
depth -= 1
|
|
1227
|
+
body_tokens << tokens[i]
|
|
1228
|
+
else
|
|
1229
|
+
body_tokens << tokens[i]
|
|
1230
|
+
end
|
|
1231
|
+
else
|
|
1232
|
+
body_tokens << tokens[i]
|
|
1233
|
+
end
|
|
1234
|
+
i += 1
|
|
1235
|
+
end
|
|
1236
|
+
|
|
1237
|
+
rendered = render_tokens(body_tokens.dup, context)
|
|
1238
|
+
rendered = rendered.gsub(/>\s+</, "><")
|
|
1239
|
+
[rendered, i]
|
|
1240
|
+
end
|
|
1241
|
+
|
|
1242
|
+
def handle_autoescape(tokens, start, context)
|
|
1243
|
+
content, _, _ = strip_tag(tokens[start][1])
|
|
1244
|
+
mode_match = content.match(/\Aautoescape\s+(false|true)/)
|
|
1245
|
+
auto_escape_on = !(mode_match && mode_match[1] == "false")
|
|
1246
|
+
|
|
1247
|
+
body_tokens = []
|
|
1248
|
+
i = start + 1
|
|
1249
|
+
depth = 0
|
|
1250
|
+
while i < tokens.length
|
|
1251
|
+
if tokens[i][0] == BLOCK
|
|
1252
|
+
tc, _, _ = strip_tag(tokens[i][1])
|
|
1253
|
+
tag = tc.split[0] || ""
|
|
1254
|
+
if tag == "autoescape"
|
|
1255
|
+
depth += 1
|
|
1256
|
+
body_tokens << tokens[i]
|
|
1257
|
+
elsif tag == "endautoescape"
|
|
1258
|
+
if depth == 0
|
|
1259
|
+
i += 1
|
|
1260
|
+
break
|
|
1261
|
+
end
|
|
1262
|
+
depth -= 1
|
|
1263
|
+
body_tokens << tokens[i]
|
|
1264
|
+
else
|
|
1265
|
+
body_tokens << tokens[i]
|
|
1266
|
+
end
|
|
1267
|
+
else
|
|
1268
|
+
body_tokens << tokens[i]
|
|
1269
|
+
end
|
|
1270
|
+
i += 1
|
|
1271
|
+
end
|
|
1272
|
+
|
|
1273
|
+
if !auto_escape_on
|
|
1274
|
+
old_auto_escape = @auto_escape
|
|
1275
|
+
@auto_escape = false
|
|
1276
|
+
rendered = render_tokens(body_tokens.dup, context)
|
|
1277
|
+
@auto_escape = old_auto_escape
|
|
1278
|
+
else
|
|
1279
|
+
rendered = render_tokens(body_tokens.dup, context)
|
|
1280
|
+
end
|
|
1281
|
+
|
|
1282
|
+
[rendered, i]
|
|
1283
|
+
end
|
|
1284
|
+
|
|
1124
1285
|
# -----------------------------------------------------------------------
|
|
1125
1286
|
# Helpers
|
|
1126
1287
|
# -----------------------------------------------------------------------
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name": "Queue", "description": "Background job producer and consumer", "try_url": "/
|
|
1
|
+
{"name": "Queue", "description": "Background job producer and consumer", "try_url": "/gallery/queue"}
|
|
@@ -1,27 +1,325 @@
|
|
|
1
|
-
# Gallery: Queue —
|
|
1
|
+
# Gallery: Queue — interactive queue demo with visual web UI.
|
|
2
|
+
#
|
|
3
|
+
# Uses a SQLite database directly for the demo queue table,
|
|
4
|
+
# matching the Python gallery demo's database-backed approach.
|
|
5
|
+
|
|
6
|
+
require "json"
|
|
7
|
+
|
|
8
|
+
def _gallery_queue_db
|
|
9
|
+
@_gallery_queue_db ||= begin
|
|
10
|
+
db = Tina4::Database.new("sqlite://data/gallery_queue.db")
|
|
11
|
+
unless db.table_exists?("tina4_queue")
|
|
12
|
+
db.execute("CREATE TABLE tina4_queue (
|
|
13
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
14
|
+
topic TEXT NOT NULL,
|
|
15
|
+
data TEXT NOT NULL,
|
|
16
|
+
status TEXT NOT NULL DEFAULT 'pending',
|
|
17
|
+
priority INTEGER NOT NULL DEFAULT 0,
|
|
18
|
+
attempts INTEGER NOT NULL DEFAULT 0,
|
|
19
|
+
error TEXT,
|
|
20
|
+
available_at TEXT NOT NULL,
|
|
21
|
+
created_at TEXT NOT NULL,
|
|
22
|
+
completed_at TEXT,
|
|
23
|
+
reserved_at TEXT
|
|
24
|
+
)")
|
|
25
|
+
end
|
|
26
|
+
db
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def _gallery_queue_now
|
|
31
|
+
Time.now.utc.iso8601
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
GALLERY_QUEUE_MAX_RETRIES = 3
|
|
35
|
+
|
|
36
|
+
GALLERY_QUEUE_HTML = <<~'HTML'
|
|
37
|
+
<!DOCTYPE html>
|
|
38
|
+
<html lang="en">
|
|
39
|
+
<head>
|
|
40
|
+
<meta charset="UTF-8">
|
|
41
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
42
|
+
<title>Queue Gallery — Tina4 Ruby</title>
|
|
43
|
+
<link rel="stylesheet" href="/css/tina4.min.css">
|
|
44
|
+
</head>
|
|
45
|
+
<body>
|
|
46
|
+
<div class="container mt-4 mb-4">
|
|
47
|
+
<h1>Queue Gallery</h1>
|
|
48
|
+
<p class="text-muted">Interactive demo of Tina4's database-backed job queue. Produce messages, consume them, simulate failures, and inspect dead letters.</p>
|
|
49
|
+
|
|
50
|
+
<div class="row mt-3">
|
|
51
|
+
<div class="col-md-6">
|
|
52
|
+
<div class="card">
|
|
53
|
+
<div class="card-header">Produce a Message</div>
|
|
54
|
+
<div class="card-body">
|
|
55
|
+
<div class="d-flex gap-2">
|
|
56
|
+
<input type="text" id="msgInput" class="form-control" placeholder="Enter a task message, e.g. send-email">
|
|
57
|
+
<button class="btn btn-primary" onclick="produce()">Produce</button>
|
|
58
|
+
</div>
|
|
59
|
+
</div>
|
|
60
|
+
</div>
|
|
61
|
+
</div>
|
|
62
|
+
<div class="col-md-6">
|
|
63
|
+
<div class="card">
|
|
64
|
+
<div class="card-header">Actions</div>
|
|
65
|
+
<div class="card-body d-flex gap-2 flex-wrap">
|
|
66
|
+
<button class="btn btn-success" onclick="consume()">Consume Next</button>
|
|
67
|
+
<button class="btn btn-danger" onclick="failNext()">Fail Next</button>
|
|
68
|
+
<button class="btn btn-warning" onclick="retryFailed()">Retry Failed</button>
|
|
69
|
+
<button class="btn btn-secondary" onclick="refresh()">Refresh</button>
|
|
70
|
+
</div>
|
|
71
|
+
</div>
|
|
72
|
+
</div>
|
|
73
|
+
</div>
|
|
74
|
+
|
|
75
|
+
<div id="alertArea" class="mt-3"></div>
|
|
76
|
+
|
|
77
|
+
<div class="card mt-3">
|
|
78
|
+
<div class="card-header d-flex justify-content-between align-items-center">
|
|
79
|
+
<span>Queue Messages</span>
|
|
80
|
+
<small class="text-muted" id="lastRefresh"></small>
|
|
81
|
+
</div>
|
|
82
|
+
<div class="card-body p-0">
|
|
83
|
+
<table class="table table-striped mb-0">
|
|
84
|
+
<thead>
|
|
85
|
+
<tr>
|
|
86
|
+
<th>ID</th>
|
|
87
|
+
<th>Data</th>
|
|
88
|
+
<th>Status</th>
|
|
89
|
+
<th>Attempts</th>
|
|
90
|
+
<th>Error</th>
|
|
91
|
+
<th>Created</th>
|
|
92
|
+
</tr>
|
|
93
|
+
</thead>
|
|
94
|
+
<tbody id="queueBody">
|
|
95
|
+
<tr><td colspan="6" class="text-center text-muted">Loading...</td></tr>
|
|
96
|
+
</tbody>
|
|
97
|
+
</table>
|
|
98
|
+
</div>
|
|
99
|
+
</div>
|
|
100
|
+
</div>
|
|
101
|
+
|
|
102
|
+
<script>
|
|
103
|
+
function statusBadge(status) {
|
|
104
|
+
var colors = {pending:"primary", reserved:"warning", completed:"success", failed:"danger", dead:"secondary"};
|
|
105
|
+
var color = colors[status] || "secondary";
|
|
106
|
+
return '<span class="badge bg-' + color + '">' + status + '</span>';
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function showAlert(msg, type) {
|
|
110
|
+
var area = document.getElementById("alertArea");
|
|
111
|
+
area.innerHTML = '<div class="alert alert-' + type + ' alert-dismissible">' + msg +
|
|
112
|
+
'<button type="button" class="btn-close" onclick="this.parentElement.remove()"></button></div>';
|
|
113
|
+
setTimeout(function(){ area.innerHTML = ""; }, 3000);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function truncate(s, n) {
|
|
117
|
+
if (!s) return "";
|
|
118
|
+
return s.length > n ? s.substring(0, n) + "..." : s;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function refresh() {
|
|
122
|
+
try {
|
|
123
|
+
var r = await fetch("/api/gallery/queue/status");
|
|
124
|
+
var data = await r.json();
|
|
125
|
+
var tbody = document.getElementById("queueBody");
|
|
126
|
+
if (!data.messages || data.messages.length === 0) {
|
|
127
|
+
tbody.innerHTML = '<tr><td colspan="6" class="text-center text-muted">No messages in queue. Produce one above.</td></tr>';
|
|
128
|
+
} else {
|
|
129
|
+
var html = "";
|
|
130
|
+
for (var i = 0; i < data.messages.length; i++) {
|
|
131
|
+
var m = data.messages[i];
|
|
132
|
+
html += "<tr><td>" + m.id + "</td><td><code>" + truncate(m.data, 60) + "</code></td><td>" +
|
|
133
|
+
statusBadge(m.status) + "</td><td>" + m.attempts + "</td><td>" +
|
|
134
|
+
truncate(m.error || "", 40) + "</td><td><small>" + (m.created_at || "") + "</small></td></tr>";
|
|
135
|
+
}
|
|
136
|
+
tbody.innerHTML = html;
|
|
137
|
+
}
|
|
138
|
+
document.getElementById("lastRefresh").textContent = "Updated " + new Date().toLocaleTimeString();
|
|
139
|
+
} catch (e) {
|
|
140
|
+
console.error(e);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function produce() {
|
|
145
|
+
var input = document.getElementById("msgInput");
|
|
146
|
+
var task = input.value.trim() || "demo-task";
|
|
147
|
+
var r = await fetch("/api/gallery/queue/produce", {
|
|
148
|
+
method: "POST", headers: {"Content-Type":"application/json"},
|
|
149
|
+
body: JSON.stringify({task: task, data: {message: task}})
|
|
150
|
+
});
|
|
151
|
+
var d = await r.json();
|
|
152
|
+
showAlert("Produced message: " + task, "success");
|
|
153
|
+
input.value = "";
|
|
154
|
+
refresh();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async function consume() {
|
|
158
|
+
var r = await fetch("/api/gallery/queue/consume", {method:"POST"});
|
|
159
|
+
var d = await r.json();
|
|
160
|
+
if (d.consumed) {
|
|
161
|
+
showAlert("Consumed job #" + d.job_id + " successfully", "success");
|
|
162
|
+
} else {
|
|
163
|
+
showAlert(d.message || "Nothing to consume", "info");
|
|
164
|
+
}
|
|
165
|
+
refresh();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async function failNext() {
|
|
169
|
+
var r = await fetch("/api/gallery/queue/fail", {method:"POST"});
|
|
170
|
+
var d = await r.json();
|
|
171
|
+
if (d.failed) {
|
|
172
|
+
showAlert("Deliberately failed job #" + d.job_id, "danger");
|
|
173
|
+
} else {
|
|
174
|
+
showAlert(d.message || "Nothing to fail", "info");
|
|
175
|
+
}
|
|
176
|
+
refresh();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function retryFailed() {
|
|
180
|
+
var r = await fetch("/api/gallery/queue/retry", {method:"POST"});
|
|
181
|
+
var d = await r.json();
|
|
182
|
+
showAlert("Retried " + (d.retried || 0) + " failed message(s)", "warning");
|
|
183
|
+
refresh();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
refresh();
|
|
187
|
+
setInterval(refresh, 2000);
|
|
188
|
+
</script>
|
|
189
|
+
</body>
|
|
190
|
+
</html>
|
|
191
|
+
HTML
|
|
192
|
+
|
|
193
|
+
# ── Render the interactive HTML page ──────────────────────────
|
|
194
|
+
|
|
195
|
+
Tina4::Router.get("/gallery/queue") do |request, response|
|
|
196
|
+
response.html(GALLERY_QUEUE_HTML)
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# ── Produce — add a message to the queue ──────────────────────
|
|
2
200
|
|
|
3
201
|
Tina4::Router.post("/api/gallery/queue/produce") do |request, response|
|
|
4
202
|
body = request.body || {}
|
|
5
203
|
task = body["task"] || "default-task"
|
|
6
204
|
data = body["data"] || {}
|
|
7
205
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
206
|
+
db = _gallery_queue_db
|
|
207
|
+
now = _gallery_queue_now
|
|
208
|
+
payload = JSON.generate({ task: task, data: data })
|
|
209
|
+
|
|
210
|
+
db.execute(
|
|
211
|
+
"INSERT INTO tina4_queue (topic, data, status, priority, attempts, available_at, created_at) VALUES (?, ?, 'pending', 0, 0, ?, ?)",
|
|
212
|
+
["gallery-tasks", payload, now, now]
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
row = db.fetch_one("SELECT last_insert_rowid() as last_id")
|
|
216
|
+
job_id = row ? row["last_id"] : 0
|
|
217
|
+
|
|
218
|
+
response.json({ queued: true, task: task, job_id: job_id }, 201)
|
|
17
219
|
end
|
|
18
220
|
|
|
221
|
+
# ── Status — list all messages with statuses ──────────────────
|
|
222
|
+
|
|
19
223
|
Tina4::Router.get("/api/gallery/queue/status") do |request, response|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
224
|
+
db = _gallery_queue_db
|
|
225
|
+
|
|
226
|
+
result = db.fetch(
|
|
227
|
+
"SELECT * FROM tina4_queue WHERE topic = ? ORDER BY id DESC",
|
|
228
|
+
["gallery-tasks"],
|
|
229
|
+
limit: 100
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
messages = []
|
|
233
|
+
counts = { pending: 0, reserved: 0, completed: 0, failed: 0 }
|
|
234
|
+
|
|
235
|
+
(result.respond_to?(:records) ? result.records : (result || [])).each do |row|
|
|
236
|
+
status = row["status"] || "pending"
|
|
237
|
+
attempts = (row["attempts"] || 0).to_i
|
|
238
|
+
|
|
239
|
+
display_status = status
|
|
240
|
+
if status == "failed" && attempts >= GALLERY_QUEUE_MAX_RETRIES
|
|
241
|
+
display_status = "dead"
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
counts[status.to_sym] = (counts[status.to_sym] || 0) + 1 if counts.key?(status.to_sym)
|
|
245
|
+
|
|
246
|
+
messages << {
|
|
247
|
+
id: row["id"],
|
|
248
|
+
data: row["data"] || "",
|
|
249
|
+
status: display_status,
|
|
250
|
+
attempts: attempts,
|
|
251
|
+
error: row["error"] || "",
|
|
252
|
+
created_at: row["created_at"] || ""
|
|
253
|
+
}
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
response.json({
|
|
257
|
+
topic: "gallery-tasks",
|
|
258
|
+
messages: messages,
|
|
259
|
+
counts: counts
|
|
260
|
+
})
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
# ── Consume — process the next pending message ────────────────
|
|
264
|
+
|
|
265
|
+
Tina4::Router.post("/api/gallery/queue/consume") do |request, response|
|
|
266
|
+
db = _gallery_queue_db
|
|
267
|
+
now = _gallery_queue_now
|
|
268
|
+
|
|
269
|
+
row = db.fetch_one(
|
|
270
|
+
"SELECT * FROM tina4_queue WHERE topic = ? AND status = 'pending' AND available_at <= ? ORDER BY priority DESC, id ASC",
|
|
271
|
+
["gallery-tasks", now]
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
if row.nil?
|
|
275
|
+
response.json({ consumed: false, message: "No pending messages to consume" })
|
|
276
|
+
else
|
|
277
|
+
db.execute(
|
|
278
|
+
"UPDATE tina4_queue SET status = 'completed', completed_at = ? WHERE id = ? AND status = 'pending'",
|
|
279
|
+
[now, row["id"]]
|
|
280
|
+
)
|
|
281
|
+
response.json({ consumed: true, job_id: row["id"], data: row["data"] })
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
# ── Fail — deliberately fail the next pending message ─────────
|
|
286
|
+
|
|
287
|
+
Tina4::Router.post("/api/gallery/queue/fail") do |request, response|
|
|
288
|
+
db = _gallery_queue_db
|
|
289
|
+
now = _gallery_queue_now
|
|
290
|
+
|
|
291
|
+
row = db.fetch_one(
|
|
292
|
+
"SELECT * FROM tina4_queue WHERE topic = ? AND status = 'pending' AND available_at <= ? ORDER BY priority DESC, id ASC",
|
|
293
|
+
["gallery-tasks", now]
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
if row.nil?
|
|
297
|
+
response.json({ failed: false, message: "No pending messages to fail" })
|
|
298
|
+
else
|
|
299
|
+
db.execute(
|
|
300
|
+
"UPDATE tina4_queue SET status = 'failed', error = ?, attempts = attempts + 1 WHERE id = ?",
|
|
301
|
+
["Deliberately failed via gallery demo", row["id"]]
|
|
302
|
+
)
|
|
303
|
+
response.json({ failed: true, job_id: row["id"], data: row["data"] })
|
|
26
304
|
end
|
|
27
305
|
end
|
|
306
|
+
|
|
307
|
+
# ── Retry — re-queue failed messages ──────────────────────────
|
|
308
|
+
|
|
309
|
+
Tina4::Router.post("/api/gallery/queue/retry") do |request, response|
|
|
310
|
+
db = _gallery_queue_db
|
|
311
|
+
now = _gallery_queue_now
|
|
312
|
+
|
|
313
|
+
db.execute(
|
|
314
|
+
"UPDATE tina4_queue SET status = 'pending', available_at = ? WHERE topic = ? AND status = 'failed' AND attempts < ?",
|
|
315
|
+
[now, "gallery-tasks", GALLERY_QUEUE_MAX_RETRIES]
|
|
316
|
+
)
|
|
317
|
+
|
|
318
|
+
row = db.fetch_one(
|
|
319
|
+
"SELECT COUNT(*) as cnt FROM tina4_queue WHERE topic = ? AND status = 'pending'",
|
|
320
|
+
["gallery-tasks"]
|
|
321
|
+
)
|
|
322
|
+
retried = row ? row["cnt"].to_i : 0
|
|
323
|
+
|
|
324
|
+
response.json({ retried: retried })
|
|
325
|
+
end
|