• Stars
    star
    101
  • Rank 326,344 (Top 7 %)
  • Language
    Ruby
  • Created almost 12 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Message busses with Observable API

tusk

DESCRIPTION:

Tusk is a minimal pub / sub system with multiple observer strategies. Tusk builds upon the Observer API from stdlib in order to provide a mostly consistent API for building cross thread or process pub / sub systems.

Currently, Tusk supports Redis and PostgreSQL as message bus back ends.

FEATURES/PROBLEMS:

  • Send message across processes
  • Supports Redis as a message bus
  • Supports PostgreSQL as a message bus
  • Supports DRb as a message bus

SYNOPSIS:

Here is an in-memory observer example:

require 'observer'

class Timer
  include Observable

  def tick
    changed
    notify_observers
  end
end

class Listener
  def update; puts "got update"; end
end

timer = Timer.new
timer.add_observer Listener.new
loop { timer.tick; sleep 1; }

The down side of this example is that the Listener cannot be in a different process. We can move the Listener to a different process by using the Redis observable mixin and providing a redis connection:

require 'tusk/observable/redis'
require 'redis'

class Timer
  include Tusk::Observable::Redis

  def tick
    changed
    notify_observers
  end

  def connection
    Thread.current[:redis] ||= ::Redis.new
  end
end

class Listener
  def update; puts "got update PID: #{$$}"; end
end

timer = Timer.new

fork {
  timer.add_observer Listener.new
  sleep
}

loop { timer.tick; sleep 1; }

PostgreSQL can also be used as the message bus:

require 'tusk/observable/pg'
require 'pg'

class Timer
  include Tusk::Observable::PG

  def tick
    changed
    notify_observers
  end

  def connection
    Thread.current[:pg] ||= ::PG::Connection.new :dbname => 'postgres'
  end
end

class Listener
  def update; puts "got update PID: #{$$}"; end
end

timer = Timer.new

fork {
  timer.add_observer Listener.new
  sleep
}

loop { timer.tick; sleep 1; }

We can easily integrate Tusk with Active Record. Here is a User model that sends notifications when a user is created:

require 'tusk/observable/pg'
class User < ActiveRecord::Base
  attr_accessible :name

  extend Tusk::Observable::PG

  # After users are created, notify the message bus
  after_create :notify_observers

  # Listeners will use the table name as the bus channel
  def self.channel
    table_name
  end

  private

  def notify_observers
    self.class.changed
    self.class.notify_observers
  end
end

The table name is used as the channel name where objects will listen. Here is a producer script:

require 'user'
loop do
  User.create!(:name => 'testing')
  sleep 1
end

Our consumer adds an observer to the User class:

require 'user'
class UserListener
  def initialize
    super
    @last_id = 0
  end

  def update
    users = User.where('id > ?', @last_id).sort_by(&:id)
    @last_id = users.last.id
    users.each { |u| p "user created: #{u.id}" }
  end
end

User.add_observer UserListener.new
# Put the main thread to sleep
sleep

Whenever a user gets created, our consumer listener will be notified.

REQUIREMENTS:

  • PostgreSQL or Redis

INSTALL:

  • gem install tusk

LICENSE:

(The MIT License)

Copyright (c) 2012 Aaron Patterson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

initial-v

It's a BMW shifter converted to a Bluetooth Keyboard that you use with Vim
C++
959
star
2

asmrepl

A REPL for x86-64 assembly language
Ruby
867
star
3

rails_autolink

The auto_link function from Rails
Ruby
582
star
4

the_metal

A spike for thoughts about Rack 2.0
Ruby
519
star
5

analog-terminal-bell

A bell for your terminal that is analog
OpenSCAD
474
star
6

tenderjit

JIT for Ruby that is written in Ruby
Ruby
416
star
7

fisk

A pure Ruby assembler
Ruby
295
star
8

phuby

phuby wraps PHP in a loving embrace
Ruby
266
star
9

hana

An implementation of JSON Patch and JSON Pointer for Ruby
Ruby
214
star
10

esp8266aq

ESP8266 and Plantower AQ sensor
C++
212
star
11

recma

Pure ruby javascript parser and interpreter.
Ruby
194
star
12

dnssd

Multicast DNS client for ruby! YAY!
Ruby
173
star
13

ruby-glossary

Just a glossary of terms I've found in Ruby source code
172
star
14

enterprise

Make ruby ruby application enter the enterprise with the enterprise gem
Ruby
156
star
15

heap-analyzer

A heap analyzer for MRI that isn't very good.
JavaScript
141
star
16

rexical

rexical is a lexical scanner generator for ruby
Ruby
135
star
17

namecase

Properly case people's names
Ruby
128
star
18

refreshing

A Rails Engine that gives Language Server support to Rails apps
Ruby
112
star
19

uart

Simple serial / UART interface for Ruby
Ruby
100
star
20

heapfrag

Heap visualizer for Ruby
Ruby
97
star
21

nfc

NFC is a ruby wrapper for the Near Field Communication library.
C
90
star
22

rubycommitters.org

The source for rubycommitters.org
Ruby
83
star
23

ds9

Wrapper around nghttp2
C
80
star
24

tinygql

A tiny and experimental GraphQL parser in Ruby
Ruby
78
star
25

fibur

Concurrent execution during Ruby I/O
Ruby
78
star
26

our_pc

Our Procedure Calls
Ruby
71
star
27

magic_scan

My magic card recognition system
Ruby
69
star
28

av_capture

A wrapper around av_capture on OS X
Objective-C
68
star
29

aarch64

Pure Ruby ARM64 Assembler
Ruby
64
star
30

gda

A SQL parser. It wraps libgda
C
64
star
31

taka

Taka is a DOM (core and html) implementation for Ruby
Ruby
63
star
32

minitest-emoji

View your test output as emoji
Ruby
59
star
33

mmap

A wrapper around mmap
C
57
star
34

neversaydie

NeverSayDie will let you rescue from SEGV's and is evil
Ruby
55
star
35

reloader

A demo of live streams on Rails 4
Ruby
50
star
36

HTML5DeckBuilder

an html5 mtg deck builder
JavaScript
49
star
37

sphero

A ruby gem for the sphero ball
Ruby
45
star
38

dot_vim

my dot vim directory
Vim Script
44
star
39

markup_validity

Test for valid markup with test/unit or rspec
Ruby
44
star
40

my_thing

demo of regression test selection
Ruby
42
star
41

defrost

Never let pesky "frozen" objects get in your way again! Use Defrost to remove the frozen state from your objects!
Ruby
41
star
42

gem_survey

a survey script for gems
Ruby
41
star
43

spectacular

View SNMP in your browser
Ruby
38
star
44

zeroconf

Multicast DNS client and server written in pure Ruby
Ruby
30
star
45

leap_motion

Ruby bindings to the Leap Motion C++ library
C++
30
star
46

paddle

Paddle is an rdoc plugin for emitting epub books suitable for use with iBooks
Ruby
29
star
47

horo

RDoc style extracted from Rails and refactored for RDoc 2.5.x
Ruby
26
star
48

earworm

What is that song? Earworm uses libofa and MusicDNS to tell you.
Ruby
26
star
49

streamdeck-ruby-plugin

Minimal Stream Deck plugin written in Ruby
Ruby
24
star
50

raop

RAOP Client is an Airport Express client written in ruby. It allows you to stream music to an Airport Express from Ruby.
Ruby
24
star
51

lolwut

Demo of SSE and reloading with AC::Live
Ruby
23
star
52

purdytest

Colorized output for minitest
Ruby
23
star
53

qrtools

QR Decoder for Ruby. Uses libdecodeqr
C++
22
star
54

dejour

find awesome stuff on the network
Ruby
22
star
55

playpen

A wrapper around the OS X security library
Ruby
21
star
56

geera

A commandline client for JIRA
Ruby
20
star
57

rjson

An JSON parser written with Racc
Ruby
20
star
58

widen

A library for narrowing and widening characters
Ruby
19
star
59

arghhh

I can't figure out SSL, please help!
Ruby
19
star
60

heap-utils

Just random scripts I'm using for Ruby heap info
Ruby
19
star
61

icanhasaudio

I am in ur computar, encodin' and decodin' ur MP3z.
C
19
star
62

housefire

Dumb campfire clone
Ruby
18
star
63

xml_truth

A collection of XML/HTML parser benchmarks for Ruby
Ruby
18
star
64

tldr

Too Long Didn't Run
Ruby
17
star
65

adequatehq.com

Adequate HQ
17
star
66

stree

A wrapper around libstree
C
16
star
67

allocation_sampler

C
16
star
68

wreckster

A Ruby API for Rexster
Ruby
15
star
69

chicken-yaml

libyaml wrapper for chicken scheme
Scheme
15
star
70

ruby-pprof

A Ruby program for parsing gperftools output
Ruby
14
star
71

uchip

Ruby library for interacting with MCP2221 and MCP2221a
Ruby
14
star
72

betabrite

This is a ruby API for the BetaBrite LED sign
Ruby
14
star
73

orient

Binary protocol implementation for Orient DB and Ruby
Ruby
14
star
74

fsmjs

Simulating fsm in JS
14
star
75

imgur2

Quick hack for uploading to imgur
Ruby
13
star
76

roastr

My Christmas roast
R
13
star
77

zomg

An OMG IDL parser and ruby code generator
Ruby
12
star
78

quail

Zero MQ ruby wrapper
Ruby
12
star
79

jit_buffer

General purpose buffer for use with building JITs
Ruby
10
star
80

hacks

Code for accessing CRuby constants and functions as Ruby
Ruby
10
star
81

meow

Meow gives you access to Growl notifications using RubyCocoa
Ruby
10
star
82

awkward

Produce dot files that represent your object graph
Ruby
10
star
83

libxml2

my fork of libxml2
C
10
star
84

mallcop

libssh2 wrapper
Ruby
9
star
85

milton

Ruby
9
star
86

odinflex

Different utilities that I've written
Ruby
9
star
87

remotehash

An OpenDHT client library
Ruby
9
star
88

wank

Ruby
9
star
89

WORF

WORF, the DWARF parser
Ruby
9
star
90

kedama

A wrapper around ming. Create swf files in ruby using ming.
Ruby
9
star
91

rubypan

Ruby
8
star
92

tree_diff

Parse html using different parsers, then show differences between the generated trees
Ruby
8
star
93

hatstone

A minimal Ruby wrapper for Capstone disassembler
C++
8
star
94

e-zine

My E-Zine
7
star
95

rsat

A pure Ruby SAT solver
Ruby
7
star
96

tenderlove.github.com

My awesome website
HTML
7
star
97

myhidapi

A wrapper around hidapi for Ruby
C
7
star
98

compiler

Proof of concept for converting Journey routes to state tables
Ruby
7
star
99

magic

I found some of my old code from 2004 on a USB stick. This code is not for use, it's just for fun
Roff
7
star
100

hearts

I love hearts!
Ruby
6
star