Skip to content

Instantly share code, notes, and snippets.

View gshutler's full-sized avatar
📅
👨‍💻

Garry Shutler gshutler

📅
👨‍💻
View GitHub Profile
@gshutler
gshutler / consumption.rb
Last active December 19, 2015 22:59
Prioritised queue consumption with multiple queues
queues = []
queues << queue_consumer("high")
queues << queue_consumer("normal")
def next_message
# look for a message from each queue in priority order
queues.each do |queue|
found, message = queue.non_blocking_dequeue
return [found, message] if found
end
@gshutler
gshutler / log_listener.rb
Created April 13, 2013 18:02
Script hacked from https://github.com/ajfaraday/Text-to-music for listening to log files. A day of logs can be listened to in under a minute.
#
# Andrew James Faraday - May 2012
#
# This script is to allow direct manual control of the text-to-music algorithm.
# After a simple check of speed (1 divided by numbers 1 to 10 = number of seconds between characters) the user is prompted to provide textual input which is then sonified.
# This is recommended as a first experience of the text-to-music system, so new users can see the correlation between their use of text and the resulting sound.
# press ctrl+c to exit the script
#
#
@gshutler
gshutler / example.rb
Created January 10, 2013 21:27
Example of nested diagnostic contexts with Hatchet
class Example
include Hatchet
def run
log.info { "Hello" }
# => "Example - Hello"
log.ndc.scope(:nest) do
log.info { "Hi" }
# => "Example nest - Hi"
module Background
include Hatchet
def self.execute(user)
log.info "Entering execute(user:#{user})"
do_something_dangerous = false
if user.privileged?
log.info "#{user} is privileged"
@gshutler
gshutler / 1_readme.md
Last active December 10, 2015 11:48
Exploration of Nested Diagnostics Context (NDC) for Hatchet

Adding NDC to Hatchet

I like the idea of adding NDCs to Hatchet.

Most of the hard work could be contained within some middleware for Rack-based applications. Non-Rack applications would be responsible for calling context.clear before/after doing all the work for a context so that it was clear before new work commenced.

I'm thinking the API of the logger (accessed via the log and logger mixin methods) would be extended to include:

# Public: Returns a handle to the current logging context which
@gshutler
gshutler / 1_before.rb
Created December 30, 2012 12:21
Example of a module before and after logging
module Background
def self.execute(user)
if user.privileged? or transient_condition?(user)
something_dangerous(user)
end
end
def self.transient_condition?(user)
user.first_name.start_with?(random_character)
# Before
- some_pairs.each do |value, text|
%option{value: value}= text
# After
- some_pairs.each do |value, text|
<option value='#{value}'>#{h text}</option>
User = Struct.new(:name, :male?) do
def sex
self[:male?] ? :male : :female
end
end
garry = User.new("Garry", true)
puts garry.name # => Garry
puts garry.sex # => male
set transaction isolation level read uncommitted
select
c.[Id],
c.[Name],
c.[SentOn],
count(s.Id) as sent,
sum(s.[Bounced]) as bounced,
sum(s.[Complained]) as complained,
sum(s.[Opened]) as opened,
@gshutler
gshutler / hatchet_errors.rb
Created November 2, 2012 09:28
Exploring Hatchet API design options for passing errors through
# Exploring API design options as it's hard to think of something that's
# aesthetically pleasing that also avoids needless string interpolation.
# Multiple return arguments.
#
# Syntax looks a little weird with the multiple brace types.
log.error { ["Some message", e] }
log.error {[ "Some message", e ]}
# Passing the exception as an optional parameter.