Skip to content

Commit

Permalink
Merge pull request #215 from NarrativeScience/fix/file-input-durability
Browse files Browse the repository at this point in the history
Adds error handling for removing watched files
  • Loading branch information
msmathers authored Jan 16, 2020
2 parents 2e91c5f + c2a46a6 commit 91906e4
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion inputs/file/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion inputs/file/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "log.io-file-input",
"version": "0.4.5",
"version": "0.4.6",
"description": "Watches files on disk and sends new messages to the server",
"homepage": "http://logio.org",
"repository": {
Expand Down
28 changes: 16 additions & 12 deletions inputs/file/src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ async function sendNewMessages(
fd = await openAsync(filePath, 'r')
fds[filePath] = fd
}
const readBuffer = Buffer.alloc(newSize - oldSize)
await readAsync(fd, readBuffer, 0, newSize - oldSize, oldSize)
const offset = Math.max(newSize - oldSize, 0)
const readBuffer = Buffer.alloc(offset)
await readAsync(fd, readBuffer, 0, offset, oldSize)
const messages = readBuffer.toString().split('\r\n').filter(msg => !!msg.trim())
messages.forEach((message) => {
client.write(`+msg|${streamName}|${sourceName}|${message}\0`)
Expand Down Expand Up @@ -85,17 +86,20 @@ async function startFileWatcher(
const fileSizes = await initializeFileSizes(inputPath, isDir)
const watcher = fs.watch(inputPath)
watcher.on('change', async (eventType: string, fileName: string) => {
if (eventType === 'rename') { return }
const filePath = isDir ? path.join(inputPath, fileName) : inputPath
const newSize = (await statAsync(filePath)).size
await sendNewMessages(
client,
streamName,
sourceName,
filePath,
newSize,
fileSizes[filePath] || 0,
)
fileSizes[filePath] = newSize
try {
const newSize = (await statAsync(filePath)).size
await sendNewMessages(
client,
streamName,
sourceName,
filePath,
newSize,
fileSizes[filePath] || 0,
)
fileSizes[filePath] = newSize
} catch {}
})
}

Expand Down
2 changes: 1 addition & 1 deletion server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "log.io",
"version": "0.4.5",
"version": "0.4.6",
"description": "Listens for new messages over TCP and broadcasts to browsers via socket.io",
"homepage": "http://logio.org",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "log.io-ui",
"version": "0.4.5",
"version": "0.4.6",
"license": "Apache-2.0",
"private": true,
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion ui/src/components/screens/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
div {
font-size: 12px;
line-height: 16px;
color: #72E34C;
color: #7AEE4F;
white-space: pre-wrap;
}
}
Expand Down

0 comments on commit 91906e4

Please sign in to comment.