Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Event Collaboration for the Rubyist

Event Collaboration for the Rubyist

Luismi Cavallé

December 29, 2011

More Decks by Luismi Cavallé

Other Decks in Programming


  1. Event Collaboration for the Rubyist by Luismi Cavallé

  2. twitter.com/cavalle github.com/cavalle

  3. None
  4. Event Collaboration?

  5. Multiple components work together by communicating with each other by

    sending events when their internal state changes. — Martin Fowler http://tinyurl.com/eaaDev
  6. Given one task is pending When I complete it Then

    my boss is noti ed Given I completed one task in 1 day And I completed other task in 2 days When I see the performance report Then my performance is 1.5 tasks/day
  7. class Task def mark_as_completed! update_attributes! :completed => true notify_boss update_progress_report

    end def notify_boss # ... end def update_progress_report # ... end end
  8. The Single Responsibility Principle “An object should have only a

    single responsibility.” — Uncle Bob http://tinyurl.com/OOPrinciples
  9. class Task def mark_as_completed! update_attributes! :completed => true Notifier.notify_task_completed self

    ProgressReport.update_performance self end end
  10. The Open Closed Principle “[An object] should be open for

    extension, but closed for modi cation.” — Uncle Bob http://tinyurl.com/OOPrinciples
  11. class Task def mark_as_completed! update_attributes! :completed => true publish_event :task_completed,

    :task_name => name, :by => user.name end # ... end
  12. class Notifier on :task_completed do |event| Notifier.new(:email => find_boss_email(event.by), :subject

    => "Task #{event.task_name} completed").send! end # ... end
  13. module PerformanceStats on :task_created do |event| start_times[event.task_name] = Time.now end

    on :task_completed do |event| completion_time_sum += Time.now - start_times.delete(event.task_name) completion_time_count += 1 end def self.average_performance completion_time_sum / completion_time_count end # ... end
  14. Task Noti er Performance Stats

  15. Loose coupling Maintainability Scalability

  16. Un momento de relax…

  17. Eventwire Event Collaboration for the Masses http://github.com/cavalle/eventwire

  18. Interface Middleware Adapters Your App Workers RabbitMQ Redis MongoDB ZeroMQ

  19. Interface: Publisher class MyPublisher include Eventwire::Publisher def my_method publish_event :task_completed,

    :task_name => 'Name of the task', :username => 'Username' end end
  20. Interface: Subscriber class MySubscriber include Eventwire::Subscriber on :task_completed do |data|

    puts "Task completed #{data.task_name}" end end
  21. Adapters AMQP Redis MongoDB ZeroMQ Bunny InProcess Syslog

  22. Middleware MsgPack ErrorHandler Logger DataObjects JSON BSON

  23. Workers $ rake eventwire:work $ rake environment eventwire:work

  24. Con guration Eventwire.configure do |config| config.adapter = :AMQP config.logger =

    Rails.logger config.middleware << MyCustomMiddleware end
  25. P1 P2 S1 S3 S2 S2 S2 Pub / Sub

    Bus Sub. Balancing Messaging Patterns
  26. None
  27. Current status Not released to Rubygems yet Some adapters are

    very experimental Next milestone: Production at BeBanjo
  28. Future Measure & Improve Adapters New Adapters Documentation Front-end ala

  29. Some tradeoffs Another layer of abstraction Lost messages Messages out

    of order
  30. References http://github.com/cavalle/eventwire http://tinyurl.com/eaaDev http://rubyamqp.info/

  31. ¡Gracias! twitter.com/cavalle github.com/cavalle speakerdeck.com/u/cavalle