Example #1
0
var queue = async.queue(function(job, cb){
  var file = path.resolve(process.cwd(), config.get('cache'), job.type +
    '_' + job.name);

  lockfile.lock(file + '.lock', function(err){
    if(err){
      log.error('Failed to get sync lock');
      log.error(err.stack);
      return cb();
    }

    if(job.data){
      fs.writeFile(file + '.tmp', job.data, finish.bind(null, file, job, cb));
    } else
      https.get(job.url, function(res){
        if(res.statusCode !== 200)
          return log.info('Not 200 when syncing ' + job.name + ': ' +
            res.statusCode);

        var writer = fs.createWriteStream(file + '.tmp');

        writer.on('finish', function(){
          finish(file, job, cb);
        });

        res.pipe(writer);
      }).on('error', function(err){
        log.error('Failed sync job');
        log.error(err.stack);
        return cb();
      });
  });
}, https.globalAgent.maxSockets);
Example #2
0
 return new Promise((resolve, reject) => {
   const lock = join(path, 'buggycli.lock')
   // locking is retries for 10 minutes (every 250 ms)
   lockFile.lock(lock, { retries: 10 * 60 * 4, retryWait: 250 }, async (err) => {
     if (err) {
       reject(err)
     } else {
       try {
         const res = await exec(`npm pack ${dependency}@${version} -q`, opts)
         const tar = join(path, res.stdout.trim())
         if (!fs.existsSync(tar)) {
           throw new Error(`Expected tar file but doesn't exist: ${tar}`)
         }
         await exec('tar -xzf ' + tar + ' --strip-components=1 package', opts)
         await exec('npm i', opts)
         try {
           await fsp.unlink(tar)
         } catch (e) {
           // we don't care about errors here, but we don't want to remove the lock file before the tar is removed
         }
         resolve()
       } catch (e) {
         reject(e)
       } finally {
         lockFile.unlock(lock, (err) => {
           if (err) reject(err)
         })
       }
     }
   })
 })
Example #3
0
 function then () {
   var opts = { stale: npm.config.get("cache-lock-stale")
              , retries: npm.config.get("cache-lock-retries")
              , wait: npm.config.get("cache-lock-wait") }
   var lf = lockFileName(u)
   log.verbose("lock", u, lf)
   lockFile.lock(lf, opts, cb)
 }
 readable._read = function(size){
   readable._read = noop
   if (exports.debug) console.warn('_read', debugInfo)
   
   var script = functionToExtendScript(fn, args)
   var cliArgs = []
   
   cliArgs.push("-e", 'on run argv')
   cliArgs.push("-e", 'with timeout of 600 seconds')
   cliArgs.push("-e",   'tell application "' + evalInPhotoshop.getName() + '" to do javascript (item 1 of argv)' + (module.exports.debugger ? ' show debugger on runtime error' : ''))
   cliArgs.push("-e", 'end timeout')
   cliArgs.push("-e", 'end run')
   
   cliArgs.push(script)
   
   if (exports.debug) console.warn('WILL LOCK', debugInfo)
   
   lockFile.lock(lockPath, lockOptions, function(error, fileDescriptor){
     if (error){
       if (exports.debug) {debugInfo.error = error; console.warn('NOT LOCKED', debugInfo)}
       return readable.emit('error', error);
     }
     if (exports.debug) {console.warn('LOCKED', debugInfo)}
     if (exports.debug) {console.warn('spawn', debugInfo)}
     
     var child = spawn('/usr/bin/osascript', cliArgs)
     
     child.stdout.on('readable', function(){
       readable.push(this.read())
     })
     var _error = ''
     child.stderr.on('data', function(data){ _error += data })
 
     child.on('exit', function(code){
       if (exports.debug) {
         debugInfo.exitCode = code
         debugInfo.stderr = _error
         console.warn('spawn exit', debugInfo)
       }
       lockFile.unlock(lockPath, function(error){
         if (exports.debug) {
           debugInfo.exitCode = code
           debugInfo.error = error
           console.warn('UNLOCK', debugInfo)
         }
         readable.push(null)
         if (error) readable.emit('error', error);
         // if (_error && _error.indexOf('(-1750)') != -1) readable.emit('error', Error(_error));
         // else
         if (code) readable.emit('error', Error(_error));
       })
     })
     
   })
 }
Example #5
0
 function then () {
   var opts = { stale: npm.config.get("cache-lock-stale")
              , retries: npm.config.get("cache-lock-retries")
              , wait: npm.config.get("cache-lock-wait") }
   var lf = lockFileName(u)
   log.verbose("lock", u, lf)
   lockFile.lock(lf, opts, function(er) {
     if (!er) myLocks[lf] = true
     cb(er)
   })
 }
Example #6
0
function readDbWithLock(cb) {
  lockfile.lock('db-file.lock', { retries: 10, retryWait: 20 }, () => {
    const done = () => {
      lockfile.unlockSync('db-file.lock');
    };

    fs.readFile(__dirname + '/data.json', (err, data) => {
      cb(err, JSON.parse(data), done);
    });
  });
}
Example #7
0
  mkdirp(base, function (er) {
    if (er) return cb(er)

    var opts = { stale:   npm.config.get("cache-lock-stale")
               , retries: npm.config.get("cache-lock-retries")
               , wait:    npm.config.get("cache-lock-wait") }
    var lf = lockFileName(base, name)
    log.verbose("locking", name, "with", lf)
    lockfile.lock(lf, opts, function(er) {
      if (!er) installLocks[lf] = true
      cb(er)
    })
  })
Example #8
0
  saveRuntime: function(callback) {
    lockFile.lock(this.runtimeLockPath, {}, function() {
      var data = yaml.safeDump(this.runtimeValues || {});
      logger.info('Writing new config (PID ' + process.pid + ')...');
      logger.debug(data);
      atomic.writeFile(this.runtimePath, data, function(error) {
        if(callback) {
          callback(error);
        }
      });

    }.bind(this));
  },
Example #9
0
exports.make_coffee = function(req, res) {
	var email = req.body.email
	,	cups = req.body.cups;
	console.log(cups, email);
	lockfile.lock('execution.lock', function(er) {
		if (er) {
			console.log('coffee locked.');
			res.render('locked', { title: 'Express' });
		} else {
			sendgrid.send({
				to:email,
				from:'acm.ucr.coffeepp@gmail.com',
				subject:'Your coffee',
				html:['<p>Hello!</p><p>Your coffee will be ready in '+ cups +'.5 minutes</p>']
			}, function(){console.log(arguments)});
			var coffee = spawn('./coffee',['' + cups]);
			coffee.stdout.on('data',function(data){
				if (/^0/.test(data)) {
					var count = + data;
					console.log(count);
					fs.writeFile('public/readyornot.txt', count, function(er){
						if (er) {
							console.log('ERROR, COULD NOT SAVE.');
						}
					});
					if(!count) {
						console.log('coffee\'s ready');
						sendgrid.send({
							to:email,
							from:'acm.ucr.coffeepp@gmail.com',
							subject:'Your coffee',
							html:'<p>Hello!</p><p>Congratulations, your coffee is ready!</p>'
						}, function(){console.log(arguments)});
						lockfile.unlock('execution.lock', function(er){
							if (er) {
								console.log('failed to unlock!!!!!');
							}
						});
					}
				}
			});
			coffee.stderr.on('data',function(data){
				console.log(data.toString());
			});
			coffee.on('end',function(end){
				console.log('process ended');
			});
		  	res.render('brewing', { title: 'Express' });
		}
	});
}
Example #10
0
    mkdirp(lockDir, function () {
      if (er) return cb(er)

      var opts = { stale:   npm.config.get("cache-lock-stale")
                 , retries: npm.config.get("cache-lock-retries")
                 , wait:    npm.config.get("cache-lock-wait") }
      var lf = lockFileName(base, name)
      log.verbose("lock", "using", lf, "for", resolve(base, name))
      lockfile.lock(lf, opts, function(er) {
        if (er) log.warn("locking", "failed", er)
        if (!er) installLocks[lf] = true
        cb(er)
      })
    })
Example #11
0
internals.writeDefaultsFile = function(defaults, cb) {
    lockfile.lock(internals.DEFAULTS_LOCKFILE, internals.DEFAULTS_LOCKFILE_OPTIONS, function(lockErr) {
        if (lockErr) {
            //lock didn't work, bubble error up and don't write the file
            cb(lockErr);
        } else {
            fs.writeFile(internals.DEFAULTS_JSON_FILEPATH, JSON.stringify(defaults), function(err) {
                lockfile.unlock(internals.DEFAULTS_LOCKFILE, function(unlockErr) {
                    if (unlockErr) {
                        internals.log(unlockErr);
                    }
                    cb(err, defaults);
                });
            });
        }
    });
};
Example #12
0
internals.writeProjectListFile = function(input, cb) {
    var data = input && input.data ? input.data : internals.DEFAULT_PROJECT_LIST_DATA;
    lockfile.lock(internals.PROJECT_LIST_LOCKFILE, internals.PROJECT_LIST_LOCKFILE_OPTIONS, function(lockErr) {
        if (lockErr) {
            //lock didn't work, bubble error up and don't write the file
            cb(lockErr);
        } else {
            fs.writeFile(internals.PROJECT_LIST_JSON_FILEPATH, JSON.stringify(data), function(err) {
                lockfile.unlock(internals.PROJECT_LIST_LOCKFILE, function(unlockErr) {
                    if (unlockErr) {
                        internals.log(unlockErr);
                    }
                    cb(err, data);
                });
            });
        }
    });
};
                        SmartCrop.crop(img, canvasOptions, function(result) {
                            //console.log(JSON.stringify(result.topCrop, null, '  '));
                            var rt = result.topCrop;

                            lockFile.lock(srcPath + ".lock", lockFileOptions, function(err) {
                                if (err) {
                                    console.log(err);
                                    return reject(err);
                                }
                                //console.log("Resizing and writing to" + dstPath + "...");
                                //console.log("Cropping to", rt.width, rt.height, rt.x, rt.y);
                                //console.log("Resizing to", options.width, options.height);
                                //console.log("Source path: "+srcPath);
                                gm(srcPath)
                                    .crop(rt.width, rt.height, rt.x, rt.y)
                                    .resize(options.width, options.height, "!")
                                    .gravity('Center')
                                    .quality(options.quality)
                                    .stream(function(err, stdout, stderr){
                                        //console.log("--unlocking file");
                                        lockFile.unlock(srcPath + ".lock", function (unlockErr) {
                                            if (unlockErr) {
                                                //console.log(unlockErr);
                                                reject(unlockErr);
                                            } else {
                                                if (!err) {
                                                    //console.log("--Writing file stream to "+dstPath);
                                                    var writeStream = fs.createWriteStream(dstPath);

                                                    stdout.pipe(writeStream);
                                                    writeStream.on('close', function () {
                                                        resolve(dstPath);
                                                    });
                                                } else {
                                                    //console.log("gm error", err);
                                                    reject(err);
                                                }
                                            }
                                        });
                                    });
                            });
                        });
Example #14
0
internals.writeFileObject = function(input, cb) {
    var fileObject = input.fileObject;
    var filepath = path.resolve(internals.PROJECT_DIRECTORY, './' + input.filepath);
    var lockfilepath = filepath + '.lock'
    lockfile.lock(lockfilepath, internals.DEFAULTS_LOCKFILE_OPTIONS, function(lockErr) {
        if (lockErr) {
            //lock didn't work, bubble error up and don't write the file
            cb(lockErr);
        } else {
            fs.writeFile(filepath, JSON.stringify(fileObject), function(err) {
                lockfile.unlock(lockfilepath, function(unlockErr) {
                    if (unlockErr) {
                        internals.log(unlockErr);
                    }
                    cb(err, fileObject);
                });
            });
        }
    });
};
Example #15
0
  correctMkdir(lockDir, function (er) {
    if (er) return cb(er)

    var opts = {
      stale: npm.config.get('cache-lock-stale'),
      retries: npm.config.get('cache-lock-retries'),
      wait: npm.config.get('cache-lock-wait')
    }
    var lf = lockFileName(base, name)
    lockfile.lock(lf, opts, function (er) {
      if (er) log.warn('locking', lf, 'failed', er)

      if (!er) {
        log.verbose('lock', 'using', lf, 'for', resolve(base, name))
        installLocks[lf] = true
      }

      cb(er)
    })
  })
      return new Promise((resolve) => {
        lockfile.lock(LOCKFILE_PATH, (err) => {
          if (err) {
            atom.notifications.addInfo('PlatformIO IDE installation suspended.', {
              detail: 'Seems like PlatformIO IDE is already being installed in ' +
                      'another window.',
              dismissable: true,
            });
            resolve();
          } else {
            const promise = performInstall(state)
              .then(releaseLock, releaseLock);
            resolve(promise);
          }

          function releaseLock() {
            return lockfile.unlock(LOCKFILE_PATH, (err) => {
              if (err) {
                console.warn('Failed to release the lock: ' + err.toString());
              }
            });
          }
        });
      });
lockfile.lock('test.lock', { retries: 200, retryWait: 20 }, function (err) {
    if (!err) {
        //console.log('lock obtained for offer.spec');

        require.undef('knockout');
        require.undef('Modules/Offer');

        require.config({
            baseUrl: '../',
            paths: {
                'knockout': 'Scripts/knockout-3.2.0.debug',
                'Modules/Offer': 'Modules/Offer'
            }
        });

        require(['Modules/Offer', 'knockout'], function(Offer, ko) {
            describe('Offer', function() {
                it('Should populate title', function(done) {
                    var offerUnderTest = new Offer('title', []);
                    expect(offerUnderTest.title()).toBe('title');
                    done();
                });
                lockfile.unlock('test.lock', function(err) {
                    if (err) {
                        console.log('unlock failed in offer.spec with ' + err);
                    }
                });
            });
        });
    } else {
        console.log('lock failed in offer.spec with ' + err);
    }
});
Example #18
0
function filequeue(name, proc_fn) {
    var basedir = path.join('/tmp', name);
    var lockfilename = path.join(basedir, '.lock');
    var rejected_dir = path.join('/tmp', name + '.fail');

    if (!fs.existsSync(basedir)) {
        fs.mkdirSync(basedir);
    }

    if (!fs.existsSync(rejected_dir)) {
        fs.mkdirSync(rejected_dir);
    }

    debug('new filepile: %s (%s)', name, basedir);

    // put an item into the queue and return the item id
    function queue(details) {
        debug('pile %j', details);
        var id = uuid();
        var filename = path.join(basedir, id + '.json');
        fs.writeFileSync(filename, JSON.stringify(details));
        return id;
    }

    queue.emit = EventEmitter.prototype.emit;
    queue.on = EventEmitter.prototype.on;
    queue.once = EventEmitter.prototype.once;
    queue.removeListener = EventEmitter.prototype.removeListener;

    // if user does not provide a process function, we just expose the queue
    if (!proc_fn) {
        return queue;
    }

    var lockopts = {
        wait: 5000,
        stale: 2000,
        retryWait: 1000,
        retries: 1000000000
    };

    // lock so other processes don't try to access the files
    lockfile.lock(lockfilename, lockopts, function(err) {
        // cannot lock, assume that something else has the lockfile
        if (err) {
            return;
        }

        // keep the lockfile fresh
        var lock_interval = setInterval(function() {
            debug('refeshing lockfile');
            touch.sync(lockfilename);
        }, 1000);

        lock_interval.unref();

        // we be done
        process.once('exit', function() {
            clearInterval(lock_interval);
        });

        debug('processing items for %s using pid %d', name, process.pid);

        // kick off an initial round of processing
        files_changed();

        // new files will trigger new processing
        fs.watch(basedir, files_changed);
    });

    var processing = false;

    function files_changed(type, fname) {
        if (fname === '.lock' || processing) {
            return;
        }

        var files = fs.readdirSync(basedir).filter(function(name) {
            return name.indexOf('.lock') < 0;
        });

        if (files.length === 0) {
            return;
        }

        processing = true;

        (function next() {
            var file = files.shift();
            if (!file) {
                processing = false;
                return files_changed();
            }

            var filename = path.join(basedir, file);
            debug('processing file %s', filename);

            try {
                // maybe this file was partially written
                // or maybe it is being deleted
                // we will come back to it once another file is created
                var details = JSON.parse(fs.readFileSync(filename, 'utf8'));
            } catch (err) {
                return next();
            }

            proc_fn(details, function(err) {
                if (err) {
                    var fail_filename = path.join(rejected_dir, file);
                    debug('failed %s', fail_filename);
                    fs.renameSync(filename, fail_filename);
                    queue.emit('error', err);
                }
                else {
                    debug('done processing %s', filename);
                    fs.unlinkSync(filename);
                }

                next();
            });
        })();
    };

    // return a function we can use to queue
    return queue;
}
Example #19
0
	var pro = new Promise(function(resolve, reject)
		{
			lockmgr.lock(lockFile, extend({}, exports.lockOpts), _resolveWidthError(resolve, reject));
		})
        lockFile.check(ListHub.LOCK_FILE, {}, function (err, isLocked) {
            if(err) {
                if(_.isFunction(cb)) {
                    return cb(err);
                } else {
                    return;
                }
            }

            if(isLocked) {
                if(_.isFunction(cb)) {
                    return cb();
                } else {
                    return;
                }
            }

            lockFile.lock(ListHub.LOCK_FILE, {}, function (err) {
                if(err) {
                    lockFile.unlock(ListHub.LOCK_FILE, function (error) {
                        if(error) {
                            if(_.isFunction(cb)) {
                                return cb(error);
                            } else {
                                return;
                            }
                        }
                        if(_.isFunction(cb)) {
                            return cb(err);
                        } else {
                            return;
                        }
                    });
                    return;
                }

                self._checkFeedUpdate(function(err, isUpdated) {
                    if(err) {
                        lockFile.unlock(ListHub.LOCK_FILE, function (error) {
                            if(error) {
                                if(_.isFunction(cb)) {
                                    return cb(error);
                                } else {
                                    return;
                                }
                            }
                            if(_.isFunction(cb)) {
                                return cb(err);
                            } else {
                                return;
                            }
                        });
                        return;
                    }

                    if(!isUpdated) {
                        log('feed file is up to date');
                        lockFile.unlock(ListHub.LOCK_FILE, function (error) {
                            if(error) {
                                if(_.isFunction(cb)) {
                                    return cb(error);
                                } else {
                                    return;
                                }
                            }
                            if(_.isFunction(cb)) {
                                return cb();
                            } else {
                                return;
                            }
                        });
                        return;
                    }

                    //if feed file was updated, then download and save it
                    self._saveNewFeedFiles(function(err) {
                        if(err) {
                            lockFile.unlock(ListHub.LOCK_FILE, function (error) {
                                if(error) {
                                    if(_.isFunction(cb)) {
                                        return cb(error);
                                    } else {
                                        return;
                                    }
                                }
                                if(_.isFunction(cb)) {
                                    return cb(err);
                                } else {
                                    return;
                                }
                            });
                            return;
                        }

                        lockFile.unlock(ListHub.LOCK_FILE, function (err) {
                            if(err) {
                                if(_.isFunction(cb)) {
                                    return cb(err);
                                } else {
                                    return;
                                }
                            }
                            if(_.isFunction(cb)) {
                                return cb();
                            } else {
                                return;
                            }
                        });

                    });
                });
            })
        });
Example #21
0
/**
 * repl
 */
lockFile.lock(kLockPath, function(err) {
  if (err) {
    console.error('Failed to aquire lock');
    process.exit(1);
  }
  pLockAquired = true;
  load();

  async.forever(function(next) {
    inquirer.prompt([{
      name: 'command',
      message: '>'
    }], function(answers) {
      var argv = parseArgv(answers.command, {});
      var cmd = argv.commands.shift();
      var handler = pCommands[pCommandAbbrev[cmd]];
      if (!handler) {
        console.log('Command %j not found', cmd);
        console.log('Available commands: %s',
            Object.keys(pCommandAbbrev).join(', '));
        setImmediate(next);
      } else {
        handler(next, argv.commands, argv.options);
      }
    });
  });
});
lockfile.lock('test.lock', {retries:200,retryWait:20}, function(err) {
    if (!err) {
        //console.log('lock obtained for offers.spec');

        require.undef('Services/http');
        require.undef('Services/offers');

        require.config({
            baseUrl: '../',
            paths: {
                'Services/http': 'Specs/http-mock',
                'Services/offers': 'Services/offers'
            }
        });

        require(['Services/offers', 'Services/http'], function(offers, http) {
            describe('offers', function() {
                it('index should perform http get', function() {
                    offers.index();
                    expect(http.get).toHaveBeenCalled();
                });
                it('play should perform http get', function() {
                    offers.offersForAccount();
                    expect(http.get).toHaveBeenCalled();
                });
                lockfile.unlock('test.lock', function(err) {
                    if (err) {
                        console.log('unlock failed in offers.spec with ' + err);
                    }
                });
            });
        });
    } else {
        console.log('lock failed in offers.spec with ' + err);
    }
});
Example #23
0
lockFile.lock('dump.lock', lockFreshness, function(er) {
  if (er && DEBUG) {
    winston.warn('Could not open lockfile!');
  } else {
    if (DEBUG) {
      winston.info("lock file set", {"user": process.getuid()});
    }
    var namecoind = require('./nmc').init().then(function(nmcd) {

      nmc = nmcd;

      nmc.blockCount().then(function(value) {
        blockCount = value;
        db.get('$lastBlockCount', function(err, doc) {
          if (err) {
            lastBlockCount = blockCount - 36000; //all current blocks
            db.save('$lastBlockCount', {"blocks": blockCount});
          } else {
            if (doc.blocks < 36000) {
              lastBlockCount = blockCount - 36000;
            } else {
              lastBlockCount = doc.blocks - 6; //1 "hour" overlap between calls
            }
            db.save('$lastBlockCount', doc._rev, {"blocks": blockCount});
          }

          nmc.filter({regex: namesRegex, age: blockCount - lastBlockCount, start: 0, max: 0, stat: true})
            .then(function(result) {
              namesTotal = result.count;
              nameDump();
            });
        });

      });
    });
  }
});
Example #24
0
 lockfile: function (callback) {
   // try to lock the file
   locker.lock(lockFileName, lockOpts, callback)
 }