Module: LiveResource

Defined in:
lib/live_resource.rb,
lib/live_resource/finders.rb,
lib/live_resource/methods.rb,
lib/live_resource/resource.rb,
lib/live_resource/attributes.rb,
lib/live_resource/log_helper.rb,
lib/live_resource/declarations.rb,
lib/live_resource/redis_client.rb,
lib/live_resource/methods/token.rb,
lib/live_resource/methods/future.rb,
lib/live_resource/methods/method.rb,
lib/live_resource/resource_proxy.rb,
lib/live_resource/methods/forward.rb,
lib/live_resource/methods/dispatcher.rb,
lib/live_resource/redis_client/methods.rb,
lib/live_resource/redis_client/attributes.rb,
lib/live_resource/redis_client/registration.rb

Overview

LiveResource is a framework for coordinating processes and status within a distributed system. Consult the documention for LiveResource::Resource for attribute and method providers, LiveResource::Finders for discovering resources, and LiveResource::ResourceProxy for using resources.

Defined Under Namespace

Modules: Attributes, Declarations, Finders, LogHelper, Methods, Resource Classes: Future, RedisClient, RemoteMethod, RemoteMethodDispatcher, RemoteMethodForward, RemoteMethodToken, ResourceProxy

Class Method Summary collapse

Class Method Details

.all(resource_class) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/live_resource/finders.rb', line 6

def LiveResource.all(resource_class)
  redis_names = RedisClient.new(resource_class, nil).all

  redis_names.map do |redis_name|
    ResourceProxy.new(RedisClient.redisized_key(resource_class), redis_name)
  end
end

.any(resource_class) ⇒ Object



36
37
38
39
40
# File 'lib/live_resource/finders.rb', line 36

def LiveResource.any(resource_class)
  resources = all(resource_class)

  resources[rand(resources.length)]
end

.find(resource_class, resource_name = nil, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/live_resource/finders.rb', line 14

def LiveResource.find(resource_class, resource_name = nil, &block)
  if resource_name.nil? and block.nil?
    # Find class resource instead of instance resource.
    resource_name = resource_class
    resource_class = "class"
  end

  if block.nil?
    block = lambda { |name| name == RedisClient.redisized_key(resource_name) ? name : nil }
  end

  redis_name = RedisClient.new(resource_class, nil).all.find do |name|
    block.call(name)
  end

  if redis_name
    ResourceProxy.new(RedisClient.redisized_key(resource_class), redis_name)
  else
    nil
  end
end

.register(resource) ⇒ Object

Register the resource, allowing its discovery and methods to be called on it. This method will block until the resource is fully registered and its method dispatcher is running.

Parameters:



15
16
17
18
19
20
21
22
# File 'lib/live_resource.rb', line 15

def self.register(resource)
  # puts "registering #{resource.to_s}"

  @@resources ||= Set.new
  @@resources << resource

  resource.start
end

.run(exit_signal = "INT", &exit_cb) ⇒ Object

Run LiveResource until the exit_signal (default=SIGINT) is recevied. Optionally invoke the exit callback before exiting.



56
57
58
59
60
61
62
63
64
65
# File 'lib/live_resource.rb', line 56

def self.run(exit_signal="INT", &exit_cb)
  Signal.trap(exit_signal) do
    self.stop
    yield if exit_cb
    exit
  end

  # Put this thread to sleep
  sleep
end

.startObject

Start all resources. Usually not needed since registering a resource automatically starts it; however if you stopped LiveResource manually, this will let you re-start all registered resources.



41
42
43
44
45
# File 'lib/live_resource.rb', line 41

def self.start
  @@resources.each do |resource|
    resource.start
  end
end

.stopObject

Stop all resources, preventing methods from being called on them.



48
49
50
51
52
# File 'lib/live_resource.rb', line 48

def self.stop
  @@resources.each do |resource|
    resource.stop
  end
end

.unregister(resource) ⇒ Object

Unregister the resource, removing it from discovery and stopping its method dispatcher. This method will block until the method dispatcher is stopped.

Parameters:



29
30
31
32
33
34
35
# File 'lib/live_resource.rb', line 29

def self.unregister(resource)
  # puts "unregistering #{resource.to_s}"

  resource.stop

  @@resources.delete resource
end