Class: Backnob::Client
- Inherits:
-
Object
- Object
- Backnob::Client
- Defined in:
- lib/backnob/client.rb
Instance Method Summary collapse
-
#add_worker(file) ⇒ Object
Add a worker file or directory.
-
#available? ⇒ Boolean
Test to see if the server is available.
-
#create_worker(name, options = {}) ⇒ Object
Create and start a new worker.
-
#initialize(uri = nil) ⇒ Client
constructor
Create a new client to connect to the server on the passed uri.
-
#results(key, hk = nil) ⇒ Object
Retrieve as results hash for a given worker key.
-
#server ⇒ Object
Return a DRb object reference to the server.
Constructor Details
#initialize(uri = nil) ⇒ Client
Create a new client to connect to the server on the passed uri. The uri defaults to druby://127.0.0.1:6444
10 11 12 |
# File 'lib/backnob/client.rb', line 10 def initialize(uri = nil) @uri = uri || SERVER_URI end |
Instance Method Details
#add_worker(file) ⇒ Object
Add a worker file or directory. For example:
client.add_worker('workers/import_worker.rb')
Will add just the import worker file, whereas:
client.add_worker('workers')
Will add all worker files in the directory workers.
30 31 32 33 34 |
# File 'lib/backnob/client.rb', line 30 def add_worker(file) server do |s| s.add_file(Pathname.new(file).realpath.to_s) end end |
#available? ⇒ Boolean
Test to see if the server is available
37 38 39 40 41 42 43 44 45 |
# File 'lib/backnob/client.rb', line 37 def available? begin server do |s| s.ping end rescue return false end end |
#create_worker(name, options = {}) ⇒ Object
Create and start a new worker. Pass the short name of the worker. For example if you have ImportWorker use ‘import’
The options hash is passed to the worker. The worker key is returned.
client.create_worker('import', {:file => '/tmp/import.csv'})
19 20 21 22 23 |
# File 'lib/backnob/client.rb', line 19 def create_worker(name, = {}) server do |s| s.create_worker(name, ) end end |
#results(key, hk = nil) ⇒ Object
Retrieve as results hash for a given worker key. Can also take a key to retrieve just the results on a given key.
49 50 51 52 53 |
# File 'lib/backnob/client.rb', line 49 def results(key, hk = nil) server do |s| s.results(key, hk) end end |
#server ⇒ Object
Return a DRb object reference to the server. If passed a block this will open the connection, yield then close the connection.
client.server do |server|
server.ping
end
This will return true
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/backnob/client.rb', line 64 def server unless block_given? DRb.start_service return DRbObject.new_with_uri(@uri) else begin DRb.start_service s = DRbObject.new_with_uri(@uri) ret = yield(s) ensure DRb.stop_service end ret end end |