1. Background Jobs
com BackgrounDRb
da série "Palestras na Softa"
blog.softa.com.br
Recomende: workingwithrails.com/person/9354
2. BackgrounDRb
BackgrounDRb é um agendador e servidor de tarefas em
Ruby.
Seu objetivo principal é ser utilizado com aplicações Ruby on
Rails - plugin - para desafogar tarefas de longa duração.
É feito com PACKET e não DRb. Mas pra entender o conceito,
vamos lá...
3. DRb
Isso é um "tipo" de digressão.
DRb é o Distributed Ruby.
Sim, um CORBA simplificado do Ruby.
Serialização de objetos sobre TCP.
DRb em Ruby é Standard Library.
http://ruby-doc.org/stdlib/libdoc/drb/rdoc/index.html
5. Simple (server)
require 'drb'
class TestServer
def doit
"Hello, Distributed World"
end
end
server = TestServer.new
DRb.start_service('druby://localhost:9000', server)
DRb.thread.join
16. Conceitos-chave
Server
Um server que recebe as chamadas. Deve ser startado uma vez junto com o web
server.
MiddleMan
Classe disponível (Factory) no Rails para chamar as tarefas dos workers em
background, programar execuções ou busca informação sobre as tarefas.
Worker
É uma classes que define as tarefas possíveis de se utilizar. Cada worker inicia um
processo próprio ao iniciar o server.
Job
É a execução de um método de um worker.
17. Conceitos-chave
cache
Hash com informações sobre a execução de um job.
job_key
Variável disponível no Worker para referenciar unicamente um Job.
assync_*
Executa o método * em background agora.
enq_*
Executa o método * em background no futuro.
18. Rake
rake -T backgroundrb
# Generate a migration for the backgroundrb queue table.
rake backgroundrb:queue_migration
# Drops and recreate backgroundrb queue table
rake backgroundrb:redo_queue
# Remove backgroundrb from your rails application
rake backgroundrb:remove
# Setup backgroundrb in your rails application
rake backgroundrb:setup
# update backgroundrb config files from your rails application
rake backgroundrb:update
# Generate documentation for the backgroundrb plugin
rake doc:plugins:backgroundrb
19. Gerador
script/generate worker WorkerName [options]
Rails Info:
-v, --version Show the Rails version number and quit.
-h, --help Show this help message and quit.
General Options:
-p, --pretend Run but do not make any changes.
-f, --force Overwrite files that already exist.
-s, --skip Skip files that already exist.
-q, --quiet Suppress normal output.
-t, --backtrace Debugging: show backtrace on errors.
-c, --svn Modify files with subversion. (Note: svn must be in path)
-g, --git Modify files with git. (Note: git must be in path)
Description:
The worker generator creates stubs for a new BackgrounDRb worker.
The generator takes a worker name as its argument. The worker name may be
given in CamelCase or under_score and should not be suffixed with 'Worker'.
The generator creates a worker class in lib/workers and a test suite in
test/unit.
Example:
./script/generate worker Tail
This will create an Tail worker:
Model: lib/workers/tail_worker.rb
Test: test/unit/tail_worker_test.rb
30. Advanced stuff
class ConnectorWorker < BackgrounDRb::MetaWorker
set_worker_name :connector_worker
def chat
require 'drb'
DRb.start_service()
@chat = DRbObject.new(nil, 'druby://localhost:9000')
@chat.login("Bot")
add_periodic_timer(5) { talk }
end
def talk
@chat.send_message("Bot", "i'm talking...")
end
end
31. Advanced stuff
class TimeServer
def receive_data(p_data)
end
def post_init
add_periodic_timer(2) { say_hello_world }
end
def connection_completed
end
def say_hello_world
p "***************** : invoking hello world #{Time.now}"
send_data("Hello Worldn")
end
end
class ServerWorker < BackgrounDRb::MetaWorker
set_worker_name :server_worker
def create(args = nil)
start_server("0.0.0.0",11009,TimeServer) do |client_connection|
client_connection.say_hello_world
end
end
end