You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit ad5ea11
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: .work/extra/doc/3.DEVELOPMENT_EN.md
+57Lines changed: 57 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -312,3 +312,60 @@ Each service has a ```data``` directory, which is the only data channel from the
312
312
313
313
- The persistent data of databases like MySQL and Redis will be mounted to their ```data``` directories.
314
314
- Projects based on environments such as Python/PHP/Go will be mounted to their ```data``` directories.
315
+
316
+
## 6. Service Lifecycle Hooks
317
+
318
+
### (1) What are hooks
319
+
320
+
Each service can drop up to 4 scripts into its `hooks/` directory; sparrow runs each at a fixed point in the start/stop flow:
321
+
322
+
| Script | When it runs |
323
+
|---|---|
324
+
|`before_start.sh`|**Before** the container starts (before pull/build and `docker compose up`) |
325
+
|`after_start.sh`|**After** the container starts |
326
+
|`before_stop.sh`|**Before** the container stops |
327
+
|`after_stop.sh`|**After** the container stops |
328
+
329
+
Typical uses: generate or seed config files the container needs in `before_start.sh`; clean up scratch resources in `after_stop.sh`.
330
+
331
+
The dispatcher lives in the `sparrow` script: it looks for `{service}/hooks/{hook_type}.sh` and executes it if present. A missing file is treated as a no-op (not an error).
332
+
333
+
### (2) Hooks are sourced into the current shell — **do not use `exit`**
334
+
335
+
⚠️ **Important footgun**: hooks are not forked into a subshell. Sparrow runs them with `.` (source) inside its own main shell. The non-obvious consequence:
336
+
337
+
- Writing `exit 0` / `exit 1` in a hook **terminates the entire sparrow process**. The remaining steps (pull, build, `docker compose up`) are silently skipped, and the user sees what looks like a clean exit — very easy to misdiagnose.
338
+
- To return early from a hook, use **`return 0` / `return 1`** instead. That only exits the sourced block; sparrow's main flow keeps going.
339
+
340
+
Reference pattern:
341
+
342
+
```sh
343
+
#!/bin/sh
344
+
345
+
# include sdk of sparrow.
346
+
. .work/include/sdk.sh
347
+
348
+
# Config already initialized — skip and let sparrow continue.
349
+
if [ -f"./myservice/data/config.json" ];then
350
+
print_info "config already exists, skip init"
351
+
return 0 # ← correct: exits the hook only
352
+
fi
353
+
354
+
# ... initialization logic ...
355
+
if! some_init_command;then
356
+
print_error "init failed"
357
+
return 1 # ← still use return, even on the failure path
358
+
fi
359
+
```
360
+
361
+
By the same logic, **do not** use `set -e` or anything like `kill $$` inside a hook — they affect the parent shell. If you need to signal a precondition failure, log it with `print_error` and `return 1`.
362
+
363
+
### (3) What is available inside a hook
364
+
365
+
`sdk.sh` has already been sourced by the time your hook runs, so you can use sparrow's helpers directly:
0 commit comments