Skip to content

Instantly share code, notes, and snippets.

View mgreenly's full-sized avatar

Michael Greenly mgreenly

View GitHub Profile
# require 'net/https'
require 'excon'
require 'fileutils'
require 'uri'
require 'byebug'
class HttpClient
SLASH = "\u2215"
attr_reader :connection, :host, :opts
@mgreenly
mgreenly / gc.rb
Last active August 27, 2019 02:08
#!/bin/usr/env ruby
#
require 'pp'
class App
def run
catch :done do
Signal.trap "INT" do
throw :done
@mgreenly
mgreenly / down.sh
Last active August 25, 2019 19:37
tmux background processes
#!/bin/bash
function terminate {
local name=$1
tmux has-session -t $name > /dev/null 2>&1
if [ $? == 0 ]; then
echo "terminating $name"
tmux kill-session -t $name
fi
require 'pp'
class Promise
def initialize(*args, &block)
@thread = Thread.new(args) do
@value = yield *args
end
end
def value
require 'logger'
require 'monitor'
# Traditional log rotation, via /usr/sbin/logrotate, just renames
# the log file. It's the responsibiilty of the application to notice
# this and re-open it's log file at the original path.
#
# If the application fails to do this it will continue to write to
# the original, now renamed, log file.
#
#!/usr/bin/env ruby
#
# This works but channel is not intended to be used across threads so this needs
# to be re-worked. Instead rewrite it so that the subscribe loop is done manually
# inside that loop it will check for a message and queue it if one exist. check
# for a completed job and eithe ack/nack it. The entire rabbit connection
# and conversation will be inside that one thread
#
@mgreenly
mgreenly / agent.rb
Last active August 7, 2019 01:42
command loop with unix domain socket
# Demonstrate creating a command loop in a service with
# a unix domain socket. This would be run in a separate
# thread from the main application. Any data it acessess
# from the main app will have to be thread safe.
require "socket"
require 'pp'
PATH = '/tmp/simple.sock' # This is the location of the domain socket used by clients to
# connect with this process
@mgreenly
mgreenly / agent.rb
Last active August 6, 2019 02:18
Demo of Killing a Process Group
boss = fork do # Create a 'boss' process, that will be the parent of workers.
Process.setpgrp # Give 'boss' a new processes-group so that it's different
# than the top level script's process-group
5.times do # Create multiple workers, they share the boss's process group
fork do
done = false # We're not done when we start
Signal.trap('TERM') { done = true } # If worker receives 'TERM' start a graceful shutdown
1.upto(60) do # do some fake work for 60 seconds
break if done # or until we're done
@mgreenly
mgreenly / agent.rb
Last active July 31, 2019 10:56
forking agent for long running jobs
require 'everything'
# read config, setup logging here.
def run(job)
# This will happen in separate process which means all the memory
# it uses will be given back to the system after it's done
end
done = false
require 'optparse'
require 'pp'
task :minitest do
options = {}
OptionParser.new do |opts|
opts.on("-f", "--filename FILENAME", "file to test"){|v| options[:filename]=v}
opts.on("-t", "--testname TESTNAME", "test to test"){|v| options[:testname]=v}
opts.on("-s", "--seed SEED", "seed value"){|v| options[:seed]=v}
opts.parse(ARGV.drop(2))