Skip to content

Instantly share code, notes, and snippets.

View kvz's full-sized avatar
🚀

Kevin van Zonneveld kvz

🚀
View GitHub Profile
@kvz
kvz / exponential_backoff.php
Last active December 20, 2015 06:58
Tries your function several times with a bigger & bigger interval, until it does not throw an exception. Useful if you want to gracefully handle external services not being available, without actually building retries into your code. Works via Hollywood principle: Don't call us, we'll call you.
<?php
Class SomeClass() {
public function retry ($cb, $intervals = array(2, 4, 8, 16)) {
while ($intervals) {
if (!is_a($results = $cb(), 'Exception')) {
// good results, return them
return $results;
}
// failed, sleep for a larger & larger interval
$backoff = array_shift($intervals);
<?php
// In PHP 5.4 you can pass JSON_UNESCAPED_UNICODE and JSON_UNESCAPED_SLASHES to json_encode:
$input = array('file' => '/\intro_cropsic – .m4v');
$paramsString = json_encode($input, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
// And that will encode arrays with unicode characters more compatible with JavaScript.
// (e.g. it will produce the same sha1 if you phpjs.utf8_encode paramString in JavaScript )
// On PHP 5.3 and lower, you may want to try
function json_encode_noescape_slashes_unicode ($arr) {
@kvz
kvz / elasticsearch.init.erb
Last active December 22, 2015 00:19
Please god no chef
# Executing ruby in a template in ruby
version=$(curl -s 'http://localhost:9200' | ruby -rubygems -e 'require "json"; print JSON.parse(STDIN.read)["version"]["number"]')
echo -e "\033[1;37;46melasticsearch $version running with PID $pid\033[0m"
ubuntu@beth:/tmp$ time dig @4.2.2.2 +time=3 +tries=1 +short tmp.transloadit.com.s3.amazonaws.com
s3-1-w.amazonaws.com.
205.251.243.73
real 0m0.079s
user 0m0.004s
sys 0m0.000s
ubuntu@beth:/tmp$ time dig @8.8.8.8 +time=3 +tries=1 +short tmp.transloadit.com.s3.amazonaws.com
;; connection timed out; no servers could be reached
#!/usr/bin/env bash
#
# A Git pre-commit hook that checks for syntax errors
# for: Ruby, JavaScript, Python, Bash, and (Cake)PHP
# based on the extensions of staged files in Git.
# Can be 'installed globally' as of Git 1.7.1 using init.templatedir
#
# Copyright 2013, kvz (http://twitter.com/kvz)
#
# Some common caveats have been taken care of:
@kvz
kvz / strace-identify-list-delegate.sh
Last active August 29, 2015 13:57
When I run `identify -list delegate` it shows my delegates, but then exits with "Aborted (core dumped)"
# I statically compiled Imagemagick.
# Converting works well so far.
# But when I run `identify -list delegate` it shows my delegates, but then exits with "Aborted (core dumped)".
# What could be wrong here?
$ strace -f identify -list delegate
execve("./target/bin/identify", ["identify", "-list", "delegate"], [/* 21 vars */]) = 0
brk(0) = 0x34e2000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f8452cd8000
"steps": {
"ipad": {
"robot" : "/video/encode",
"use" : [ ":original" ],
"preset": "ipad"
},
"webm": {
"robot" : "/video/encode",
"use" : [ ":original" ],
"preset": "webm"
cd /tmp
wget -nc https://www.dropbox.com/s/04u5xws6fkvbkgb/Shred_Those_Pounds.mp4?dl=1 -O Shred_Those_Pounds.mp4
echo ""
echo ""
echo "--> Failure with '-sn -dn'"
ffmpeg-v2.2.3 \
-i ./Shred_Those_Pounds.mp4 \
-codec:a libfdk_aac \
#!/usr/bin/env bash
# Transloadit API2. Copyright (c) 2015, Transloadit Ltd.
#
# This file
# - Reproduces the issue where resizing a certain jpg breaks
# the image on OSX & iOS, wheras the original dispays fine.
#
# Reported first by Richard Taylor in http://support.transloadit.com/discussions/questions/96479-image-conversion-problem
#
# Typically called as `./debug.sh [target.jpg]`
@kvz
kvz / gist:a566ba3be4a3faf7d6e6
Last active May 10, 2016 11:39
Proposal for (partial) template includes
{
include_template: "a", // <-- full template, used as a base
is_video: {
use: ":original",
robot: "/file/filter",
accepts: [
[
"${file.mime}",
"regex",
"video"