Skip to content

Instantly share code, notes, and snippets.

require 'socket'
def log(s)
puts "#{Time.now.strftime('%H:%M:%S')}: #{s}"
end
server = TCPServer.new('', 1337)
loop do
socket = server.accept
class Waiter
def initialize
@m, @cv = Mutex.new, ConditionVariable.new
@signalled = false
end
def wait
@m.synchronize{ @cv.wait(@m) until @signalled; @signalled = false }
end
def signal
@m.synchronize{ @signalled = true; @cv.signal }
require 'thread'
class WorkerPool
def initialize
@num = 10
@jobs = Queue.new
@results = Queue.new
@stopped = false
@toretore
toretore / now.rb
Last active January 9, 2016 17:00 — forked from VelizarHristov/before.rb
PIPELINE = %w[booked collected ironed delivered]
def state
PIPELINE[@pipeline_position]
end
def advance
@pipeline_position += 1
end
require 'socket'
server1 = TCPServer.new('0.0.0.0', 1337)
server2 = TCPServer.new('0.0.0.0', 1338)
servers = [server1, server2]
clients = []
loop do
readables, * = IO.select(servers+clients)
@toretore
toretore / s.rb
Last active January 3, 2016 19:38
require 'socket'
class Page
attr_reader :body, :method, :path
def initialize(m, p, b)
@body, @method, @path = b, m, p
end
end
class Server
@toretore
toretore / HOWTO.md
Last active December 26, 2015 03:09

Here's a quick introduction to how to get the best help in #rubyonrails (or any other programming medium). There are competent people who want to help you, but if you follow a few pointers you will make it much easier for them to do so and will get answers to your question much faster.

1. Show us your code

This is the most critical part. You absolutely must show us the code you're having problems with. It is incredibly important that you show us the relevant code. Trying to figure out a programming problem without code is like trying to fix a car without a car.

Use Gist to paste your code. Do not paste your code directly in the channel. Do not use Pastebin. Read this handy guide to learn how to use Gist properly.

Again, just to make sure you understand how incredibly important it is, you must show us your code, no way around it.

@toretore
toretore / README.md
Last active December 19, 2015 10:49
Rails 0MQ logger

These files work together to extract useful data from a Rails application and publish it through a ZeroMQ PUSH socket as JSON.

The code in application_controller.rb will automatically log every request and exception, but you're free to log other things. If you're inside a controller, you can use log_message(atts) or emit_event(atts). These controller methods simply delegate to the global object $ZMQ_LOGGER, which responds to log(atts) and emit(atts). They add the data.http.request-id key if not already present.

The difference between logging and emitting is somewhat blurry, but I like to think of log messages as something that will be saved somewhere and emitted events as ephemeral.

All attributes are expected to use strings as keys. If you use symbols, you may experience issues.

You may use these files as you wish, with one exception: You may not package them into a reusable library and publish it. This is not easily reusable code, and any attempt to make it into a pre-packaged library will result

@toretore
toretore / foo.sh
Last active December 18, 2015 04:18
Installing ruby-build, ruby, unicorn, bundling and running a rails app on a server
#sudo as necessary
#First, install ruby-build to /opt/ruby-build
git clone git://github.com/sstephenson/ruby-build.git /opt/ruby-build
#it's a good idea to make sure you have these installed before compiling ruby
#apt-get install libssl-dev libreadline6-dev zlib1g-dev
#All ruby installations/versions are going to live in /opt/rubies
mkdir /opt/rubies
rails_env = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'production'
worker_processes (rails_env == 'production' ? 5 : 3)
preload_app true
timeout 30
listen 20000