Skip to content

Commit b9453b3

Browse files
committed
fix: README should explain a clean shutdown
Since there are various events and ways to track when `TailFile` is finished, be clear with an example of what to do to shut down and track the last known file position. Fixes: #24 Semver: patch
1 parent dc31f3b commit b9453b3

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

README.md

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ in Node.js could also benefit from using this.
1717
* [Example consuming `data` events](#example-consuming-data-events)
1818
* [Example using `pipe`](#example-using-pipe)
1919
* [Example using `readline`](#example-using-readline)
20+
* [Example for Clean Shutdown](#example-for-clean-shutdown)
2021
* **[Events](#events)**
2122
* [Event: 'flush'](#event-flush)
2223
* [Event: 'renamed'](#event-renamed)
@@ -146,6 +147,46 @@ startTail().catch((err) => {
146147
})
147148
```
148149

150+
### Example for Clean Shutdown
151+
152+
`TailFile` will call `flush()` when `quit()` is called. Therefore, to exit cleanly,
153+
one must simply await the `quit` call. If the implementation wishes to keep track of
154+
the last position read from the file (for resuming in the same spot later, for example),
155+
then a simple listener can be added to always track the file position. That way, when
156+
`quit()` is called, it will get properly updated.
157+
158+
```js
159+
const TailFile = require('@logdna/tail-file')
160+
let position // Can be used to resume the last position from a new instance
161+
162+
const tail = new TailFile('./somelog.txt')
163+
164+
process.on('SIGINT', () => {
165+
tail.quit()
166+
.then(() => {
167+
console.log(`The last read file position was: ${position}`)
168+
})
169+
.catch((err) => {
170+
process.nextTick(() => {
171+
console.error('Error during TailFile shutdown', err)
172+
})
173+
})
174+
})
175+
176+
tail
177+
.on('flush', ({lastReadPosition}) => {
178+
position = lastReadPosition
179+
})
180+
.on('data', (chunk) => {
181+
console.log(chunk.toString())
182+
})
183+
.start()
184+
.catch((err) => {
185+
console.error('Cannot start. Does the file exist?', err)
186+
throw err
187+
})
188+
```
189+
149190
## Events
150191

151192
`TailFile` is a [`Readable` stream][`Readable`], so it can emit any events from that
@@ -274,7 +315,7 @@ that data will not flow through the stream [until it's consumed][reading modes].
274315
* Returns: `undefined`
275316
* Emits: [`close`](#event-any-readable-event) when the parent `Readstream` is ended.
276317

277-
This function closes all streams and exits cleanly. The parent `TailFile` stream will be
318+
This function calls `flush`, then closes all streams and exits cleanly. The parent `TailFile` stream will be
278319
properly ended by pushing `null`, therefore an `end` event may be emitted as well.
279320

280321
## Program Flow
@@ -306,7 +347,7 @@ but `pipe` or [data events][data] are not immediately set up, `TailFile` may enc
306347
backpressure if its [`push()`][push] calls exceed the [high water mark][high water].
307348
Backpressure can also happen if `TailFile` becomes unpiped.
308349
In these cases, `TailFile` will stop polling and wait until data is flowing before
309-
polling resumes.
350+
polling resumes.
310351

311352
### Log Rolling During Backpressure
312353

@@ -333,4 +374,4 @@ stream almost immediately upon creation.
333374
[reading modes]: https://nodejs.org/dist/latest-v12.x/docs/api/stream.html#stream_two_reading_modes
334375
[async/await iterators]: https://nodejs.org/dist/latest-v12.x/docs/api/stream.html#stream_consuming_readable_streams_with_async_iterators
335376
[push]: https://nodejs.org/dist/latest-v12.x/docs/api/stream.html#stream_readable_push_chunk_encoding
336-
[high water]: https://nodejs.org/dist/latest-v12.x/docs/api/stream.html#stream_readable_readablehighwatermark
377+
[high water]: https://nodejs.org/dist/latest-v12.x/docs/api/stream.html#stream_readable_readablehighwatermark

0 commit comments

Comments
 (0)