Class: Cylons::Agent

Inherits:
Object
  • Object
show all
Includes:
ActiveAttr::MassAssignment, ActiveAttr::Model, ActiveModel::AttributeMethods, ActiveModel::Dirty, Associations, Attributes
Defined in:
lib/cylons/agent.rb

Overview

this is the local application remote which is dynamically built, from the registry

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes

#read_attribute, #write_attribute

Class Attribute Details

.builtObject

Returns the value of attribute built.



18
19
20
# File 'lib/cylons/agent.rb', line 18

def built
  @built
end

.schemaObject

Returns the value of attribute schema.



18
19
20
# File 'lib/cylons/agent.rb', line 18

def schema
  @schema
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



131
132
133
# File 'lib/cylons/agent.rb', line 131

def errors
  @errors
end

Class Method Details

.agent_namespaceObject



54
55
56
# File 'lib/cylons/agent.rb', line 54

def self.agent_namespace
  @agent_namespace ||= ::Cylons::RemoteDiscovery.namespace_for_agent(self.name)
end

.allObject



50
51
52
# File 'lib/cylons/agent.rb', line 50

def self.all
  remote.all
end

.build_agentObject



27
28
29
30
# File 'lib/cylons/agent.rb', line 27

def self.build_agent
  load_schema
  @built = true
end

.countObject



58
59
60
# File 'lib/cylons/agent.rb', line 58

def self.count
  remote.count
end

.create(params) ⇒ Object



74
75
76
# File 'lib/cylons/agent.rb', line 74

def self.create(params)
  remote.create(params)
end

.find(id) ⇒ Object



66
67
68
# File 'lib/cylons/agent.rb', line 66

def self.find(id)
  remote.find(id)
end

.firstObject



62
63
64
# File 'lib/cylons/agent.rb', line 62

def self.first
  remote.first
end

.first_or_create(params) ⇒ Object



86
87
88
89
90
# File 'lib/cylons/agent.rb', line 86

def self.first_or_create(params)
  result = remote.scope_by(params).first
  result = remote.create(params) unless result.present?
  result
end

.first_or_initialize(params) ⇒ Object



92
93
94
95
96
# File 'lib/cylons/agent.rb', line 92

def self.first_or_initialize(params)
  result = remote.scope_by(params).first
  result ||= remote.new(params) unless result.present?
  result
end

.inherited(subklass) ⇒ Object



23
24
25
# File 'lib/cylons/agent.rb', line 23

def self.inherited(subklass)
  ::Cylons.connect unless ::Cylons.connected?
end

.lastObject



70
71
72
# File 'lib/cylons/agent.rb', line 70

def self.last
  remote.last
end

.load_schemaObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cylons/agent.rb', line 36

def self.load_schema
  ::Cylons.logger.debug { "Loading schema for #{name}" }

  @schema = ::Cylons::RemoteRegistry.get_remote_schema(name.downcase)

  @schema.remote_attributes.each do |remote_attribute|
    attribute remote_attribute.to_sym
  end

  @schema.remote_associations.each do |association_hash|
    __send__("build_remote_#{association_hash[:association_type]}_association", association_hash)
  end
end

.remoteObject



98
99
100
101
102
103
104
# File 'lib/cylons/agent.rb', line 98

def self.remote
  remote_connection_failed! unless remote?

  build_agent unless built

  ::DCell::Node[agent_namespace][service_class_name.to_sym]
end

.remote?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/cylons/agent.rb', line 106

def self.remote?
  agent_namespace && remote_application? && remote_service?
end

.remote_application?Boolean

Returns:

  • (Boolean)


110
111
112
113
114
# File 'lib/cylons/agent.rb', line 110

def self.remote_application?
  ::DCell::Node.all.any?{ |node|
    node.id == agent_namespace
  }
end

.remote_connection_failed!Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/cylons/agent.rb', line 120

def self.remote_connection_failed!
  if self.unsucessful_remote_connection_attempts > ::Cylons.config.remote_connection_failure_threshold
    self.unsucessful_remote_connection_attempts = 0
    raise ::Cylons::CylonsRemoteServiceNotFound, "#{service_class_name} not found"
  else
    sleep(::Cylons.config.remote_connection_failure_timeout)
    self.unsucessful_remote_connection_attempts += 1
    remote_connection_failed! unless remote?
  end
end

.remote_service?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/cylons/agent.rb', line 116

def self.remote_service?
  ::DCell::Node[agent_namespace].actors.include?(service_class_name.to_sym)
end

.scope_by(params) ⇒ Object



82
83
84
# File 'lib/cylons/agent.rb', line 82

def self.scope_by(params)
  remote.scope_by(params)
end

.search(params) ⇒ Object



78
79
80
# File 'lib/cylons/agent.rb', line 78

def self.search(params)
  remote.search(params)
end

.service_class_nameObject



32
33
34
# File 'lib/cylons/agent.rb', line 32

def self.service_class_name
  "#{name}Service"
end

Instance Method Details

#destroyObject



133
134
135
136
# File 'lib/cylons/agent.rb', line 133

def destroy
  return unless self.attributes["id"]
  result = self.class.remote.destroy(self.attributes["id"])
end

#saveObject

have to manually update attributes if id wasnt set, i believe attr_accessible oddity so maybe can rip out later



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cylons/agent.rb', line 139

def save
  if self.attributes["id"]
    result = self.class.remote.save(self.attributes["id"], self.attributes.with_indifferent_access.slice(*self.changed))
    self.changed_attributes.clear
  else
    result = self.class.remote.save(nil, self.attributes.with_indifferent_access.slice(*self.changed))

    if result.errors.messages.present?
      self.assign_attributes({:errors => result.errors})
    else
      self.assign_attributes(result.attributes)
    end
  end

  result
end