Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_createLock() {
let redlock = new Redlock(
// you should have one client for each independent redis node
// or cluster
[this.redisConnection],
{
// the expected clock drift; for more details
// see http://redis.io/topics/distlock
driftFactor: 0.01, // time in ms
// the max number of times Redlock will attempt
// to lock a resource before erroring
retryCount: 3,
// the time in ms between attempts
retryDelay: 300, // time in ms
// the max time in ms randomly added to retries
useFactory: (redis: Redis.Redis): Redlock => {
const redlockClient = new Redlock([redis], {
// the expected clock drift; for more details
// see http://redis.io/topics/distlock
driftFactor: 0.01, // time in ms
// the max number of times Redlock will attempt
// to lock a resource before erroring
retryCount: 350,
// the time in ms between attempts
retryDelay: 100, // time in ms
// the max time in ms randomly added to retries
// to improve performance under high contention
// see https://www.awsarchitectureblog.com/2015/03/backoff.html
retryJitter: 1000, // time in ms
});
constructor(redisUrl: string) {
const redis = new Redis(redisUrl, {
retryStrategy: (times: number): number => {
warn("Lost connection to redis. Retrying to connect...");
const delay = Math.min(times * 50, 2000);
return delay;
},
});
this.redlock = new Redlock([redis], {
// the expected clock drift; for more details
// see http://redis.io/topics/distlock
driftFactor: 0.01, // time in ms
// the max number of times Redlock will attempt
// to lock a resource before erroring
retryCount: 100,
// the time in ms between attempts
retryDelay: 100, // time in ms
// the max time in ms randomly added to retries
// to improve performance under high contention
// see https://www.awsarchitectureblog.com/2015/03/backoff.html
retryJitter: 1000, // time in ms
});
async onStart() {
this.locker = new RedLock([this.uw.redis]);
const current = await this.getCurrentEntry();
if (current && this.timeout === null) {
// Restart the advance timer after a server restart, if a track was
// playing before the server restarted.
const duration = (current.media.end - current.media.start) * ms('1 second');
const endTime = Number(current.playedAt) + duration;
if (endTime > Date.now()) {
this.timeout = setTimeout(
() => this.advance(),
endTime - Date.now(),
);
} else {
this.advance();
}
}
constructor({ connection, prefix = JOB_PREFIX, ttl = JOB_TTL, tz, disableRedisConfig } = {}) {
const DB_NUMBER = (connection && connection.db) || 0;
this.prefix = prefix;
this.ttl = ttl;
this.tz = tz;
this.client = new Redis(connection);
this.subscriber = new Redis(connection);
this.redlock = new Redlock([this.client], { retryCount: 0 });
this.jobs = {};
this.qas = [];
if (!disableRedisConfig) {
this.subscriber.config('SET', 'notify-keyspace-events', 'Ex');
}
// Subscribe to expiring keys on the jobs DB:
this.subscriber.subscribe(`__keyevent@${DB_NUMBER}__:expired`);
this.subscriber.on('message', (channel, message) => {
// Check to make sure that the message is a job run request:
if (!message.startsWith(`${this.prefix}:work:`)) return;
const jobName = message.startsWith(`${this.prefix}:work:demand:`)
? message.replace(`${this.prefix}:work:demand:`, '')
locker.unlock = sinon.spy(lock => { throw new redlock.LockError('fail!'); });
const tracker = createTracker('test', redis, locker);
client.get(resource, function(err, value) {
var lock = new Redlock.Lock(redlock, resource, value, 1);
console.log('Unlocking', resource, value);
lock.unlock(function(err) {
if (err) throw err;
console.log("Unlocked", blog.id);
sync(blog.id, function(err, folder, done) {
if (err) throw err;
done(null, function(err) {
if (err) throw err;
console.log('Successfully acquired and released sync for', blog.id);
process.exit();
});
});
});
});
return new Promise(resolve => {
if (!redis) {
console.log(
'Connecting to Redis "redis://%s:%d/0"',
config.host,
config.port,
)
redis = new Redis(config)
redlock = new Redlock([redis])
redis.on('connect', () => {
resolve(redis)
})
} else {
resolve(redis)
}
})
}