Skip to content

Instantly share code, notes, and snippets.

View kwilczynski's full-sized avatar

Krzysztof Wilczyński kwilczynski

  • Yokohama, Japan
  • 22:49 (UTC +09:00)
View GitHub Profile
@kwilczynski
kwilczynski / gist:1483690
Created December 16, 2011 00:20
test.sh
#!/bin/bash
echo $(id -nu) $(date -R) $$ "$@" | tee -a /tmp/test.log
exit 0
class Callback
def initialize(sleep_time=5)
@sleep = sleep_time
end
def start
sleep(@sleep)
@on_finish.call if defined?(@on_finish)
end
module Kernel
class << self
def retry_on_exception(exception, retry_count=1, throw=true, &block)
raise ArgumentError, 'no block given' unless block_given?
retry_attempt = 0
begin
block.call
rescue exception => e
@kwilczynski
kwilczynski / dump.rb
Created January 16, 2012 16:56
Variable content display for Puppet ...
#
# dump.rb
#
# Copyright 2011 Puppet Labs Inc.
# Copyright 2011 Krzysztof Wilczynski
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
irb(main):008:0> kwilczynski@desktop:~$ irb
irb(main):001:0> value = %w(this is a test)
=> ["this", "is", "a", "test"]
irb(main):002:0> [*value].join ','
=> "this,is,a,test"
irb(main):003:0> value = 'this is a test'
=> "this is a test"
irb(main):004:0> [*value].join ','
=> "this is a test"
irb(main):005:0> [*value]
class Hash
def deep_merge!(other)
other.each_pair do |k,v|
if self[k].is_a?(Hash) and other[k].is_a?(Hash)
self[k].deep_merge!(other[k])
else
self[k] = other[k]
end
end
end
@kwilczynski
kwilczynski / eventmachine-fork-example.rb
Created January 25, 2012 21:29
EventMachine with fork and children ...
require 'rubygems'
require 'eventmachine'
children = []
Signal.trap('SIGINT') do
EventMachine.next_tick { EventMachine.stop_event_loop }
end
Signal.trap('EXIT') do
@kwilczynski
kwilczynski / eventmachine-fork-example.rb
Created January 25, 2012 22:31
EventMachine with fork and children ... Mark II
require 'rubygems'
require 'eventmachine'
require 'socket'
SOCKET = '/tmp/test.socket'
module Connection
def receive_data(data)
puts "\t#{data}"
@kwilczynski
kwilczynski / em-expose-queues.rb
Created January 27, 2012 03:18
Accessing EventMachine internal queues -- when working with defer ...
module EventMachine
def self.threadqueue
@threadqueue
end
def self.resultqueue
@resultqueue
end
end
@kwilczynski
kwilczynski / eventmachine-fork-example.rb
Created January 28, 2012 00:36
EventMachine with fork and children ... Mark III
require 'rubygems'
require 'eventmachine'
children = []
Signal.trap('SIGINT') do
EventMachine.next_tick { EventMachine.stop_event_loop }
end
Signal.trap('EXIT') do