lib/cpee/implementation.rb in cpee-2.1.29 vs lib/cpee/implementation.rb in cpee-2.1.30
- old
+ new
@@ -14,10 +14,11 @@
require 'fileutils'
require 'redis'
require 'riddl/server'
require 'riddl/client'
+require_relative 'fail'
require_relative 'redis'
require_relative 'message'
require_relative 'persistence'
require_relative 'statemachine'
require_relative 'implementation_properties'
@@ -81,11 +82,11 @@
opts[:sse_keepalive_frequency] ||= 10
opts[:sse_connections] = {}
opts[:statemachine] = CPEE::StateMachine.new opts[:states], %w{running simulating replaying finishing stopping abandoned finished} do |id|
- opts[:redis].get("instance:#{id}/state")
+ CPEE::Persistence::extract_item(id,opts,"state")
end
opts[:runtime_cmds] << [
"startclean", "Delete instances before starting.", Proc.new { |status|
Dir.glob(File.expand_path(File.join(opts[:instances],'*'))).each do |d|
@@ -174,20 +175,18 @@
end
end
class Instances < Riddl::Implementation #{{{
def response
- redis = @a[0][:redis]
+ opts = @a[0]
Riddl::Parameter::Complex.new("wis","text/xml") do
ins = XML::Smart::string('<instances/>')
- redis.zrevrange('instances',0,-1).each do |instance|
- statekey = "instance:#{instance}/state"
- attributes = "instance:#{instance}/attributes/"
- info = redis.get(attributes + 'info')
- uuid = redis.get(attributes + 'uuid')
- state = redis.get(statekey)
- state_changed = redis.get(File.join(statekey,'@changed'))
+ CPEE::Persistence::each_object(opts) do |instance|
+ info = CPEE::Persistence::extract_item(instance,opts,'attributes/info')
+ uuid = CPEE::Persistence::extract_item(instance,opts,'attributes/uuid')
+ state = CPEE::Persistence::extract_item(instance,opts,'state')
+ state_changed = CPEE::Persistence::extract_item(instance,opts,'state/@changed')
ins.root.add('instance', info, 'uuid' => uuid, 'id' => instance, 'state' => state, 'state_changed' => state_changed )
end
ins.to_s
end
end
@@ -207,13 +206,13 @@
opts = @a[0]
redis = opts[:redis]
doc = XML::Smart::open_unprotected(opts[:properties_init])
doc.register_namespace 'p', 'http://cpee.org/ns/properties/2.0'
name = @p[0].value
- id = redis.zrevrange('instances', 0, 0).first.to_i + 1
+ id = CPEE::Persistence::new_object(opts)
uuid = SecureRandom.uuid
- instance = 'instance:' + id.to_s
+ instance = CPEE::Persistence::OBJ + ':' + id.to_s
redis.multi do |multi|
multi.zadd('instances',id,id)
doc.root.find(PROPERTIES_PATHS_FULL.join(' | ')).each do |e|
if e.class == XML::Smart::Dom::Element && e.element_only?
val = e.find('*').map { |f| f.dump }.join
@@ -233,26 +232,32 @@
Dir[File.join(opts[:notifications_init],'*','subscription.xml')].each do |f|
XML::Smart::open_unprotected(f) do |doc|
doc.register_namespace 'np', 'http://riddl.org/ns/common-patterns/notifications-producer/2.0'
key = File.basename(File.dirname(f))
url = doc.find('string(/np:subscription/@url)')
- multi.sadd("instance:#{id}/handlers",key)
- multi.set("instance:#{id}/handlers/#{key}/url",url)
+ multi.sadd(CPEE::Persistence::OBJ + ":#{id}/handlers",key)
+ multi.set(CPEE::Persistence::OBJ + ":#{id}/handlers/#{key}/url",url)
doc.find('/np:subscription/np:topic/*').each do |e|
c = File.join(e.parent.attributes['id'],e.qname.name,e.text)
- multi.sadd("instance:#{id}/handlers/#{key}",c)
- multi.sadd("instance:#{id}/handlers/#{c}",key)
+ multi.sadd(CPEE::Persistence::OBJ + ":#{id}/handlers/#{key}",c)
+ multi.sadd(CPEE::Persistence::OBJ + ":#{id}/handlers/#{c}",key)
end
end rescue nil # all the ones that are not ok, are ignored
end
multi.set(File.join(instance, 'attributes', 'uuid'), uuid)
multi.zadd(File.join(instance, 'attributes'), -2, 'uuid')
multi.set(File.join(instance, 'attributes', 'info'), name)
multi.zadd(File.join(instance, 'attributes'), -1, 'info')
multi.set(File.join(instance, 'state', '@changed'), Time.now.xmlschema(3))
end
+ content = {
+ :state => 'ready',
+ :attributes => CPEE::Persistence::extract_list(id,opts,'attributes').to_h
+ }
+ CPEE::Message::send(:event,'state/change',File.join(opts[:url],'/'),id,uuid,name,content,redis)
+
@headers << Riddl::Header.new("CPEE-INSTANCE", id.to_s)
@headers << Riddl::Header.new("CPEE-INSTANCE-URL", File.join(opts[:url].to_s,id.to_s,'/'))
@headers << Riddl::Header.new("CPEE-INSTANCE-UUID", uuid)
Riddl::Parameter::Simple.new("id", id.to_s)
@@ -287,21 +292,25 @@
id = @r[0].to_i
unless CPEE::Persistence::exists?(id,opts)
@status = 404
return
end
- empt = redis.keys("instance:#{id}/*").to_a
+
+ content = {
+ :state => 'purged',
+ :attributes => CPEE::Persistence::extract_list(id,opts,'attributes').to_h
+ }
+ state = CPEE::Persistence::extract_item(id,opts,'state')
+ if state == 'stopped' || state == 'ready'
+ CPEE::Message::send(:event,'state/change',File.join(opts[:url],'/'),id,content[:attributes]['uuid'],content[:attributes]['info'],content,redis)
+ end
+
+ empt = CPEE::Persistence::keys(id,opts).to_a
+ empt.delete_if{|e| e =~ /\/handlers/ }
redis.multi do |multi|
multi.del empt
multi.zrem 'instances', id
end
end
end #}}}
-
- class FAIL < Riddl::Implementation #{{{
- def response
- @status = 404
- nil
- end
- end #}}}
end