File tree Expand file tree Collapse file tree 1 file changed +14
-3
lines changed Expand file tree Collapse file tree 1 file changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ const P = require('./promise');
88const semaphore = require ( './semaphore' ) ;
99const { NewlineReader } = require ( './file_reader' ) ;
1010const dbg = require ( './debug_module' ) ( __filename ) ;
11+ const APPEND_ATTEMPTS_LIMIT = 5 ;
1112
1213/**
1314 * PersistentLogger is a logger that is used to record data onto disk separated by newlines.
@@ -105,10 +106,20 @@ class PersistentLogger {
105106 * @param {string } data
106107 */
107108 async append ( data ) {
108- const fh = await this . init ( ) ;
109-
110109 const buf = Buffer . from ( data + '\n' , 'utf8' ) ;
111- await fh . write ( this . fs_context , buf , buf . length ) ;
110+
111+ for ( let attempt = 0 ; attempt < APPEND_ATTEMPTS_LIMIT ; ++ attempt ) {
112+ const fh = await this . init ( ) ;
113+ //if another process has deleted the active file,
114+ //this process' _poll_active_file_change might have closed the fd
115+ //in that case fd is -1
116+ //in order to avoid inter-process locking, we just re-init
117+ //the fd to the new active file.
118+ if ( fh . fd === - 1 ) continue ;
119+ await fh . write ( this . fs_context , buf , buf . length ) ;
120+ break ;
121+ }
122+
112123 this . local_size += buf . length ;
113124 }
114125
You can’t perform that action at this time.
0 commit comments