This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rules = { | |
enter: [ 'created' ], | |
exit: [ 'completed' ], | |
uncancelable: %w[ completing completed ], | |
tasks: [ | |
{ | |
name: 'tag', | |
desc: 'taggable', | |
transitions: [ | |
{ from: { state: 'tagging', :status: 'pending' }, to: { state: 'tagged' } }, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pp' | |
Rules = [ | |
{ | |
is: { state: 'created', status: 'pending', provider: 'aws' }, | |
in: 'taggable', | |
to: { state: 'tagged' }, | |
as: { state: 'tagging' }, | |
on: 'housekeeper' | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'concurrent' | |
MAX_THREADS = 20 | |
class Job | |
def step | |
sleep 3 # simulates performing the next task in the jobs workflow | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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. | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rack' | |
require 'rack/handler/puma' | |
require 'oj' | |
class Stats | |
def initialize | |
@stats = {} | |
@lock = Mutex.new | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Channel < SizedQueue | |
def initialize | |
super(1) | |
end | |
end | |
channel = Channel.new | |
Thread.new(channel) do |ch| | |
val = rand(3.0) + 1.0 |