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
This is a tiny library that makes it trivial to monitor the results of repeatedly executing an asynchronous action. A canonical use-case would be monitoring a URL for healthy (e.g. status code 200) responses but really it could be anything, including actions that don’t do any IO but still have something to monitor like random computation. This is a fairly general tool and it interestingly generalizes some things that previously would have been realized with purpose-built code. For example the concept of request-retry can be generally implemented with this library, see the example below.
Examples
Declarative Request-Retry
We can combine streams together to declaratively create request-retry logic. Inspiration for this example was taken from the imperative request-retry logic in this ElasticSearch test suite which establishes the connectivity with the database before running its test suite.
importMonitorfrom"monitoring-stream"construnTests=()=>Promise.resolve("Your integration test suite here...")constpingElasticSearch=()=>Math.round(Math.random())
? Promise.reject(newError("Foobar network error"))
: Promise.resolve("OK!")constpingInterval=1000constpingAttempts=3constmonitor=Monitor.create(pingElasticSearch,pingInterval)constmaxRetries=monitor.downs.takeUntil(monitor.ups).take(pingAttempts)monitor.ups.take(1).takeUntil(maxRetries).drain().then((result)=>result.isResponsive
? runTests()
: Promise.reject(result.data)))
A monitor event represents the result of an action invocation. The type variable A represents the action’s resolution type.
isResponsive indicates if the action is resolving while isResponsiveChanged indicates if that state is different than the previous event. For the first check wherein there is no such previous event isResponsiveChanged is considered to be true.
You can think of isResponsive as being your health indicator while isResponsiveChanged as being your alert to when crashing or recovery occurs. isResponsiveChanged is technically a convenience that you could calculate yourself but seeing as its often needed and that its calculation requires state it seems worthwhile.
action The async action to recurse. This could be anything: HTTP requests, pure calculation, file system checks…
intervalMs The milliseconds between each action invocation. Defaults to 1000.
returns An observable stream of MonitorEvent. The observable implementation we use is most.
Event State Helpers
There are several statically exported predicate functions that can tell you what kind of state a MonitorEvent is in.
.isUp
Monitor.isUp(event: MonitorEvent)=>boolean
Returns true if event isResponsive is true.
.isDown
Monitor.isDown(event: MonitorEvent)=>boolean
Returns true if event isResponsive is false.
.isRise
Monitor.isRise(event: MonitorEvent)=>boolean
Returns true if event isResponsive is true and isResponsiveChanged is true.
.isFall
Monitor.isFall(event: MonitorEvent)=>boolean
Returns true if event isResponsive is false and isResponsiveChanged is true.
.isChanges
Monitor.isChanges(event: MonitorEvent)=>boolean
Returns true if event isResponsiveChanged is true.
Sub-streams
There are several additional streams on monitor instances that are filtered by event state predicates. These sub-streams provide a convenient way to consume just a subset of events without any additional code/work from you.