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

Background jobs with realtime results – RailsClub'Moscow 2012

Background jobs with realtime results – RailsClub'Moscow 2012

Sergey Nartimov

September 15, 2012
Tweet

More Decks by Sergey Nartimov

Other Decks in Programming

Transcript

  1. О себе • Rails, Rubinius, Elixir contributor • Señor software

    engineer at Brainspec • Консалтинг, обучение, разработка Сергей Нартымов Brainspec https://github.com/lest twitter: @just_lest
  2. DelayedJob class Device def deliver # long running method end

    handle_asynchronously :deliver end device = Device.new device.deliver
  3. DelayedJob class LongTasks def send_mailer # Some other code end

    handle_asynchronously :send_mailer, :priority => 20 def in_the_future # Some other code end # 5.minutes.from_now will be evaluated when in_the_future is called handle_asynchronously :in_the_future, :run_at => Proc.new { 5.minutes.from_now } end
  4. Resque • https://github.com/defunkt/resque • Разработан и используется в Github •

    Задания хранятся в Redis • Для сериализации используется JSON
  5. Resque class Archive @queue = :file_serve def self.perform(repo_id, branch =

    'master') repo = Repository.find(repo_id) repo.create_archive(branch) end end
  6. Sidekiq • https://github.com/mperham/sidekiq • Выполняет задания в потоках • Используется

    Celluloid • Задания хранятся в Redis • Сериализует данные в JSON
  7. Sidekiq # app/workers/hard_worker.rb class HardWorker include Sidekiq::Worker def perform(name, count)

    puts 'Doing hard work' end end HardWorker.perform_async('bob', 5)
  8. Sidekiq class ImportantWorker include Sidekiq::Worker sidekiq_options :queue => :critical def

    perform(*important_args) puts "Doing critical work" end end
  9. Общие советы • Храните в очереди идентификаторы, а не состояние

    • Используйте after_commit в ActiveRecord для добавления задания в очередь • Избегайте блокировок
  10. Pusher <script src="http://js.pusher.com/1.12/pusher.min.js"></script> var channel = pusher.subscribe('my-channel'); channel.bind('my-event', function(data) {

    alert('Received my-event with message: ' + data.message); }); Pusher['my-channel'].trigger('my-event', {'message' => 'hello world'})
  11. Faye • http://faye.jcoglan.com/ • Open source • Серверная часть на

    любой вкус – Ruby/EventMachine и Node.JS • Можно масштабировать
  12. Faye • http://faye.jcoglan.com/ • Open source • Серверная часть на

    любой вкус – Ruby/EventMachine и Node.JS • Можно масштабировать • Более тесная итеграция с приложением
  13. Faye require 'faye' faye = Faye::RackAdapter.new(:mount => '/faye') faye.listen(9292) var

    Faye = require('faye'), server = new Faye.NodeAdapter({mount: '/'}); server.listen(9292);