Skip to content

Instantly share code, notes, and snippets.

from __future__ import absolute_import
import signal
import gevent
import gevent.pool
from rq import Worker
from rq.timeouts import BaseDeathPenalty, JobTimeoutException
from rq.worker import StopRequested, green, blue
from rq.exceptions import DequeueTimeout
@jhorman
jhorman / redis_semaphore.py
Last active April 13, 2022 12:39
Redis semaphore implemented in Python via zsets. Lock expiration is implemented by only scanning the zset for items within a time range.
from __future__ import absolute_import
from time import time, sleep
import uuid
class RedisSemaphore(object):
"""
Redis base semaphore. Supports timeouts of semaphore locks.
"""
@jhorman
jhorman / twisted_sleep.py
Created March 29, 2011 02:38
Simple non-blocking sleep in twisted.
def sleep(secs):
d = Deferred()
reactor.callLater(secs, d.callback, None)
return d
# Git prompt
export GIT_PS1_SHOWDIRTYSTATE=1 GIT_PS1_SHOWUNTRACKEDFILES=1 GIT_PS1_SHOWSTASHSTATE=1
export PS1='\w\[\033[01;33m\]$(__git_ps1)\[\033[00m\] \$ '
# less syntax highlighting
# brew install source-highlight
export LESSOPEN="| src-hilite-lesspipe.sh %s"
export LESS=' -R '
///////////////////////////
// JAVASCRIPT RULE ENGINE
///////////////////////////
/*global define*/
define([
'lodash.noconflict'
], function (_) {
'use strict';
function Rules(options) {
@jhorman
jhorman / git_find.sh
Created October 24, 2012 19:37
git log find by content
git config --global alias.find "!gf() { git log -G \"\$1\" --pretty=\"format:%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset\" --abbrev-commit --date=relative; }; gf"
@jhorman
jhorman / springpad_oauth.sh
Created December 14, 2011 18:17
Springpad OAuth Help
# http://mojodna.net/2009/08/21/exploring-oauth-protected-apis.html
easy_install oauth-proxy
sudo gem install oauth
oauth --consumer-key XXXXX --consumer-secret XXXXX --access-token-url http://springpadit.com/api/oauth-access-token --authorize-url http://springpadit.com/api/oauth-authorize --request-token-url http://springpadit.com/api/oauth-request-token authorize
oauth-proxy --consumer-key XXXXX --consumer-secret XXXXX -p 8001
@jhorman
jhorman / reddit_userstyle.css
Created December 8, 2011 05:34
Reddit to Hacker News
body { font: normal x-small verdana !important; }
.link { margin-bottom: 4px !important; }
.link .title { font: normal 10pt verdana !important; }
.link .flat-list { padding: 0px !important; }
.tagline { font: normal 7pt verdana !important; display:none !important; }
.link .rank { display: none; }
.sitetable { padding: 10px !important; margin-top: 30px !important; margin-left: 100px !important; margin-right: 100px !important; background-color: #F6F6EF }
.side { display: none; }
a:link { color: black !important; text-decoration: none !important;}
a:visited {color: #828282 !important;}
@jhorman
jhorman / iptables.sh
Created April 22, 2011 18:29
iptables stuff
# Reject traffic out to a specific IP
iptables -o eth0 -I OUTPUT -d 10.207.47.148 -j REJECT
@jhorman
jhorman / twisted_timing.py
Created March 29, 2011 02:42
Times a method that may or may not return a deferred
def time_method(stat_name):
"""
Attaches a timeout to the deferred returned by the wrapped function.
After timeout seconds the deferred is canceled and an exception raised.
"""
def attach_timing(method):
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
start = time()