Skip to content

Instantly share code, notes, and snippets.

@lyoshenka
lyoshenka / RedisLock.class.php
Last active November 6, 2016 15:44
Atomic, distributed locking with PHP and Redis
<?php
class RedisLockFailException extends Exception {}
class RedisLock
{
const DEFAULT_TIMEOUT = 20;
// what to do if we cannot get lock
const FAIL_EXCEPT = 'fail_except'; // throw exception
@lyoshenka
lyoshenka / j.sh
Created October 22, 2016 18:46
The simplest journal / blog / daily writing app ever
#!/bin/bash
set -euo pipefail
EDITOR="vi"
DIR="$HOME/.journal"
mkdir -p "$DIR"
ARG=${1:-}
Verifying that +alexgrin is my blockchain ID. https://onename.com/alexgrin
@lyoshenka
lyoshenka / curl.php
Last active January 19, 2019 18:34
Very simple wrapper around php's curl
<?php
class Curl
{
const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
public static function get($url, $params = [], $options = [])
{
@lyoshenka
lyoshenka / find_extraneous_lines_php.sh
Last active April 4, 2016 18:24
Finds PHP files that don't have `<?php` on the very first line. These files, when included, will cause extraneous spaces in output.
while IFS= read -r -d '' file; do
if [ "$(head -n 1 "$file")" != '<?php' ]; then
echo "$file";
fi;
done < <(find . -name '*.php' -print0)
@lyoshenka
lyoshenka / passwordgen.sh
Last active May 17, 2016 13:26 — forked from linuxboytoo/passwordgen.sh
Bash Random Password Generator
#!/bin/bash
head -n100 /dev/urandom | strings | egrep -o '([a-zA-Z0-9#%&^])' | awk '{ORS=""; print $1}' | head -c 35; echo
@lyoshenka
lyoshenka / mysql_check_slave.sh
Created March 8, 2016 01:01
Check that MySQL slave is running and no errors are present. Optionally send an email if errors are detected.
#!/bin/bash
set -euo pipefail
### CONFIG
EMAIL_ADDR="" # email this address if errors detected
MAX_SECONDS_BEHIND=5 # max seconds that slave can be behind master
var autoCurry = (function () {
var toArray = function toArray(arr, from) {
return Array.prototype.slice.call(arr, from || 0);
},
curry = function curry(fn /* variadic number of args */) {
var args = toArray(arguments, 1);
return function curried() {
return fn.apply(this, args.concat(toArray(arguments)));
@lyoshenka
lyoshenka / stat.sh
Last active March 2, 2016 23:46
Read a bunch of numbers (one per line), print some basic stats
#!/bin/bash
# sample usage:
# cat *.csv | cut -d',' -f3 | ./stat.sh
set -euo pipefail
[ -n "${1:-}" -a -f "${1:-}" ] && INPUT="$1" || INPUT="-"
cat $INPUT | sort -n | awk '
@lyoshenka
lyoshenka / instaload.sh
Created February 24, 2016 02:40
Instaload a MySQL database. 1) have a fresh db in reserve, 2) rename fresh db to your current db, 3) reload fresh db in background
#!/bin/bash
set -euo pipefail
#set -x # for debugging
if [ -z "${2:-}" ]; then
echo "Usage: $0 PASSWORD DB [reload] [NOTIFY-EMAIL]"
echo "Check your bash history?"
exit 1
fi