Skip to content

Instantly share code, notes, and snippets.

View mgreenly's full-sized avatar

Michael Greenly mgreenly

View GitHub Profile
require 'pp'
Rules = [
{
is: { state: 'created', status: 'pending', provider: 'aws' },
in: 'taggable',
to: { state: 'tagged' },
as: { state: 'tagging' },
on: 'housekeeper'
},
require 'concurrent'
MAX_THREADS = 20
class Job
def step
sleep 3 # simulates performing the next task in the jobs workflow
end
end
#
# The key thing to understand about this is that an `ensure` section is not guarnteed to run!
#
# If code executing is in the main thread of the process and a Interrupt, caused by a SIGINT or SIGTERM, occurs
# `ensure` blocks in that thread will not be executed. This is because Interrupt is an asynchrnous event and
# Ruby has no way to return to the point in execution it left when the signal happened.
#
# But, because Interrupts are only processed in the main thread of a process, moving all the actual code of an
# application into a child thread fixes this problem.
#
# inherit from Enumerator
class Pager1 < Enumerator
def initialize(url, page_size: 100, max_pages: Float::INFINITY, first_page: 0)
@url = url
super() do |receiver|
page = first_page
pages = []
while page < (first_page + max_pages)
get(page, page_size).each do |row|
receiver.yield row
@mgreenly
mgreenly / tmux.sh
Last active November 17, 2019 17:23
#!/bin/sh
tmux has-session -t notes > /dev/null 2>&1
if [ $? != 0 ]; then
echo 'starting session: notes'
tmux new-session -s notes -d
tmux rename-window -t notes notes
tmux send-keys -t notes "vim $DROPBOX_DIR/documents/notes.txt" C-M
fi
require 'logger'
# This logger will notice the log has been rotated and reopen it.
# The logrotate options must include 'nocreate'.
class RotatableLogger < Logger
def add(*args)
reopen if @logdev.filename && !File.exist?(@logdev.filename)
super
end
end
Process.setproctitle("foo-parent")
ppid = Process.pid
fork do
exit if fork # You can only setsid if you're in a child process so first we fork
Process.setsid # and exit the parent then setsid in the child. Now that we have a
exit if fork # new session create a child process in it and exit the parent again.
Dir.chdir("/") # Finally, change directory to one that can't be deleted or moved.
Process.setproctitle("foo-child")
@mgreenly
mgreenly / puma.rb
Last active October 22, 2019 03:19
require 'rack'
require 'rack/handler/puma'
require 'oj'
class Stats
def initialize
@stats = {}
@lock = Mutex.new
end
class Channel < SizedQueue
def initialize
super(1)
end
end
channel = Channel.new
Thread.new(channel) do |ch|
val = rand(3.0) + 1.0
# require 'net/https'
require 'excon'
require 'fileutils'
require 'uri'
require 'byebug'
class HttpClient
SLASH = "\u2215"
attr_reader :connection, :host, :opts