Skip to content

Instantly share code, notes, and snippets.

View dpjayasekara's full-sized avatar

dpjayasekara

View GitHub Profile
@dpjayasekara
dpjayasekara / pool.js
Created October 14, 2019 21:11 — forked from deepal/pool.js
Worker Threads Pool
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) {
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;
async function getUser(userId) {
if (!userId) throw new InvalidInputError('userId is not provided');
try {
return getUserFromApi(userId);
} catch (err) {
throw err;
}
}
router.get('/users', async (req, res) => {
try {
res.status(200).send(await getUsers());
} catch (err) {
res.status(500).send({error: err.message});
}
})
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);
});
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');
}
function myAsyncFunction() {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('oops'))
}, 1000);
})
}
myAsyncFunction()
.then(() => {
#!/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=""
function myAsyncFunction(callback) {
setTimeout(() => {
callback(new Error('oops'))
}, 1000);
}
myAsyncFunction((err) => {
if (err) {
// handle error
} else {
function myAsyncFunction() {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('oops'))
}, 1000);
});
}
(async() => {
try {