decaffeinate: Convert expire_docops.coffee and 2 other files to JS

This commit is contained in:
decaffeinate 2021-05-19 20:25:07 +01:00 committed by Alf Eaton
parent e1e0d26b1f
commit 5ad0b6eea2
3 changed files with 135 additions and 101 deletions

View file

@ -1,44 +1,54 @@
Settings = require "settings-sharelatex"
rclient = require("@overleaf/redis-wrapper").createClient(Settings.redis.documentupdater)
keys = Settings.redis.documentupdater.key_schema
async = require "async"
RedisManager = require "./app/js/RedisManager"
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const Settings = require("settings-sharelatex");
const rclient = require("@overleaf/redis-wrapper").createClient(Settings.redis.documentupdater);
let keys = Settings.redis.documentupdater.key_schema;
const async = require("async");
const RedisManager = require("./app/js/RedisManager");
getKeysFromNode = (node, pattern, callback) ->
cursor = 0 # redis iterator
keySet = {} # use hash to avoid duplicate results
# scan over all keys looking for pattern
doIteration = (cb) ->
node.scan cursor, "MATCH", pattern, "COUNT", 1000, (error, reply) ->
return callback(error) if error?
[cursor, keys] = reply
console.log "SCAN", keys.length
for key in keys
keySet[key] = true
if cursor == '0' # note redis returns string result not numeric
return callback(null, Object.keys(keySet))
else
doIteration()
doIteration()
const getKeysFromNode = function(node, pattern, callback) {
let cursor = 0; // redis iterator
const keySet = {}; // use hash to avoid duplicate results
// scan over all keys looking for pattern
var doIteration = cb => node.scan(cursor, "MATCH", pattern, "COUNT", 1000, function(error, reply) {
if (error != null) { return callback(error); }
[cursor, keys] = Array.from(reply);
console.log("SCAN", keys.length);
for (let key of Array.from(keys)) {
keySet[key] = true;
}
if (cursor === '0') { // note redis returns string result not numeric
return callback(null, Object.keys(keySet));
} else {
return doIteration();
}
});
return doIteration();
};
getKeys = (pattern, callback) ->
nodes = rclient.nodes?('master') || [ rclient ];
console.log "GOT NODES", nodes.length
doKeyLookupForNode = (node, cb) ->
getKeysFromNode node, pattern, cb
async.concatSeries nodes, doKeyLookupForNode, callback
const getKeys = function(pattern, callback) {
const nodes = (typeof rclient.nodes === 'function' ? rclient.nodes('master') : undefined) || [ rclient ];
console.log("GOT NODES", nodes.length);
const doKeyLookupForNode = (node, cb) => getKeysFromNode(node, pattern, cb);
return async.concatSeries(nodes, doKeyLookupForNode, callback);
};
TTL = 60 * 60 # 1 hour
expireDocOps = (callback) ->
getKeys keys.docOps(doc_id: "*"), (error, keys) ->
async.mapSeries keys,
(key, cb) ->
console.log "EXPIRE #{key} #{RedisManager.DOC_OPS_TTL}"
rclient.expire key, RedisManager.DOC_OPS_TTL, cb
callback
const TTL = 60 * 60; // 1 hour
const expireDocOps = callback => getKeys(keys.docOps({doc_id: "*"}), (error, keys) => async.mapSeries(keys,
function(key, cb) {
console.log(`EXPIRE ${key} ${RedisManager.DOC_OPS_TTL}`);
return rclient.expire(key, RedisManager.DOC_OPS_TTL, cb);
},
callback));
setTimeout () -> # Give redis a chance to connect
expireDocOps (error) ->
throw error if error?
process.exit()
, 1000
setTimeout(() => // Give redis a chance to connect
expireDocOps(function(error) {
if (error != null) { throw error; }
return process.exit();
})
, 1000);

View file

@ -1,41 +1,53 @@
redis = require "@overleaf/redis-wrapper"
rclient1 = redis.createClient(cluster: [{
port: "7000"
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let listenInBackground, sendPings;
const redis = require("@overleaf/redis-wrapper");
const rclient1 = redis.createClient({cluster: [{
port: "7000",
host: "localhost"
}])
}]});
rclient2 = redis.createClient(cluster: [{
port: "7000"
const rclient2 = redis.createClient({cluster: [{
port: "7000",
host: "localhost"
}])
}]});
counter = 0
sendPing = (cb = () ->) ->
rclient1.rpush "test-blpop", counter, (error) ->
console.error "[SENDING ERROR]", error.message if error?
if !error?
counter += 1
cb()
let counter = 0;
const sendPing = function(cb) {
if (cb == null) { cb = function() {}; }
return rclient1.rpush("test-blpop", counter, function(error) {
if (error != null) { console.error("[SENDING ERROR]", error.message); }
if ((error == null)) {
counter += 1;
}
return cb();
});
};
previous = null
listenForPing = (cb) ->
rclient2.blpop "test-blpop", 200, (error, result) ->
return cb(error) if error?
[key, value] = result
value = parseInt(value, 10)
if value % 10 == 0
console.log "."
if previous? and value != previous + 1
error = new Error("Counter not in order. Got #{value}, expected #{previous + 1}")
previous = value
return cb(error, value)
let previous = null;
const listenForPing = cb => rclient2.blpop("test-blpop", 200, function(error, result) {
if (error != null) { return cb(error); }
let [key, value] = Array.from(result);
value = parseInt(value, 10);
if ((value % 10) === 0) {
console.log(".");
}
if ((previous != null) && (value !== (previous + 1))) {
error = new Error(`Counter not in order. Got ${value}, expected ${previous + 1}`);
}
previous = value;
return cb(error, value);
});
PING_DELAY = 100
do sendPings = () ->
sendPing () ->
setTimeout sendPings, PING_DELAY
const PING_DELAY = 100;
(sendPings = () => sendPing(() => setTimeout(sendPings, PING_DELAY)))();
do listenInBackground = () ->
listenForPing (error, value) ->
console.error "[RECEIVING ERROR]", error.message if error
setTimeout listenInBackground
(listenInBackground = () => listenForPing(function(error, value) {
if (error) { console.error("[RECEIVING ERROR]", error.message); }
return setTimeout(listenInBackground);
}))();

View file

@ -1,33 +1,45 @@
redis = require "@overleaf/redis-wrapper"
rclient1 = redis.createClient(cluster: [{
port: "7000"
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let sendPings;
const redis = require("@overleaf/redis-wrapper");
const rclient1 = redis.createClient({cluster: [{
port: "7000",
host: "localhost"
}])
}]});
rclient2 = redis.createClient(cluster: [{
port: "7000"
const rclient2 = redis.createClient({cluster: [{
port: "7000",
host: "localhost"
}])
}]});
counter = 0
sendPing = (cb = () ->) ->
rclient1.publish "test-pubsub", counter, (error) ->
console.error "[SENDING ERROR]", error.message if error?
if !error?
counter += 1
cb()
let counter = 0;
const sendPing = function(cb) {
if (cb == null) { cb = function() {}; }
return rclient1.publish("test-pubsub", counter, function(error) {
if (error != null) { console.error("[SENDING ERROR]", error.message); }
if ((error == null)) {
counter += 1;
}
return cb();
});
};
previous = null
rclient2.subscribe "test-pubsub"
rclient2.on "message", (channel, value) ->
value = parseInt(value, 10)
if value % 10 == 0
console.log "."
if previous? and value != previous + 1
console.error "[RECEIVING ERROR]", "Counter not in order. Got #{value}, expected #{previous + 1}"
previous = value
let previous = null;
rclient2.subscribe("test-pubsub");
rclient2.on("message", function(channel, value) {
value = parseInt(value, 10);
if ((value % 10) === 0) {
console.log(".");
}
if ((previous != null) && (value !== (previous + 1))) {
console.error("[RECEIVING ERROR]", `Counter not in order. Got ${value}, expected ${previous + 1}`);
}
return previous = value;
});
PING_DELAY = 100
do sendPings = () ->
sendPing () ->
setTimeout sendPings, PING_DELAY
const PING_DELAY = 100;
(sendPings = () => sendPing(() => setTimeout(sendPings, PING_DELAY)))();