This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {EventEmitter} = require('events'); | |
const {Worker, MessageChannel, parentPort} = require('worker_threads'); | |
const WORKER_STATUS = { | |
IDLE: Symbol('idle'), | |
BUSY: Symbol('busy'), | |
}; | |
module.exports.WorkerPool = class WorkerPool extends EventEmitter{ | |
constructor(script, size) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const {WorkerPool} = require('./pool'); | |
const app = express(); | |
// Create a new worker pool to run script ./task.js | |
const pool = new WorkerPool('./task.js', 4); | |
app.get('/', (req, res) => { | |
const plainText = req.query.text; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function getUser(userId) { | |
if (!userId) throw new InvalidInputError('userId is not provided'); | |
try { | |
return getUserFromApi(userId); | |
} catch (err) { | |
throw err; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
router.get('/users', async (req, res) => { | |
try { | |
res.status(200).send(await getUsers()); | |
} catch (err) { | |
res.status(500).send({error: err.message}); | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.on('uncaughtException', (err) => { | |
logger.fatal('an uncaught exception detected', err); | |
process.exit(-1); | |
}); | |
process.on('unhandledRejection', (err) => { | |
logger.fatal('an unhandled rejection detected', err); | |
process.exit(-1); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
schema.pre('findOneAndUpdate', function (next) { | |
const MongooseValidationError = mongoose.Error.ValidationError, | |
model = this.model(), | |
immutableFields = ['email', 'password'], | |
update = this.getUpdate(); | |
immutableFields.forEach((field) => { | |
if (update.hasOwnProperty(field)) { | |
model.invalidate(field, `${field} cannot be modified`, update[field], 'immutable'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function myAsyncFunction() { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
reject(new Error('oops')) | |
}, 1000); | |
}) | |
} | |
myAsyncFunction() | |
.then(() => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
GIT_HOST=$(git remote -v | grep fetch | cut -d "@" -f2 | cut -d ":" -f1) | |
GIT_PROJECT=$(git remote -v | grep fetch | cut -d "@" -f2 | cut -d ":" -f2 | cut -d "." -f1) | |
GIT_URL="https://$GIT_HOST/$GIT_PROJECT" | |
TAG1=$1 | |
TAG2=$2 | |
if [ -z $2 ] | |
then | |
TAG2="" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function myAsyncFunction(callback) { | |
setTimeout(() => { | |
callback(new Error('oops')) | |
}, 1000); | |
} | |
myAsyncFunction((err) => { | |
if (err) { | |
// handle error | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function myAsyncFunction() { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
reject(new Error('oops')) | |
}, 1000); | |
}); | |
} | |
(async() => { | |
try { |