@@ -17,6 +17,7 @@ in Node.js could also benefit from using this.
17
17
* [ Example consuming ` data ` events] ( #example-consuming-data-events )
18
18
* [ Example using ` pipe ` ] ( #example-using-pipe )
19
19
* [ Example using ` readline ` ] ( #example-using-readline )
20
+ * [ Example for Clean Shutdown] ( #example-for-clean-shutdown )
20
21
* ** [ Events] ( #events ) **
21
22
* [ Event: 'flush'] ( #event-flush )
22
23
* [ Event: 'renamed'] ( #event-renamed )
@@ -146,6 +147,46 @@ startTail().catch((err) => {
146
147
})
147
148
```
148
149
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
+
149
190
## Events
150
191
151
192
` 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].
274
315
* Returns: ` undefined `
275
316
* Emits: [ ` close ` ] ( #event-any-readable-event ) when the parent ` Readstream ` is ended.
276
317
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
278
319
properly ended by pushing ` null ` , therefore an ` end ` event may be emitted as well.
279
320
280
321
## Program Flow
@@ -306,7 +347,7 @@ but `pipe` or [data events][data] are not immediately set up, `TailFile` may enc
306
347
backpressure if its [ ` push() ` ] [ push ] calls exceed the [ high water mark] [ high water ] .
307
348
Backpressure can also happen if ` TailFile ` becomes unpiped.
308
349
In these cases, ` TailFile ` will stop polling and wait until data is flowing before
309
- polling resumes.
350
+ polling resumes.
310
351
311
352
### Log Rolling During Backpressure
312
353
@@ -333,4 +374,4 @@ stream almost immediately upon creation.
333
374
[ reading modes ] : https://nodejs.org/dist/latest-v12.x/docs/api/stream.html#stream_two_reading_modes
334
375
[ async/await iterators ] : https://nodejs.org/dist/latest-v12.x/docs/api/stream.html#stream_consuming_readable_streams_with_async_iterators
335
376
[ 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