hamster-web 0.1.0.pre.2 → 0.1.0.pre.5
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 +42 -0
- data/README.md +576 -5
- data/lib/hamster/agent_guidance.rb +56 -0
- data/lib/hamster/application_graph.rb +336 -0
- data/lib/hamster/build/assets.rb +72 -0
- data/lib/hamster/build/callbacks.rb +124 -0
- data/lib/hamster/build/jobs.rb +78 -0
- data/lib/hamster/build/manifest.rb +31 -9
- data/lib/hamster/build/query_accessors.rb +18 -0
- data/lib/hamster/check.rb +122 -0
- data/lib/hamster/cli.rb +105 -12
- data/lib/hamster/data/persistence.rb +11 -6
- data/lib/hamster/data/read/nodes.rb +2 -0
- data/lib/hamster/data/read/schema.rb +28 -16
- data/lib/hamster/data/read/validator.rb +3 -0
- data/lib/hamster/data/record.rb +15 -1
- data/lib/hamster/data/table.rb +22 -3
- data/lib/hamster/database/database.rb +2 -25
- data/lib/hamster/database/migrator.rb +151 -0
- data/lib/hamster/database/pool.rb +7 -3
- data/lib/hamster/database/result.rb +3 -3
- data/lib/hamster/dev.rb +116 -0
- data/lib/hamster/endpoint_generator.rb +7 -0
- data/lib/hamster/form.rb +110 -0
- data/lib/hamster/generators/plan.rb +96 -0
- data/lib/hamster/generators/runner.rb +168 -0
- data/lib/hamster/jobs/adapters.rb +106 -0
- data/lib/hamster/jobs/postgres_queue.rb +146 -0
- data/lib/hamster/jobs/worker.rb +103 -0
- data/lib/hamster/jobs.rb +116 -0
- data/lib/hamster/migration.rb +36 -0
- data/lib/hamster/native_smoke.rb +53 -0
- data/lib/hamster/schema_validation.rb +4 -2
- data/lib/hamster/storage/disk.rb +89 -0
- data/lib/hamster/storage.rb +152 -0
- data/lib/hamster/test_database.rb +63 -0
- data/lib/hamster/test_runner.rb +29 -0
- data/lib/hamster/testing.rb +110 -0
- data/lib/hamster/web/action/base.rb +49 -4
- data/lib/hamster/web/application.rb +9 -0
- data/lib/hamster/web/assets.rb +28 -0
- data/lib/hamster/web/context.rb +122 -1
- data/lib/hamster/web/files.rb +118 -0
- data/lib/hamster/web/http.rb +4 -2
- data/lib/hamster/web/json.rb +39 -0
- data/lib/hamster/web/multipart.rb +138 -0
- data/lib/hamster/web/router.rb +13 -2
- data/lib/hamster/web/routing/base.rb +4 -2
- data/lib/hamster/web/turbo.rb +24 -0
- data/lib/hamster/web/version.rb +1 -1
- data/lib/hamster/web/view/form.rb +18 -1
- data/lib/hamster/web.rb +9 -0
- data/templates/app/.agents/skills/hamster-database/SKILL.md +29 -0
- data/templates/app/.agents/skills/hamster-endpoint/SKILL.md +27 -0
- data/templates/app/.dockerignore +19 -0
- data/templates/app/AGENTS.md +20 -7
- data/templates/app/Dockerfile +77 -0
- data/templates/app/Gemfile +5 -1
- data/templates/app/README.md +365 -2
- data/templates/app/app/application.rb +15 -0
- data/templates/app/app/assets/application.css +2 -0
- data/templates/app/app/assets/application.js +2 -0
- data/templates/app/app/assets/controllers/hello_controller.js +5 -0
- data/templates/app/app/assets/vendor/VERSIONS.md +12 -0
- data/templates/app/app/assets/vendor/stimulus.LICENSE.txt +9 -0
- data/templates/app/app/assets/vendor/stimulus.js +2563 -0
- data/templates/app/app/assets/vendor/turbo.LICENSE.txt +20 -0
- data/templates/app/app/assets/vendor/turbo.js +7178 -0
- data/templates/app/app/routes/mappings.rb +1 -0
- data/templates/app/app/routes/up/index/action.rb +14 -0
- data/templates/app/app/views/layouts/application.html.erb +2 -1
- data/templates/app/bin/hamster +8 -0
- data/templates/app/bin/server +13 -1
- data/templates/app/bin/worker +24 -0
- data/templates/app/config/application.rb +1 -1
- data/templates/app/config/deploy.worker.yml.example +32 -0
- data/templates/app/public/.keep +0 -0
- data/templates/app/test/home_test.rb +8 -0
- data/templates/app/test/native_http.rb +11 -0
- data/templates/app/test/native_server.rb +10 -0
- data/templates/app/test/test_helper.rb +8 -0
- metadata +58 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 91dc53f9ea0ecfa5b4d437c99c86188bb55c61a7a68b42d38dfd087c6c0f9ac2
|
|
4
|
+
data.tar.gz: 144c3ab169fd08f7046c3d50a19e18e1ba629a8fde097b0ae43b842250c817e4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fdc5e4c5d4f81ef761184994e62d11c181ada38238b2a7ed9eda16ce95b9b3b7aea16f111487d423ed0238c6c801b90a1577de16db5e3ec712ac36daf782061a
|
|
7
|
+
data.tar.gz: b02521bfdfaef2bde9eb06b9b2af8510f78b5c4f3d61fe7abddb82336210db53da2f9e1e1d1540b8f5587d73efd873b5f92e4bc7e3b806d91eeb63e01c7d6739
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.1.0.pre.5] - 2026-11-01
|
|
4
|
+
|
|
5
|
+
- Install missing application dependencies before activating the app bundle on
|
|
6
|
+
first setup, and skip installation when the bundle is already satisfied.
|
|
7
|
+
- Include the complete native release probe harness in the isolated consumer
|
|
8
|
+
image so every probe can run against the published package.
|
|
9
|
+
|
|
10
|
+
## [0.1.0.pre.4] - 2026-11-01
|
|
11
|
+
|
|
12
|
+
- Require Spinner 0.1.0.pre.4 for binary-safe buffered, streaming and WebSocket
|
|
13
|
+
transport on the pinned unmodified Spinel toolchain.
|
|
14
|
+
- Add reversible Ruby migrations, status/history, integrity tracking and schema
|
|
15
|
+
snapshots alongside existing SQL migrations.
|
|
16
|
+
- Add independent table/column enabled contracts, including native record guards
|
|
17
|
+
and detailed/JSON read-only schema validation.
|
|
18
|
+
- Add staged endpoint, action, route, view, table, migration, form, component,
|
|
19
|
+
Stimulus, attachment and job generators with collision protection, dry runs
|
|
20
|
+
and exact JSON change reports.
|
|
21
|
+
- Add explicit action callbacks/concerns, bounded multipart uploads, streaming
|
|
22
|
+
downloads and permission-aware attachment metadata/local disk storage.
|
|
23
|
+
- Add explicit JSON rendering/parsing, format negotiation, validated input forms,
|
|
24
|
+
CSRF verification helpers, compiled components and Turbo response helpers.
|
|
25
|
+
- Vendor pinned MIT-licensed Turbo/Stimulus distributions and generate immutable
|
|
26
|
+
fingerprinted asset registries without requiring a Node build pipeline.
|
|
27
|
+
- Add request tests, disposable database helpers, unified quick/full checks,
|
|
28
|
+
optional Git hooks, static application inspection and a native dev rebuild loop.
|
|
29
|
+
- Generate versioned project agent instructions and discoverable local skills.
|
|
30
|
+
- Add versioned native jobs with inline, test and bounded async adapters; add a
|
|
31
|
+
durable PostgreSQL queue, scheduling/retries/cancellation and separately built
|
|
32
|
+
native workers with Docker/Kamal examples. Delivery is at least once.
|
|
33
|
+
- Record the Active Job portability spike and the deliberate smaller native API;
|
|
34
|
+
do not claim Rails Active Job compatibility or production readiness.
|
|
35
|
+
- Bootstrap the application bundle before loading the CLI on first setup.
|
|
36
|
+
- Include Minitest in generated test bundles so request checks work under Bundler.
|
|
37
|
+
- Extend native, browser CRUD, PostgreSQL recovery and Linux image verification.
|
|
38
|
+
|
|
39
|
+
## [0.1.0.pre.3] - 2026-10-29
|
|
40
|
+
|
|
41
|
+
- Publish the clean-install framework preview with explicit PostgreSQL-backed
|
|
42
|
+
applications, records/queries, compiled views and native application packaging.
|
data/README.md
CHANGED
|
@@ -7,13 +7,18 @@ its HTTP server dependency.
|
|
|
7
7
|
The starter generates and compiles with PostgreSQL support, explicit routes,
|
|
8
8
|
actions, compiled views, and SQL migrations. Database creation and migration
|
|
9
9
|
commands, the console, and schema-aware records/queries are available.
|
|
10
|
-
|
|
10
|
+
JSON actions, forms, compiled components, fingerprinted assets, attachments and
|
|
11
|
+
native jobs/workers are implemented in this source checkout. Telemetry extraction
|
|
12
|
+
remains under development.
|
|
11
13
|
|
|
12
|
-
|
|
14
|
+
This preview pairs Hamster 0.1.0.pre.5 with Spinner 0.1.0.pre.4. See
|
|
15
|
+
[CHANGELOG.md](CHANGELOG.md) for the framework changes in this release.
|
|
16
|
+
|
|
17
|
+
Install the public framework preview from RubyGems:
|
|
13
18
|
|
|
14
19
|
```sh
|
|
15
|
-
gem
|
|
16
|
-
gem install
|
|
20
|
+
gem install bundler --version 2.5.23
|
|
21
|
+
gem install hamster-web --version 0.1.0.pre.5
|
|
17
22
|
hamster new MyApp
|
|
18
23
|
cd my_app
|
|
19
24
|
bin/hamster setup
|
|
@@ -28,7 +33,7 @@ bin/hamster build
|
|
|
28
33
|
bin/hamster serve
|
|
29
34
|
```
|
|
30
35
|
|
|
31
|
-
Run framework tests with `ruby -Ilib -e 'Dir["test/*_test.rb"].sort.each { |file|
|
|
36
|
+
Run framework tests with `ruby -Ilib -e 'Dir["test/*_test.rb"].sort.each { |file| require File.expand_path(file) }'`.
|
|
32
37
|
|
|
33
38
|
## Records and queries
|
|
34
39
|
|
|
@@ -59,3 +64,569 @@ configured PostgreSQL database. It reports missing tables/columns, incompatible
|
|
|
59
64
|
types, and unexpected nullability with a nonzero exit status. It never migrates
|
|
60
65
|
or modifies the schema. Configuration is loaded through config/commands.rb,
|
|
61
66
|
a CRuby-only adapter generated with new applications.
|
|
67
|
+
|
|
68
|
+
Tables default to `table :posts, enabled: true`. Set `enabled: false` to keep a
|
|
69
|
+
declaration while preventing queries, joins, record access and persistence.
|
|
70
|
+
Subclasses inherit the flag; table and column flags remain independent. Disabled
|
|
71
|
+
columns cannot be read or written, including through record setters. Disabled
|
|
72
|
+
timestamps are excluded from automatic writes. Enabled tables must exist; disabled
|
|
73
|
+
tables skip column validation regardless of physical presence. These flags never
|
|
74
|
+
change the physical schema.
|
|
75
|
+
|
|
76
|
+
`hamster db:validate --detailed` reports table existence and column check state;
|
|
77
|
+
`--json` emits a versioned report with `valid`, `errors`, and `tables`. Both keep
|
|
78
|
+
validation read-only and return a nonzero status on mismatches. Default output
|
|
79
|
+
remains quiet about disabled declarations.
|
|
80
|
+
|
|
81
|
+
## Installation contract and Linux verification
|
|
82
|
+
|
|
83
|
+
The clean-install gate targets Ruby 3.3.6, Bundler 2.5.23, Hamster
|
|
84
|
+
0.1.0.pre.4, Spinner 0.1.0.pre.4, PostgreSQL 16.6, and unmodified Spinel/Spin
|
|
85
|
+
revision `10f112c51666e7ffd308e0ab90a69c2184870c06`. The exact CI inputs live
|
|
86
|
+
in `script/ci/versions.env`; Linux compatibility is verified by the native
|
|
87
|
+
smoke workflow. This pin does not include the experimental benchmark runtime.
|
|
88
|
+
|
|
89
|
+
The server gem is named `spinel-spinner`; Ruby application code uses
|
|
90
|
+
`require "spinner"` and the `Spinner` namespace. Hamster installs it as a
|
|
91
|
+
separate, exact-version dependency. The RubyGems package named `spinner` is
|
|
92
|
+
unrelated. Neither framework gem bundles a compiler or a precompiled server.
|
|
93
|
+
|
|
94
|
+
Spinel and its `spin` project tool must be installed before app setup. On
|
|
95
|
+
Debian Bookworm, install `build-essential`, `ca-certificates`, `curl`, `git`,
|
|
96
|
+
`pkg-config`, `libffi-dev`, and `libssl-dev`. PostgreSQL must be available;
|
|
97
|
+
`postgresql-client` provides the smoke test's `psql` checks. Build the pinned
|
|
98
|
+
upstream toolchain into a dedicated installation prefix:
|
|
99
|
+
|
|
100
|
+
```sh
|
|
101
|
+
git clone https://github.com/matz/spinel.git
|
|
102
|
+
cd spinel
|
|
103
|
+
git checkout --detach 10f112c51666e7ffd308e0ab90a69c2184870c06
|
|
104
|
+
make deps
|
|
105
|
+
make -j2
|
|
106
|
+
make install PREFIX="$HOME/.local"
|
|
107
|
+
export PATH="$HOME/.local/bin:$PATH"
|
|
108
|
+
spinel --version
|
|
109
|
+
spin --version
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Then run the app generation commands above from an empty directory. Setup
|
|
113
|
+
fetches the PostgreSQL driver revision in the generated `spin.lock` from
|
|
114
|
+
`https://github.com/rubys/spinel-pg`. RubyGems and GitHub network access are
|
|
115
|
+
required. Configure `DATABASE_HOST`, `DATABASE_PORT`, `DATABASE_NAME`,
|
|
116
|
+
`DATABASE_USERNAME`, and `DATABASE_PASSWORD` for your PostgreSQL server.
|
|
117
|
+
`DATABASE_ADMIN_NAME` defaults to `postgres`; administrative credentials default
|
|
118
|
+
to the application credentials and need permission to create the database.
|
|
119
|
+
The generated README describes the defaults and build/serve commands.
|
|
120
|
+
|
|
121
|
+
To build a local framework preview instead of installing the public package:
|
|
122
|
+
|
|
123
|
+
```sh
|
|
124
|
+
gem build hamster-web.gemspec
|
|
125
|
+
gem install --local hamster-web-0.1.0.pre.5.gem
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Run the clean Linux gate locally with Docker running (no local PostgreSQL or
|
|
129
|
+
Spinel installation needed):
|
|
130
|
+
|
|
131
|
+
```sh
|
|
132
|
+
# Public packages, installed directly from RubyGems:
|
|
133
|
+
bash script/ci/run_native_smoke.sh published
|
|
134
|
+
|
|
135
|
+
# Candidate Hamster with the released Spinner dependency:
|
|
136
|
+
mkdir -p tmp
|
|
137
|
+
gem build hamster-web.gemspec --output tmp/hamster-web.gem
|
|
138
|
+
bash script/ci/run_native_smoke.sh candidate tmp/hamster-web.gem
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Each invocation builds an image without caches, installs the compiler from its
|
|
142
|
+
pinned upstream source, and tests in a separate consumer image containing no
|
|
143
|
+
Hamster/Spinner checkout. Only the CI harness and optional candidate gems enter
|
|
144
|
+
that environment. Gems, Bundler settings, and the generated app use fresh
|
|
145
|
+
isolated directories. Package resolution must stay within that gem installation.
|
|
146
|
+
The test creates a disposable database, applies a real migration twice, checks
|
|
147
|
+
its effects and tracking, compiles the app, and asserts HTTP 200 and welcome
|
|
148
|
+
content from the native executable. Containers, their processes, networks, and
|
|
149
|
+
the tagged test image are removed on exit. Docker retains image build layers;
|
|
150
|
+
no restored layers are used by the gate.
|
|
151
|
+
|
|
152
|
+
Logs, lockfiles, resolved package paths, and HTTP output are retained under
|
|
153
|
+
`tmp/native-smoke-<mode>/` (override with `SMOKE_ARTIFACTS_DIR`). GitHub Actions
|
|
154
|
+
uploads those diagnostics even on failure. Pull requests and main test the
|
|
155
|
+
candidate package; weekly and manually triggered runs also test published
|
|
156
|
+
packages. The optional third argument installs a local Spinner gem before Hamster; both
|
|
157
|
+
versions, installed paths, and candidate archive/file hashes are checked before
|
|
158
|
+
generation and under the app bundle. Omitting it uses the published Spinner pin.
|
|
159
|
+
Published-package runs and normal RubyGems installs resolve the separate
|
|
160
|
+
Spinner `0.1.0.pre.4` release.
|
|
161
|
+
|
|
162
|
+
## Generated deployment images
|
|
163
|
+
|
|
164
|
+
New applications include a multi-stage `Dockerfile`, `.dockerignore`, and a
|
|
165
|
+
`GET /up` liveness endpoint. Build with `docker build -t my_app .`; no host Ruby,
|
|
166
|
+
Spinel, or database is needed. The default image runs the native executable as
|
|
167
|
+
UID 10001 on port 4000, with shared libraries, migrations and public assets only.
|
|
168
|
+
Ruby and the compiler are confined to build/operational stages.
|
|
169
|
+
|
|
170
|
+
Build `--target migrations` separately and run that image's `db:migrate`
|
|
171
|
+
command with database settings supplied at runtime before deploying the native
|
|
172
|
+
image. Migrations never run during image build or server startup. `/up` bypasses
|
|
173
|
+
database checkout after the application's initial database/migration verification,
|
|
174
|
+
so it reports process liveness rather than database readiness. The generated
|
|
175
|
+
README includes deployment commands, environment settings and shutdown semantics.
|
|
176
|
+
|
|
177
|
+
Run `bash script/ci/run_app_docker_smoke.sh` to verify the generated image using
|
|
178
|
+
Docker alone. It packages and installs the current framework inside a temporary
|
|
179
|
+
container, generates an app, builds before creating any database, checks secret
|
|
180
|
+
exclusions and runtime linkage, applies a real migration, verifies HTTP and Docker
|
|
181
|
+
health checks as a non-root user without Ruby/Spinel, stops PostgreSQL to test
|
|
182
|
+
liveness, and verifies clean SIGTERM shutdown. Diagnostics are retained in
|
|
183
|
+
`tmp/app-docker-smoke/`. This runs as a separate job in the native smoke workflow.
|
|
184
|
+
To test a future Spinner candidate, set `SPINNER_CANDIDATE_GEM` to its built gem.
|
|
185
|
+
The harness stages candidate gems and replaces the installation step only in the
|
|
186
|
+
temporary test Dockerfile. Generated deployment Dockerfiles retain their normal
|
|
187
|
+
released-package installation workflow.
|
|
188
|
+
|
|
189
|
+
For the full build/upload, PostgreSQL provisioning, migration, and public HTTPS
|
|
190
|
+
sequence, see [Deploy a generated app to DigitalOcean](docs/deployment/digitalocean.md)
|
|
191
|
+
and its [Caddy configuration example](docs/deployment/Caddyfile.example).
|
|
192
|
+
|
|
193
|
+
### Migration lifecycle
|
|
194
|
+
|
|
195
|
+
`bin/hamster db:migrate` applies chronological `db/migrations/VERSION_name.sql`
|
|
196
|
+
and `.rb` files. Ruby files define one `Hamster::Migration` subclass with explicit
|
|
197
|
+
`up` and `down` methods. For example:
|
|
198
|
+
|
|
199
|
+
```ruby
|
|
200
|
+
class CreateWidgets < Hamster::Migration
|
|
201
|
+
def up
|
|
202
|
+
create_table :widgets, columns: {id: :bigint, name: :text}
|
|
203
|
+
add_index :widgets, :name, name: :widgets_name
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def down
|
|
207
|
+
drop_table :widgets
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
The tooling supports `add_column`, `remove_column`, `change_column`,
|
|
213
|
+
`remove_index` and `execute(sql)` for custom PostgreSQL DDL. Each migration runs
|
|
214
|
+
in its own transaction: do not include transaction-control SQL or operations
|
|
215
|
+
such as concurrent index creation that require running outside a transaction.
|
|
216
|
+
Ruby migrations run in CRuby tooling, never in the compiled server.
|
|
217
|
+
|
|
218
|
+
Use `db:rollback STEP=1`, `db:migrate:status`, or `db:migrate:history --json`.
|
|
219
|
+
Rollback preflights the selected batch and refuses SQL migrations or Ruby files
|
|
220
|
+
without `down`. Applied files must remain available and unchanged. Existing
|
|
221
|
+
tracking rows keep their versions/timestamps with unknown historical checksums;
|
|
222
|
+
new applications record SHA-256 checksums and apply/rollback events. All tracking
|
|
223
|
+
and migration changes are serialized through a PostgreSQL advisory lock.
|
|
224
|
+
History JSON has `format_version`, `migrations`, and ordered `events` fields.
|
|
225
|
+
`legacy_unknown` denotes a preserved tracking row without checksum provenance.
|
|
226
|
+
|
|
227
|
+
`db:structure:dump` explicitly writes a schema-only `pg_dump` to
|
|
228
|
+
`db/structure.sql`; it is a derived snapshot, never a migration input. It requires
|
|
229
|
+
PostgreSQL client tools. Run database commands only against an explicitly chosen
|
|
230
|
+
database; obtain authorization before changing production databases.
|
|
231
|
+
|
|
232
|
+
### Action callbacks and concerns
|
|
233
|
+
|
|
234
|
+
The router invokes framework-owned `dispatch`, which runs before hooks, your
|
|
235
|
+
existing `call`, and then after hooks. Declare hooks with literal symbols:
|
|
236
|
+
|
|
237
|
+
```ruby
|
|
238
|
+
module Authentication
|
|
239
|
+
extend Hamster::Action::Callbacks
|
|
240
|
+
before_action :authenticate
|
|
241
|
+
|
|
242
|
+
def authenticate
|
|
243
|
+
return respond(body: "Unauthorized", status: :unauthorized) unless context.same_origin?
|
|
244
|
+
nil
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
Put shared modules in `app/concerns/` and include them in action classes.
|
|
250
|
+
Before hooks return `nil` to continue or an HTTP response to halt. After hooks
|
|
251
|
+
accept the response, return `nil` to retain it, or return a replacement response.
|
|
252
|
+
They run in registration order, with inherited hooks first, only after a successful
|
|
253
|
+
`call`; they do not run on halts or exceptions. Use `ensure` for unconditional
|
|
254
|
+
cleanup. Repeated inclusion of a concern does not duplicate its hooks.
|
|
255
|
+
|
|
256
|
+
Run `bin/hamster manifest` after changing callbacks. The manifest generates direct
|
|
257
|
+
method calls for both CRuby and Spinel; dynamic callback names are unsupported.
|
|
258
|
+
Code loaded outside a generated manifest does not have generated callback chains.
|
|
259
|
+
Do not override `dispatch` or the generated `run_before_actions`/
|
|
260
|
+
`run_after_actions` methods. Generator flags follow this runtime API.
|
|
261
|
+
|
|
262
|
+
### Uploads and downloads
|
|
263
|
+
|
|
264
|
+
`context.form` parses URL-encoded forms as before. For multipart forms, it returns
|
|
265
|
+
arrays of UTF-8 text values per key; `context.uploads` returns arrays of
|
|
266
|
+
`Hamster::UploadedFile` per key. Parsing is lazy and shared between these APIs.
|
|
267
|
+
Uploads expose `filename`, untrusted `content_type`, `size`, and binary `io`.
|
|
268
|
+
`copy_to(destination)` explicitly creates a new durable file without overwriting
|
|
269
|
+
an existing path. Client filenames never determine temporary storage paths.
|
|
270
|
+
Request-owned temporary files are deleted when the response closes, including
|
|
271
|
+
HEAD and interrupted transfers. Copy files before handing work to another process.
|
|
272
|
+
|
|
273
|
+
Actions can call `send_file("report.bin", root: trusted_directory)` or
|
|
274
|
+
`send_data(io_or_string, filename: "report.bin")`. IO bodies are read in 64 KiB
|
|
275
|
+
chunks and closed after the response. Downloads default to attachment disposition,
|
|
276
|
+
`application/octet-stream`, and `nosniff`; use explicit validated MIME types for
|
|
277
|
+
trusted content. Download paths must be within the canonical trusted root and
|
|
278
|
+
cannot contain symlinks beneath it. Authorization belongs in the action or its
|
|
279
|
+
before hooks. A HEAD request falls back to its GET route and never enumerates the
|
|
280
|
+
response body. Range requests are not implemented.
|
|
281
|
+
|
|
282
|
+
Generated servers expose `MAX_BODY_BYTES` (1 MiB), `BODY_MEMORY_BYTES` (64 KiB),
|
|
283
|
+
`SPOOL_DIRECTORY`, and `SPOOL_BUDGET_BYTES` (128 MiB across Spinner workers).
|
|
284
|
+
Multipart limits are `UPLOAD_MAX_PARTS` (128), `UPLOAD_MAX_HEADER_BYTES` (16 KiB per
|
|
285
|
+
part), `UPLOAD_MAX_FIELD_BYTES` (64 KiB per field), and `UPLOAD_MAX_FILE_BYTES`
|
|
286
|
+
(defaults to the request limit). `UPLOAD_DIRECTORY` selects the private upload
|
|
287
|
+
spool directory. Increase request and file limits explicitly for larger uploads.
|
|
288
|
+
Upload copies use additional disk space, bounded per request by the body limit;
|
|
289
|
+
provision temporary disk for concurrent requests in addition to Spinner's spool
|
|
290
|
+
budget. Parser limits are checked even when called outside Spinner. Malformed
|
|
291
|
+
multipart bodies return 400; exceeded limits return 413.
|
|
292
|
+
|
|
293
|
+
Binary response transport requires Spinner `0.1.0.pre.4`. Spinner handles binary
|
|
294
|
+
socket writes for buffered responses, streams, and WebSockets with the pinned
|
|
295
|
+
compiler. Existing apps should update their exact Spinner Gemfile pin and remove
|
|
296
|
+
the old compatibility require, then rebuild their native executable. Hamster owns
|
|
297
|
+
file bodies, path validation, authorization hooks, and upload cleanup. Multipart
|
|
298
|
+
header captures use owned string copies to avoid the pinned runtime's saved-regex
|
|
299
|
+
GC bug across workers; header and file limits remain unchanged. The native gate
|
|
300
|
+
repeats file acceptance and disconnect checks three times.
|
|
301
|
+
|
|
302
|
+
### Project-local agent guidance and endpoint generation
|
|
303
|
+
|
|
304
|
+
New apps include versioned `AGENTS.md` plus discoverable `hamster-endpoint` and
|
|
305
|
+
`hamster-database` skills under `.agents/skills/`. No global agent configuration
|
|
306
|
+
is required. Agents that do not automatically discover these files can read them
|
|
307
|
+
explicitly from AGENTS.md.
|
|
308
|
+
|
|
309
|
+
Run `bin/hamster generate --help` or `bin/hamster generate --schema` for the
|
|
310
|
+
installed generator contract. For example:
|
|
311
|
+
|
|
312
|
+
```sh
|
|
313
|
+
bin/hamster generate endpoint posts/show --method GET --path /posts/:id --name post --view
|
|
314
|
+
bin/hamster manifest:check
|
|
315
|
+
bin/hamster build
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
Use repeatable `--concern Module` and `--before method` options for existing
|
|
319
|
+
concerns and callback methods. Generation validates in staging and refuses file,
|
|
320
|
+
route, or name conflicts. Automatic route edits require the conventional generated
|
|
321
|
+
Mappings class with literal routes and explicit names; customized layouts receive
|
|
322
|
+
a diagnostic without partial output. No authentication stubs are generated.
|
|
323
|
+
|
|
324
|
+
Existing apps can run `bin/hamster agents:install --dry-run`, then
|
|
325
|
+
`bin/hamster agents:install`. Managed-file hashes live in
|
|
326
|
+
`.agents/hamster-guidance.json`. Missing files are installed, unchanged managed
|
|
327
|
+
files are upgraded, and customized files are preserved with a manual-merge
|
|
328
|
+
notice and nonzero exit status. Review old guidance against the current templates
|
|
329
|
+
when a conflict is reported. Component generators and the unified check/test CLI
|
|
330
|
+
are described below; consult the installed --help/--schema contract for supported
|
|
331
|
+
commands.
|
|
332
|
+
|
|
333
|
+
### Composable generators
|
|
334
|
+
|
|
335
|
+
Use `bin/hamster generate --schema` for the installed contract. Standalone
|
|
336
|
+
`action posts/show`, `view posts/show`, and `route posts/show --path /posts/:id`
|
|
337
|
+
compose the same files as `endpoint posts/show --view`. Use `--no-view` for a
|
|
338
|
+
body-only action. `route --to App::Routes::Posts::Show::Action` targets an existing
|
|
339
|
+
statically declared action. Automatic route edits require conventional literal
|
|
340
|
+
Mappings declarations; customized routing receives a diagnostic.
|
|
341
|
+
|
|
342
|
+
`generate table posts title:text` creates an explicit `Posts` declaration for
|
|
343
|
+
physical table `posts`; it never creates or migrates the database. `generate
|
|
344
|
+
migration create_posts` creates an intentionally unimplemented reversible Ruby
|
|
345
|
+
migration; fill in both methods before running it. `--timestamp YYYYMMDDhhmmss`
|
|
346
|
+
selects an explicit unique version.
|
|
347
|
+
|
|
348
|
+
All generators accept `--dry-run` and `--json`. JSON lists the exact changed
|
|
349
|
+
relative files, including refreshed manifests. Proposals are parsed and checked
|
|
350
|
+
in staging before writes; collisions, symlinks, and unresolved callbacks fail
|
|
351
|
+
without changing the app. Dry runs leave the app untouched.
|
|
352
|
+
|
|
353
|
+
### Shared checks and optional hooks
|
|
354
|
+
|
|
355
|
+
`bin/hamster check --quick` checks Ruby syntax, manifest freshness, literal route
|
|
356
|
+
references/conflicts and action view references without rewriting files or
|
|
357
|
+
connecting to a database. `--json` returns versioned diagnostic codes, relative
|
|
358
|
+
paths and line numbers. `check --full` adds project `test/**/*_test.rb` tests and
|
|
359
|
+
compiles `test/native_server.rb`, starts it on a private ephemeral loopback port,
|
|
360
|
+
and checks `/up`. It also runs `test/native_http.rb` when present. The default
|
|
361
|
+
native test entrypoint uses no database; database tests must explicitly select
|
|
362
|
+
an isolated disposable test database. CI runs full checks in the native consumer.
|
|
363
|
+
|
|
364
|
+
Run `bin/hamster hooks:install` explicitly from an app Git root to configure
|
|
365
|
+
`.githooks/pre-commit`; add `--pre-push` for full checks before pushing. Customized
|
|
366
|
+
hooks are preserved. Cloning or generating an app never changes Git config.
|
|
367
|
+
Hooks can be bypassed with Git's normal `--no-verify` option; CI is authoritative.
|
|
368
|
+
|
|
369
|
+
### JSON actions and explicit negotiation
|
|
370
|
+
|
|
371
|
+
Use `respond(json: {id: 1}, status: :created)` for UTF-8 JSON; `json: nil`
|
|
372
|
+
produces `null`. Supply exactly one of `view:`, `body:`, or `json:`. Only JSON
|
|
373
|
+
scalars, arrays, and hashes with string/symbol keys are supported. Select record
|
|
374
|
+
fields explicitly; arbitrary objects, duplicate normalized keys, cycles, invalid
|
|
375
|
+
UTF-8 and nonfinite numbers are rejected.
|
|
376
|
+
|
|
377
|
+
`context.json` lazily parses and caches `application/json` or `application/*+json`
|
|
378
|
+
bodies with a default 1 MiB limit (server-owned env key `hamster.json_max_bytes`
|
|
379
|
+
can lower it). Invalid bodies return 400, wrong media types 415, and oversized
|
|
380
|
+
bodies 413. JSON and form parsing are mutually exclusive per request. Spinner's
|
|
381
|
+
transport limit still applies independently.
|
|
382
|
+
|
|
383
|
+
Branch on `context.wants_json?` or `context.format` (`:html`, `:json`,
|
|
384
|
+
`:turbo_stream`, `:none`) when an endpoint offers variants. Negotiation honors
|
|
385
|
+
Accept quality/specificity and defaults to HTML for absent Accept or ties;
|
|
386
|
+
respond with 406 explicitly for `:none`. Negotiated responses include `Vary:
|
|
387
|
+
Accept, Turbo-Frame`, including the HTML branch. Existing HTML/Turbo rendering
|
|
388
|
+
continues through `respond(view: ...)` and `type: :turbo_stream`.
|
|
389
|
+
|
|
390
|
+
### Request tests and disposable databases
|
|
391
|
+
|
|
392
|
+
`bin/hamster test [test/path] [--json]` runs CRuby request tests. New endpoints
|
|
393
|
+
include tests that assert their actual generated response. `check --full` runs
|
|
394
|
+
the same tests plus compiled HTTP checks. Extend `test/native_http.rb` for your
|
|
395
|
+
application's native assertions; `test/native_server.rb` is an explicit test
|
|
396
|
+
entrypoint and defaults to no database.
|
|
397
|
+
|
|
398
|
+
`Hamster::Testing::Client` accepts headers, JSON, form, raw and multipart bodies;
|
|
399
|
+
responses expose status, headers, body, parsed `json`, and `content_type`. Bodies
|
|
400
|
+
and request inputs close even on HEAD or iteration failures. Tests inherit
|
|
401
|
+
`Hamster::Testing::Test` for status, content-type, HTML and JSON assertions.
|
|
402
|
+
|
|
403
|
+
For real database tests, require `hamster/test_database`, make the vendored pg
|
|
404
|
+
driver available, and call `Hamster::Testing::TestDatabase.with_database` with
|
|
405
|
+
your migration path. Explicitly set `HAMSTER_ENV=test`, `HAMSTER_TEST_DATABASE=1`
|
|
406
|
+
and `TEST_DATABASE_USERNAME` (plus optional TEST_DATABASE_HOST/PORT/PASSWORD).
|
|
407
|
+
Unset DATABASE_NAME and DATABASE_URL. Each invocation creates a unique database,
|
|
408
|
+
migrates it, and drops only that owned database in ensure. Non-loopback hosts
|
|
409
|
+
require `HAMSTER_TEST_ALLOW_REMOTE=1`; use a dedicated test PostgreSQL service.
|
|
410
|
+
`TestDatabase.rollback(database) { |connection| ... }` isolates fixture changes
|
|
411
|
+
within a transaction that always rolls back. Never share that connection with
|
|
412
|
+
another thread. No production endpoint is inferred from application settings.
|
|
413
|
+
|
|
414
|
+
### Development rebuild loop
|
|
415
|
+
|
|
416
|
+
Run `bin/hamster dev` after setup. It hashes source/view/config/asset/test files,
|
|
417
|
+
debounces changes, runs fast CRuby syntax checks, regenerates deterministic
|
|
418
|
+
manifests through the build command, compiles Spinel, checks freshness, and starts
|
|
419
|
+
the new executable. The server stops as soon as an edit is observed. Failed
|
|
420
|
+
builds leave it stopped, show compiler diagnostics, and recover on the next edit.
|
|
421
|
+
Changes during compilation trigger another build before any server starts.
|
|
422
|
+
|
|
423
|
+
One build runs at a time; polling uses a bounded interval. Build timings are
|
|
424
|
+
printed after every successful rebuild. Ctrl-C/TERM drains the entire child
|
|
425
|
+
server process group with a deadline, then forces cleanup if necessary. Full
|
|
426
|
+
request/native tests remain explicit (`check --full`); dev reports the checks it
|
|
427
|
+
actually ran. AOT compilation still occurs on edits; no incremental compiler or
|
|
428
|
+
runtime code reloading is promised. Compare reported timings after small action,
|
|
429
|
+
view and route edits on your machine before tuning the loop.
|
|
430
|
+
|
|
431
|
+
### Static application inspection
|
|
432
|
+
|
|
433
|
+
`bin/hamster inspect --json` emits a deterministic versioned graph of routes,
|
|
434
|
+
actions, views, tables/columns, queries, concerns, methods/callbacks and tests.
|
|
435
|
+
Nodes have stable declaration-based IDs and relative source paths/lines; edges
|
|
436
|
+
carry `declared`, `inferred`, or `unknown` certainty. `inspect routes --json` and
|
|
437
|
+
`inspect action posts/create --json` filter the graph. `--check` returns nonzero
|
|
438
|
+
for unresolved callbacks/routes, missing views or declared disabled dependencies;
|
|
439
|
+
these checks also participate in `check --quick`.
|
|
440
|
+
|
|
441
|
+
Inspection parses declarations and manifests without executing application Ruby,
|
|
442
|
+
loading credentials, or connecting to a database. It reports dynamic/unresolved
|
|
443
|
+
references instead of claiming complete Ruby analysis. Test-to-action links use
|
|
444
|
+
generator path conventions and are explicitly inferred. No live DB inspection
|
|
445
|
+
is performed. Graph facts update automatically after renames and new components.
|
|
446
|
+
|
|
447
|
+
### Explicit input forms
|
|
448
|
+
|
|
449
|
+
Subclass `Hamster::Form` and declare literal fields, for example `field :title,
|
|
450
|
+
required: true, max_length: 200`, `field :count, type: :integer`, or `field
|
|
451
|
+
:published, type: :boolean`. Construct it with a Hash of scalar input values.
|
|
452
|
+
`valid?`, field-keyed `errors` and `error_codes`, preserved `values`, and explicit
|
|
453
|
+
`attributes` keep missing, blank, malformed, invalid and unauthorized input
|
|
454
|
+
separate. Invalid forms refuse `attributes`; undeclared fields fail validation.
|
|
455
|
+
Sensitive fields opt into `sensitive: true` and are excluded from preserved values.
|
|
456
|
+
|
|
457
|
+
Optional `model Post, operation: :create` (or `:update`) checks declared column
|
|
458
|
+
flags and write permissions; it never changes or inspects the physical schema.
|
|
459
|
+
Form validation performs no persistence. On invalid submission, construct
|
|
460
|
+
`Hamster::View::Form` with action, explicit csrf_token, values and errors, render
|
|
461
|
+
labels/inputs/error_summary, and return 422 for HTML or Turbo Stream responses.
|
|
462
|
+
Valid writes normally redirect with 303. POST URL-encoded forms may use the
|
|
463
|
+
helper's `_method` field for PATCH/DELETE; other methods cannot be overridden.
|
|
464
|
+
|
|
465
|
+
CSRF token generation/storage belongs to the application's session policy.
|
|
466
|
+
Before processing a mutation, call `context.valid_csrf?(expected_session_token)`
|
|
467
|
+
in an explicit before hook. It requires same-origin metadata and compares the
|
|
468
|
+
submitted form/header token without early per-byte exits. Empty tokens fail.
|
|
469
|
+
The rendering helper escapes tokens, labels, user values and errors; password
|
|
470
|
+
inputs are never repopulated. `csrf_token` and `_method` are reserved control
|
|
471
|
+
fields and are never returned as persistence attributes.
|
|
472
|
+
|
|
473
|
+
`generate form posts/create` creates a `PostsCreateForm` and a compiled
|
|
474
|
+
`forms/posts/create` template taking an explicit `form` local. Customize fields,
|
|
475
|
+
authorized action and CSRF policy, then run manifest/check/test/build.
|
|
476
|
+
|
|
477
|
+
### Compiled components, Turbo and assets
|
|
478
|
+
|
|
479
|
+
Views support literal text, escaped `<%= expression %>`, comments, explicit
|
|
480
|
+
`locals:`, literal-name `render` with explicit locals, conditional/each blocks,
|
|
481
|
+
and named layout slots via `content_for`/`yield`. They are compiled restricted
|
|
482
|
+
ERB, not arbitrary Rails ERB evaluation. Missing references/locals and dependency
|
|
483
|
+
cycles fail before deployment. Use `generate component card` to create a compiled
|
|
484
|
+
`components/card` partial. Pass component content/slots as explicit locals;
|
|
485
|
+
ordinary strings escape, while reviewed `Hamster::HTML` fragments retain markup.
|
|
486
|
+
|
|
487
|
+
`Hamster::Turbo.frame(id, content)` and `.stream(action, target, content)` validate
|
|
488
|
+
DOM identifiers and escape untrusted content. Stream actions are replace, append,
|
|
489
|
+
prepend, remove and update. Respond with `type: :turbo_stream`; use 422 for invalid
|
|
490
|
+
submissions and 303 redirects for successful ordinary HTML forms. Explicit Accept
|
|
491
|
+
order breaks equally specific quality ties, so Turbo's preferred stream type is
|
|
492
|
+
honored while absent/wildcard Accept defaults to HTML.
|
|
493
|
+
|
|
494
|
+
Put CSS, JavaScript and images/fonts in `app/assets/`. Manifest generation writes
|
|
495
|
+
content fingerprints under `public/assets/`, a JSON mapping, and a static native
|
|
496
|
+
registry. Relative ESM imports and CSS URLs are rewritten to fingerprinted URLs;
|
|
497
|
+
missing imports and cycles fail clearly. Use `Hamster::Assets.path("application.css")`
|
|
498
|
+
in compiled views. Only registered fingerprint paths are served, with correct
|
|
499
|
+
MIME, nosniff and immutable caching; arbitrary public files are not exposed.
|
|
500
|
+
The runtime root is `HAMSTER_APP_ROOT` or the working directory. Old fingerprints
|
|
501
|
+
may remain on disk for cached clients; deploy assets with their native binary.
|
|
502
|
+
|
|
503
|
+
Generated apps vendor pinned MIT-licensed Turbo and Stimulus distributions; see
|
|
504
|
+
`app/assets/vendor/VERSIONS.md`. `generate stimulus greeting` registers a controller
|
|
505
|
+
automatically in the generated module registry. `application.js` is the editable
|
|
506
|
+
entrypoint. No Node, npm, Vite or bundler is required for app setup/build/runtime.
|
|
507
|
+
Static relative module imports are the supported default; use an explicit custom
|
|
508
|
+
pipeline if you need npm package resolution or advanced bundling.
|
|
509
|
+
|
|
510
|
+
The framework's `examples/frontend` provides native/browser CRUD acceptance with
|
|
511
|
+
components, forms, escaping, CSRF, Turbo updates, Stimulus and no-JS fallback.
|
|
512
|
+
|
|
513
|
+
### Private attachments and local storage
|
|
514
|
+
|
|
515
|
+
Require `hamster/storage`. `generate attachments storage` creates the explicit
|
|
516
|
+
SQL migration for blob metadata and attachment references; review and migrate
|
|
517
|
+
it separately. Instantiate `Hamster::Storage::Disk` with a private existing
|
|
518
|
+
parent/mounted directory (0700), then `Storage::Attachments.new(database: db,
|
|
519
|
+
adapter: disk)`. Adapters implement `put(io, key:, max_bytes:)`, `open(key)`,
|
|
520
|
+
`delete(key)` and `exists?(key)`; put returns byte_size and SHA-256 checksum.
|
|
521
|
+
Local disk is implemented; object storage is a future optional adapter.
|
|
522
|
+
|
|
523
|
+
Upload with explicit filename/owner, then attach the returned blob ID to a
|
|
524
|
+
record_type/record_id/name with owner and an authorization callback. Storage
|
|
525
|
+
keys are independent random values; client names are display metadata only.
|
|
526
|
+
`open`, `download`, `detach` and `delete` require explicit authorization callbacks
|
|
527
|
+
receiving metadata. Bind identity to authenticated application state, never an
|
|
528
|
+
untrusted request owner parameter. Downloads verify size/checksum, default to
|
|
529
|
+
private attachment/octet-stream/nosniff, and return closeable streaming bodies.
|
|
530
|
+
Never place the storage directory under public/ or fingerprinted app assets.
|
|
531
|
+
|
|
532
|
+
Metadata and files are not one atomic transaction: pending metadata commits
|
|
533
|
+
before file writes, and ready is published only after a completed, flushed disk
|
|
534
|
+
write. Storage uses its own idle DB connections; attach records only after the
|
|
535
|
+
business record commits. Failed uploads remain pending for recovery. Deletion
|
|
536
|
+
locks metadata, refuses shared references, commits deleting state, removes the
|
|
537
|
+
file, then removes metadata. Retries are idempotent; a failed disk deletion
|
|
538
|
+
remains recoverable. Detaching one reference never deletes a shared blob.
|
|
539
|
+
|
|
540
|
+
Run privileged `cleanup(older_than: cutoff, limit: 100)` explicitly with a grace
|
|
541
|
+
period longer than the maximum upload/attachment workflow. It removes old
|
|
542
|
+
unreferenced ready/pending/deleting blobs and rechecks references under lock.
|
|
543
|
+
No async job system is required. Mount the same private persistent volume across
|
|
544
|
+
single-host web restarts and back up metadata and files together. Multiple hosts
|
|
545
|
+
need shared storage/an object adapter; local disk does not provide replication.
|
|
546
|
+
|
|
547
|
+
### Native jobs and bounded asynchronous execution
|
|
548
|
+
|
|
549
|
+
The Active Job portability spike and reuse decision are recorded in the framework's
|
|
550
|
+
`docs/compatibility/active-job.md`. The current native API deliberately accepts a
|
|
551
|
+
single explicit JSON argument hash, not Rails arbitrary arguments or GlobalID.
|
|
552
|
+
|
|
553
|
+
Subclass `Hamster::Job`, declare `job "mail.deliver", version: 1`, and implement
|
|
554
|
+
`perform(arguments, version:)`. Use `perform_now({"id" => 1})` or
|
|
555
|
+
`perform_later({"id" => 1}, metadata: {})`. Payloads are versioned, limited to
|
|
556
|
+
64 KiB and JSON values, and copied across the queue boundary. Declare older
|
|
557
|
+
supported payload versions with `accepts: [1]` when advancing the current version;
|
|
558
|
+
handle the provided version explicitly. `generate job deliver_mail` creates an
|
|
559
|
+
intentionally unimplemented job. Run manifest/build after adding or changing jobs.
|
|
560
|
+
The generated registry calls known job classes directly under Spinel.
|
|
561
|
+
|
|
562
|
+
The default adapter is inline. Configure `Hamster::Jobs::Async.new(threads: 2,
|
|
563
|
+
capacity: 64)` explicitly for bounded in-process execution. Full queues raise
|
|
564
|
+
`Overloaded`; stopped queues reject admission. Worker threads never inherit
|
|
565
|
+
request-scoped DB connections; acquire their own scoped connection inside jobs.
|
|
566
|
+
Failures are isolated and retained in a bounded `failures` list containing only
|
|
567
|
+
job identity and error class, without arguments or exception-message data.
|
|
568
|
+
|
|
569
|
+
Async jobs are ephemeral: restart/crash loses queued or active work. Shutdown
|
|
570
|
+
stops admission and drains to a deadline, then drops pending payloads and returns
|
|
571
|
+
false if user code remains active. Running user code is not killed unsafely;
|
|
572
|
+
its bounded execution slot remains until return or process replacement. Hamster
|
|
573
|
+
application shutdown invokes job shutdown before closing its DB pool. Standalone
|
|
574
|
+
programs must call `Hamster::Jobs.shutdown` themselves. Spinner owns no job pool
|
|
575
|
+
or job lifecycle.
|
|
576
|
+
|
|
577
|
+
For deterministic tests configure `Hamster::Jobs::TestAdapter`, inspect its
|
|
578
|
+
serialized `enqueued` payloads, then call `perform_enqueued` or `clear`. Shut down
|
|
579
|
+
an async adapter before replacing it. The candidate native CI also compiles and
|
|
580
|
+
runs CPU/concurrency/failure probes for jobs and binary disk storage.
|
|
581
|
+
|
|
582
|
+
### Durable PostgreSQL jobs and native workers
|
|
583
|
+
|
|
584
|
+
`generate job_queue queue` creates an explicit queue migration (format version 1)
|
|
585
|
+
and installs a missing worker entrypoint in older apps. Migrate it before serving
|
|
586
|
+
or running workers. Configure `Hamster::Jobs::PostgresQueue.new(database: db)` as
|
|
587
|
+
the web adapter; `perform_later({...}, at: Time.now + 60, queue: "mail", priority: 5)`
|
|
588
|
+
supports scheduling and named queues. Higher priorities run first. Inline/async
|
|
589
|
+
adapters reject scheduling, named queue and connection options.
|
|
590
|
+
|
|
591
|
+
For transaction-consistent enqueueing, pass `connection: context.connection` to
|
|
592
|
+
`perform_later`, or to `queue.enqueue(serialized_payload, connection: connection)`
|
|
593
|
+
inside the same business transaction. The connection's database is verified;
|
|
594
|
+
rollback removes both changes and the job. Otherwise enqueue uses a separate
|
|
595
|
+
checkout/commit; size the web pool accordingly. Reusing the same serialized job
|
|
596
|
+
ID/payload makes uncertain enqueue retries idempotent; conflicting payloads with
|
|
597
|
+
an existing ID are rejected. Do not regenerate IDs when retrying an uncertain
|
|
598
|
+
network result.
|
|
599
|
+
|
|
600
|
+
Run `bin/hamster build:worker`, then `bin/hamster jobs:work` (or `--once`). The
|
|
601
|
+
separate native executable statically links the application's registered jobs.
|
|
602
|
+
It uses an independent pool sized by JOB_DATABASE_POOL_SIZE (default 2*threads+2);
|
|
603
|
+
job bodies acquire their own scoped connection through `Hamster::Jobs.database`.
|
|
604
|
+
WORKER_THREADS defaults to 2 (maximum 64), JOB_QUEUES to default,
|
|
605
|
+
JOB_LEASE_SECONDS to 30, and JOB_DRAIN_SECONDS to 10.
|
|
606
|
+
|
|
607
|
+
Claims use one short `FOR UPDATE SKIP LOCKED`/`UPDATE RETURNING` statement. No
|
|
608
|
+
claim transaction remains open while user code executes. Heartbeats renew leases;
|
|
609
|
+
expired jobs recover after worker death or connection failure. Lease tokens fence
|
|
610
|
+
old acknowledgements, but cannot undo external user-code effects. Delivery is
|
|
611
|
+
**at least once**: jobs must be idempotent. There is no exactly-once guarantee.
|
|
612
|
+
Retries use capped exponential backoff and become dead after max_attempts
|
|
613
|
+
(default 5). Error metadata retains class only, not payloads/messages. Use
|
|
614
|
+
`queue.retry_dead(id)` for explicit retries and `queue.cancel(id)` for cancellation.
|
|
615
|
+
Running cancellation is cooperative (`Hamster::Jobs.cancelled?`); arbitrary user
|
|
616
|
+
code is not safely interrupted. TERM/INT stop admission and drain to a deadline;
|
|
617
|
+
unfinished work remains leased and is recovered after process replacement.
|
|
618
|
+
|
|
619
|
+
`jobs:status --json` reports versioned counts, due/scheduled backlog and oldest
|
|
620
|
+
pending timestamp without disclosing arguments. Queue schema versions are checked
|
|
621
|
+
before processing; migrations never run implicitly. For rolling upgrades deploy
|
|
622
|
+
workers accepting old and new job payload versions before producers emit the new
|
|
623
|
+
version, drain old workers, then retire old handlers in a later deployment.
|
|
624
|
+
|
|
625
|
+
`docker build --target worker -t my_app-jobs .` produces a non-root native worker
|
|
626
|
+
image without Ruby/compiler or an HTTP listener. Keep migrations operational and
|
|
627
|
+
separate. The default Docker target remains the web image. The optional
|
|
628
|
+
`config/deploy.worker.yml.example` uses a separate Kamal 2 service/worker role,
|
|
629
|
+
`builder.target: worker`, and no proxy. Configure its registry, architecture,
|
|
630
|
+
private PostgreSQL and secrets; set container stop timeout longer than drain.
|
|
631
|
+
See [Kamal roles](https://kamal-deploy.org/docs/configuration/roles/) and
|
|
632
|
+
[build targets](https://kamal-deploy.org/docs/configuration/builders/).
|