tina4ruby 3.11.13 → 3.11.15
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/CHANGELOG.md +80 -80
- data/LICENSE.txt +21 -21
- data/README.md +137 -137
- data/exe/tina4ruby +5 -5
- data/lib/tina4/ai.rb +696 -696
- data/lib/tina4/api.rb +189 -189
- data/lib/tina4/auth.rb +305 -305
- data/lib/tina4/auto_crud.rb +244 -244
- data/lib/tina4/cache.rb +154 -154
- data/lib/tina4/cli.rb +1449 -1449
- data/lib/tina4/constants.rb +46 -46
- data/lib/tina4/container.rb +74 -74
- data/lib/tina4/cors.rb +74 -74
- data/lib/tina4/crud.rb +692 -692
- data/lib/tina4/database/sqlite3_adapter.rb +165 -165
- data/lib/tina4/database.rb +625 -625
- data/lib/tina4/database_result.rb +208 -208
- data/lib/tina4/debug.rb +8 -8
- data/lib/tina4/dev.rb +14 -14
- data/lib/tina4/dev_admin.rb +935 -935
- data/lib/tina4/dev_mailbox.rb +191 -191
- data/lib/tina4/drivers/firebird_driver.rb +124 -110
- data/lib/tina4/drivers/mongodb_driver.rb +561 -561
- data/lib/tina4/drivers/mssql_driver.rb +112 -112
- data/lib/tina4/drivers/mysql_driver.rb +90 -90
- data/lib/tina4/drivers/odbc_driver.rb +191 -191
- data/lib/tina4/drivers/postgres_driver.rb +116 -106
- data/lib/tina4/drivers/sqlite_driver.rb +122 -122
- data/lib/tina4/env.rb +95 -95
- data/lib/tina4/error_overlay.rb +252 -252
- data/lib/tina4/events.rb +109 -109
- data/lib/tina4/field_types.rb +154 -154
- data/lib/tina4/frond.rb +2025 -2025
- data/lib/tina4/gallery/auth/meta.json +1 -1
- data/lib/tina4/gallery/auth/src/routes/api/gallery_auth.rb +114 -114
- data/lib/tina4/gallery/database/meta.json +1 -1
- data/lib/tina4/gallery/database/src/routes/api/gallery_db.rb +43 -43
- data/lib/tina4/gallery/error-overlay/meta.json +1 -1
- data/lib/tina4/gallery/error-overlay/src/routes/api/gallery_crash.rb +17 -17
- data/lib/tina4/gallery/orm/meta.json +1 -1
- data/lib/tina4/gallery/orm/src/routes/api/gallery_products.rb +16 -16
- data/lib/tina4/gallery/queue/meta.json +1 -1
- data/lib/tina4/gallery/queue/src/routes/api/gallery_queue.rb +325 -325
- data/lib/tina4/gallery/rest-api/meta.json +1 -1
- data/lib/tina4/gallery/rest-api/src/routes/api/gallery_hello.rb +14 -14
- data/lib/tina4/gallery/templates/meta.json +1 -1
- data/lib/tina4/gallery/templates/src/routes/gallery_page.rb +12 -12
- data/lib/tina4/gallery/templates/src/templates/gallery_page.twig +257 -257
- data/lib/tina4/graphql.rb +966 -966
- data/lib/tina4/health.rb +39 -39
- data/lib/tina4/html_element.rb +170 -170
- data/lib/tina4/job.rb +80 -80
- data/lib/tina4/localization.rb +168 -168
- data/lib/tina4/log.rb +203 -203
- data/lib/tina4/mcp.rb +696 -696
- data/lib/tina4/messenger.rb +587 -587
- data/lib/tina4/metrics.rb +793 -793
- data/lib/tina4/middleware.rb +445 -445
- data/lib/tina4/migration.rb +451 -451
- data/lib/tina4/orm.rb +790 -790
- data/lib/tina4/public/css/tina4.css +2463 -2463
- data/lib/tina4/public/css/tina4.min.css +1 -1
- data/lib/tina4/public/images/logo.svg +5 -5
- data/lib/tina4/public/js/frond.min.js +2 -2
- data/lib/tina4/public/js/tina4-dev-admin.js +565 -565
- data/lib/tina4/public/js/tina4-dev-admin.min.js +480 -480
- data/lib/tina4/public/js/tina4.min.js +92 -92
- data/lib/tina4/public/js/tina4js.min.js +48 -48
- data/lib/tina4/public/swagger/index.html +90 -90
- data/lib/tina4/public/swagger/oauth2-redirect.html +63 -63
- data/lib/tina4/query_builder.rb +380 -380
- data/lib/tina4/queue.rb +366 -366
- data/lib/tina4/queue_backends/kafka_backend.rb +80 -80
- data/lib/tina4/queue_backends/lite_backend.rb +298 -298
- data/lib/tina4/queue_backends/mongo_backend.rb +126 -126
- data/lib/tina4/queue_backends/rabbitmq_backend.rb +73 -73
- data/lib/tina4/rack_app.rb +817 -817
- data/lib/tina4/rate_limiter.rb +130 -130
- data/lib/tina4/request.rb +268 -255
- data/lib/tina4/response.rb +346 -346
- data/lib/tina4/response_cache.rb +551 -551
- data/lib/tina4/router.rb +406 -406
- data/lib/tina4/scss/tina4css/_alerts.scss +34 -34
- data/lib/tina4/scss/tina4css/_badges.scss +22 -22
- data/lib/tina4/scss/tina4css/_buttons.scss +69 -69
- data/lib/tina4/scss/tina4css/_cards.scss +49 -49
- data/lib/tina4/scss/tina4css/_forms.scss +156 -156
- data/lib/tina4/scss/tina4css/_grid.scss +81 -81
- data/lib/tina4/scss/tina4css/_modals.scss +84 -84
- data/lib/tina4/scss/tina4css/_nav.scss +149 -149
- data/lib/tina4/scss/tina4css/_reset.scss +94 -94
- data/lib/tina4/scss/tina4css/_tables.scss +54 -54
- data/lib/tina4/scss/tina4css/_typography.scss +55 -55
- data/lib/tina4/scss/tina4css/_utilities.scss +197 -197
- data/lib/tina4/scss/tina4css/_variables.scss +117 -117
- data/lib/tina4/scss/tina4css/base.scss +1 -1
- data/lib/tina4/scss/tina4css/colors.scss +48 -48
- data/lib/tina4/scss/tina4css/tina4.scss +17 -17
- data/lib/tina4/scss_compiler.rb +178 -178
- data/lib/tina4/seeder.rb +567 -567
- data/lib/tina4/service_runner.rb +303 -303
- data/lib/tina4/session.rb +297 -297
- data/lib/tina4/session_handlers/database_handler.rb +72 -72
- data/lib/tina4/session_handlers/file_handler.rb +67 -67
- data/lib/tina4/session_handlers/mongo_handler.rb +49 -49
- data/lib/tina4/session_handlers/redis_handler.rb +43 -43
- data/lib/tina4/session_handlers/valkey_handler.rb +43 -43
- data/lib/tina4/shutdown.rb +84 -84
- data/lib/tina4/sql_translation.rb +158 -158
- data/lib/tina4/swagger.rb +124 -124
- data/lib/tina4/template.rb +894 -894
- data/lib/tina4/templates/base.twig +26 -26
- data/lib/tina4/templates/errors/302.twig +14 -14
- data/lib/tina4/templates/errors/401.twig +9 -9
- data/lib/tina4/templates/errors/403.twig +29 -29
- data/lib/tina4/templates/errors/404.twig +29 -29
- data/lib/tina4/templates/errors/500.twig +38 -38
- data/lib/tina4/templates/errors/502.twig +9 -9
- data/lib/tina4/templates/errors/503.twig +12 -12
- data/lib/tina4/templates/errors/base.twig +37 -37
- data/lib/tina4/test_client.rb +159 -159
- data/lib/tina4/testing.rb +340 -340
- data/lib/tina4/validator.rb +174 -174
- data/lib/tina4/version.rb +1 -1
- data/lib/tina4/webserver.rb +312 -312
- data/lib/tina4/websocket.rb +343 -343
- data/lib/tina4/websocket_backplane.rb +190 -190
- data/lib/tina4/wsdl.rb +564 -564
- data/lib/tina4.rb +458 -458
- data/lib/tina4ruby.rb +4 -4
- metadata +3 -3
|
@@ -1,325 +1,325 @@
|
|
|
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 ──────────────────────
|
|
200
|
-
|
|
201
|
-
Tina4::Router.post("/api/gallery/queue/produce") do |request, response|
|
|
202
|
-
body = request.body || {}
|
|
203
|
-
task = body["task"] || "default-task"
|
|
204
|
-
data = body["data"] || {}
|
|
205
|
-
|
|
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)
|
|
219
|
-
end
|
|
220
|
-
|
|
221
|
-
# ── Status — list all messages with statuses ──────────────────
|
|
222
|
-
|
|
223
|
-
Tina4::Router.get("/api/gallery/queue/status") do |request, response|
|
|
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"] })
|
|
304
|
-
end
|
|
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
|
|
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 ──────────────────────
|
|
200
|
+
|
|
201
|
+
Tina4::Router.post("/api/gallery/queue/produce") do |request, response|
|
|
202
|
+
body = request.body || {}
|
|
203
|
+
task = body["task"] || "default-task"
|
|
204
|
+
data = body["data"] || {}
|
|
205
|
+
|
|
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)
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
# ── Status — list all messages with statuses ──────────────────
|
|
222
|
+
|
|
223
|
+
Tina4::Router.get("/api/gallery/queue/status") do |request, response|
|
|
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"] })
|
|
304
|
+
end
|
|
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
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name": "REST API", "description": "A simple JSON API with GET and POST endpoints", "try_url": "/api/gallery/hello"}
|
|
1
|
+
{"name": "REST API", "description": "A simple JSON API with GET and POST endpoints", "try_url": "/api/gallery/hello"}
|