-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Docs for tickers, graceful shutdown
- Loading branch information
Showing
10 changed files
with
57 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Graceful Shutdown | ||
|
||
Microservices typically run on hardware that can shut down at any time. When a microservice receives the `SIGTERM` command to shut down, it stops accepting new operations and attempts to end all pending operations gracefully. | ||
|
||
Graceful shutdown process: | ||
* Disable tickers so new iterations are not run | ||
* Stop accepting new requests | ||
* Wait 8 seconds for running tickers, pending requests and goroutines to end naturally | ||
* Cancel the lifetime `context.Context` | ||
* Wait 4 more seconds for running tickers and pending requests, and goroutines to end | ||
* Close the NATS connection | ||
* Exit | ||
|
||
In order for goroutines to be gracefully shut down, it is important to launch them using the `Connector`'s `Go` method rather than using the standard `go` directive. When launched with `Go`, the goroutines have the `context.Context` that gets canceled during shut down. |
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,26 @@ | ||
# Tickers | ||
|
||
Tickers are background jobs that need to run irrespective of an external trigger. Tickers have a fixed schedule, with consecutive iterations separated by a fixed interval. If execution of a prior iteration takes longer than the duration of the interval and has not yet completed by the time the next iteration is due, the new iteration is skipped. | ||
|
||
Tickers run independently for each replica of the microservice, so a once an hour ticker will run 5 times an hour if 5 replicas are running concurrently. To avoid coordinated spikes of multiple tickers all running at the same time, iterations are aligned to the startup time of the microservice, not to clock time. | ||
|
||
Tickers are defined using the `Connector`s `StartTicker` method which accepts a name, an interval and a callback function (handler). If the microservice is running, the ticker activates immediately. Otherwise, it will activate when the microservice starts. `StopTicker` can be used to stop a ticker identified by its name. | ||
|
||
The more common case is to define tickers in `service.yaml` and use the [code generator](../blocks/codegen.md) to generate the skeleton code. | ||
|
||
```yaml | ||
# Tickers | ||
# | ||
# signature - Go-style method signature (no arguments) | ||
# Ticker() | ||
# description - Documentation | ||
# interval - Duration between iterations (e.g. 15m) | ||
tickers: | ||
# - signature: | ||
# description: | ||
# interval: | ||
``` | ||
|
||
Tickers are disabled in the `TESTING` [deployment environment](../tech/deployments.md) in order to avoid the unpredictability of their running schedule. | ||
|
||
If a job is running at the time that the microservice shuts down, the `ctx` argument of the handler gets canceled and the job is given a grace period to end cleanly. |
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