Skip to content

Instantly share code, notes, and snippets.

@crizstian
Created October 30, 2017 16:04
Show Gist options
  • Save crizstian/05f76fb85bc4fea7a3ecf4a19a5a6293 to your computer and use it in GitHub Desktop.
Save crizstian/05f76fb85bc4fea7a3ecf4a19a5a6293 to your computer and use it in GitHub Desktop.
workers logic
// ##############################################
// WORKERS
} else if (process.env.WorkerName === 'readlogs') {
console.log('πŸ‘‰πŸ½ Worker readlogs ' + process.pid + ' has started.')
process.on('message', async ({logfiles}) => {
try {
const read = await readLogFiles.parseFiles(logfiles)
process.send({read, logfiles})
} catch (e) {
console.log(`something went wrong with reading logs => ${e}`)
process.exit(0)
}
})
} else if (process.env.WorkerName === 'savelogs') {
console.log('πŸ‘‰πŸ½ Worker savelogs ' + process.pid + ' has started.')
process.on('message', async ({read, collection}) => {
try {
const save = await saveToDB.saveToDB(read, collection)
console.log(`RECORDS SAVED IN ${collection} => ${save.reduce((prev, cur) => prev + cur, 0)} πŸ€™`)
} catch (e) {
console.log(`something went wrong with saving parsed logs => ${e}`)
process.exit(0)
}
})
} else if (process.env.WorkerName === 'deletelogs') {
console.log('πŸ‘‰πŸ½ Worker deletelogs ' + process.pid + ' has started. ')
process.on('message', async ({logfiles, type}) => {
try {
const deleted = await moveAndDelete.deleteFiles(logfiles)
console.log(`LOCAL LOGS DELETED => ${deleted.length}`)
} catch (e) {
console.log(`something went wrong with deleting local logs => ${e}`)
process.exit(0)
}
})
} else if (process.env.WorkerName === 'movelogs') {
console.log('πŸ‘‰πŸ½ Worker movelogs ' + process.pid + ' has started.')
process.on('message', async ({logfiles}) => {
try {
moveAndDelete.renameFiles(logfiles)
.then(async moved => console.log(`REMOTE LOGS MOVED =>`, await moved.length))
} catch (e) {
console.log(`something went wrong with moving remote logs => ${e}`)
process.exit(0)
}
})
} else if (process.env.WorkerName === 'geolocation') {
console.log('πŸ‘‰πŸ½ Worker geolocation ' + process.pid + ' has started.')
process.on('message', async (read) => {
try {
geoip.getLocations(read)
.then(async locations => process.send(locations))
} catch (e) {
console.log(`something went wrong locating ips => ${e}`)
process.exit(0)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment