Skip to content

Instantly share code, notes, and snippets.

View TheDeveloper's full-sized avatar

TheDeveloper TheDeveloper

View GitHub Profile
@TheDeveloper
TheDeveloper / hmsetex.js
Last active July 24, 2017 17:44
Node module for Redis hmsetex command
/**
* npm install --save lodash redis node-redis-scripty
*
* Usage:
* let hmsetex = require('./hmsetex.js')
* let key = 'your-key';
* let fields = { some_field: 'test', another_field: 'foo' };
* hmsetex(key, fields).then(handleResult).catch(handleError);
*/
const _ = require('lodash');
@TheDeveloper
TheDeveloper / hmsetx.lua
Created July 21, 2017 10:02
Redis HMSETX script
local key = KEYS[1]
local args = ARGV
local call = redis.call
if call("EXISTS", key) == 0 then
return nil
end
return call("HMSET", key, unpack(args))
@TheDeveloper
TheDeveloper / callback-to-promise.js
Created March 9, 2016 10:43
A little function to convert a callback-style function to Promises
// usage: promisify(function([args..., cb]) { /* do stuff with args then cb */ })()
exports.promisify = function(fn) {
return function(...args) {
return new Promise((resolve, reject) => {
fn(...args, (err, result) => {
if (err) return reject(err);
resolve(result);
});
});
};
@TheDeveloper
TheDeveloper / es-migrate.es6.js
Last active February 15, 2022 01:36
Script to migrate documents from one Elasticsearch cluster to another using Scan/Scroll
/*
Migrate documents from one elasticsearch endpoint to another.
To squeeze out extra performance, disable index refresh and replicas on the target cluster:
curl -XPUT 'http://elasticsearch:9200/_settings' -d '{
"index" : {
"refresh_interval" : "-1",
"number_of_replicas": "0"
}
/**
* A Connection handler for Amazon ES.
*
* Uses the aws-sdk to make signed requests to an Amazon ES endpoint.
* Define the Amazon ES config and the connection handler
* in the client configuration:
*
* var es = require('elasticsearch').Client({
* hosts: 'https://amazon-es-host.us-east-1.es.amazonaws.com',
* connectionClass: require('http-aws-es'),
@TheDeveloper
TheDeveloper / http-aws-es.es6.js
Last active March 31, 2021 09:27
Use the Node elasticsearch client with Amazon ES
/**
* A Connection handler for Amazon ES.
*
* Uses the aws-sdk to make signed requests to an Amazon ES endpoint.
* Define the Amazon ES config and the connection handler
* in the client configuration:
*
* var es = require('elasticsearch').Client({
* hosts: 'https://amazon-es-host.us-east-1.es.amazonaws.com',
* connectionClass: require('http-aws-es'),
@TheDeveloper
TheDeveloper / lock.lua
Created March 17, 2014 16:33
Set a lock in Redis
--
-- Set a lock
--
-- KEYS[1] - key
-- KEYS[2] - ttl in ms
-- KEYS[3] - lock content
local key = KEYS[1]
local ttl = KEYS[2]
local content = KEYS[3]
@TheDeveloper
TheDeveloper / gist:5404654
Created April 17, 2013 14:18
Usage: remove_submodule submodule_path Where submodule_path is the relative path to your submodule from the current directory. Must be run from the root of a git working copy
# Sweep a git submodule out of the working copy
remove_submodule() {
SMD_PATH=$1
if [ ! -d $SMD_PATH ]; then
echo "$SMD_PATH does not exist"
return 1
fi
git config -f .git/config --remove-section submodule.$SMD_PATH
git config -f .gitmodules --remove-section submodule.$SMD_PATH
# View list of connections and their states
netstat -tan | grep ':80 ' | awk '{print $6}' | sort | uniq -c
# List all connections and timers
ss -rota | less
@TheDeveloper
TheDeveloper / gist:4512889
Created January 11, 2013 18:33
Lua script for use in redis. Deletes all keys matching argument 1. Returns count for total number of keys deleted.
local keys
keys = redis.call('keys', KEYS[1])
local ret=0
local type
for k,v in pairs(keys) do
redis.call('del', v)
ret = ret+1
end
return ret