-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(container): added hotReload.postSyncCommand
Adds a new config option to hot reload specs: `postSyncCommand`. This is a command that is run inside the container after a hot reload sync takes place.
- Loading branch information
Showing
17 changed files
with
611 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Hot-reload post-sync command example project | ||
|
||
This example is a variation on the `hot-reload` example. Here, we demonstrate the `hotReload.postSyncCommand` option by providing a motivating example of its use. | ||
|
||
Since this project builds on the `hot-reload` example, you may want to check out that project's README if you haven't already. | ||
|
||
## Adding a `postSyncCommand` | ||
|
||
Like the `hot-reload` example project, This project contains a single service called `node-service`. When running, the service waits for requests on `/hello` and responds with a message. | ||
|
||
Here, however, we modify the `dev` npm script in `node-service/package.json` to have `nodemon` only watch a single file (`/app/hotreloadfile` in the container's directory structure) for changes: | ||
|
||
```json | ||
{ | ||
... | ||
"scripts": { | ||
"start": "node main.js", | ||
"dev": "nodemon main.js --watch hotreloadfile", | ||
"test": "echo OK" | ||
}, | ||
... | ||
} | ||
``` | ||
|
||
We also add a `postSyncCommand` to `node-service`'s `garden.yml`: | ||
|
||
```yaml | ||
kind: Module | ||
description: Node greeting service | ||
name: node-service | ||
type: container | ||
hotReload: | ||
sync: | ||
- target: /app/ | ||
postSyncCommand: [touch, /app/hotreloadfile] | ||
services: | ||
- name: node-service | ||
args: [npm, start] | ||
hotReloadArgs: [npm, run, dev] | ||
... | ||
``` | ||
|
||
When one is specified, the `postSyncCommand` is executed inside inside the running container during each hot reload, after any changed files have been synced. | ||
|
||
In this example, the idea is to "notify" the `nodemon` process that a reload is needed by `touch`-ing `hotreloadfile` during each hot reload. `nodemon` will then pick up the updated modification time, triggering a reload. `hotreloadfile` doesn't exist when the image is built (and doesn't need to for our purposes here). | ||
|
||
> Note: There's nothing special about the name `hotreloadfile` here. Any file name would do. | ||
Since `nodemon` only has to watch a single path, this approach should significantly reduce the resource footprint `nodemon`'s FS watching incurs when compared to e.g. watching all of the module's source paths. | ||
|
||
When this general approach is applicable for several modules in the system (which depends on the particular languages, frameworks and libraries being used), the lighter total FS watching footprint may facilitate more services being deployed with hot reloading enabled during development. | ||
|
||
## Usage | ||
|
||
Identical to the `hot-reload` example project. The only difference is that here, the updated message returned by `garden call` is a result of `nodemon` performing a reload after picking up the updated modification time of `hotreloadfile` (instead of noticing/watching for changes to `node-service/app.js` directly). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
kind: Project | ||
name: hot-reload-post-sync-command | ||
environments: | ||
- name: local | ||
providers: | ||
- name: local-kubernetes | ||
- name: testing | ||
providers: | ||
- name: kubernetes | ||
context: gke_garden-dev-200012_europe-west1-b_garden-dev-1 | ||
namespace: hot-reload-callback-testing-${local.env.CIRCLE_BUILD_NUM || local.username} | ||
defaultHostname: hot-reload-callback-testing.dev-1.sys.garden | ||
buildMode: cluster-docker |
4 changes: 4 additions & 0 deletions
4
examples/hot-reload-post-sync-command/node-service/.dockerignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
Dockerfile | ||
garden.yml | ||
app.yaml |
13 changes: 13 additions & 0 deletions
13
examples/hot-reload-post-sync-command/node-service/Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM node:9-alpine | ||
|
||
ENV PORT=8080 | ||
EXPOSE ${PORT} | ||
|
||
RUN npm install -g nodemon | ||
|
||
ADD . /app | ||
WORKDIR /app | ||
|
||
RUN npm install | ||
|
||
CMD ["npm", "start"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const express = require("express") | ||
|
||
const app = express() | ||
|
||
app.get("/hello", (req, res) => { | ||
res.json({message: "Hello from Node!"}) | ||
}) | ||
|
||
// This is the path GAE uses for health checks | ||
app.get("/_ah/health", (req, res) => { | ||
res.sendStatus(200) | ||
}) | ||
|
||
module.exports = { app } |
22 changes: 22 additions & 0 deletions
22
examples/hot-reload-post-sync-command/node-service/garden.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
kind: Module | ||
description: Node greeting service | ||
name: node-service | ||
type: container | ||
hotReload: | ||
sync: | ||
- target: /app/ | ||
postSyncCommand: [touch, /app/hotreloadfile] | ||
services: | ||
- name: node-service | ||
args: [npm, start] | ||
hotReloadArgs: [npm, run, dev] | ||
ports: | ||
- name: http | ||
containerPort: 8080 | ||
ingresses: | ||
- path: /hello | ||
port: http | ||
healthCheck: | ||
httpGet: | ||
path: /_ah/health | ||
port: http |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const { app } = require("./app") | ||
|
||
app.listen(process.env.PORT, "0.0.0.0", () => console.log("App started")) |
Oops, something went wrong.