Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[service-ng] Define in-memory failed_times explicitly

Change-Id: I916193a7a4138799c563664d17925853cb7a60a6
  • Loading branch information...
commit 1b08b0c4eb2f2b89d1091c5cf8d9c52624926557 1 parent a8e395b
@felixhoo felixhoo authored
View
57 lib/base/warden/service.rb
@@ -34,22 +34,25 @@ def init(options)
@in_memory_status = {}
end
- attr_reader :base_dir, :log_dir, :bin_dir, :common_dir, :image_dir, :max_disk, :logger, :quota, :max_memory, :memory_overhead, :service_start_timeout, :bandwidth_per_second, :service_port, :rm_instance_dir_timeout, :m_failed_times, :in_memory_status
- end
+ def define_im_properties(*args)
+ args.each do |prop|
+ define_method("#{prop}=".to_sym) do |value|
+ self.class.in_memory_status[self[:name]] ||= {}
+ self.class.in_memory_status[self[:name]][prop] = value
+ end
- def method_missing(name, *args, &block)
- prop = name.to_s.chomp("=").to_sym
- self.class.send(:define_method, "#{prop}=".to_sym) do |value|
- self.class.in_memory_status[self[:name]] ||= {}
- self.class.in_memory_status[self[:name]][prop] = value
+ define_method(prop) do
+ self.class.in_memory_status[self[:name]] && self.class.in_memory_status[self[:name]][prop]
+ end
+ end
end
- self.class.send(:define_method, prop) do
- self.class.in_memory_status[self[:name]] && self.class.in_memory_status[self[:name]][prop]
- end
- send(name, *args, &block)
+ attr_reader :base_dir, :log_dir, :bin_dir, :common_dir, :image_dir, :max_disk, :logger, :quota, :max_memory, :memory_overhead, :service_start_timeout, :bandwidth_per_second, :service_port, :rm_instance_dir_timeout, :m_failed_times, :in_memory_status
+
end
+ define_im_properties :failed_times
+
def in_monitored?
!failed_times || failed_times <= self.class.m_failed_times
end
@@ -134,30 +137,36 @@ def migration_check
end
end
+ def task(desc)
+ begin
+ yield
+ rescue => e
+ logger.error("Fail to #{desc}. Error: #{e}")
+ end
+ end
+
# instance operation helper
def delete
container_name = self[:container]
name = self[:name]
- # delete the record when it's saved
- destroy! if saved?
- self.class.in_memory_status.delete(name)
- # stop container
- begin
+ task "destroy record in local db" do
+ destroy! if saved?
+ end
+
+ task "delete in-memory status" do
+ self.class.in_memory_status.delete(name)
+ end
+
+ task "stop container when deleting service #{name}" do
stop(container_name)
- rescue
- # catch the exception and record error log here to guarantee the following cleanup work is done.
- logger.error("Fail to stop container when deleting service #{self[:name]}")
end
- # delete log and service directory
- begin
+
+ task "delete instance directories" do
if self.class.quota
self.class.sh("rm -f #{image_file}", {:block => false})
end
# delete serivce data directory could be slow, so increase the timeout
self.class.sh("rm -rf #{base_dir} #{log_dir} #{util_dirs.join(' ')}", {:block => false, :timeout => self.class.rm_instance_dir_timeout})
- rescue => e
- # catch the exception and record error log here to guarantee the following cleanup work is done.
- logger.error("Fail to delete instance directories, the error is #{e}")
end
end
View
2  spec/helper/warden_service_spec_helper.rb
@@ -6,7 +6,7 @@
:local_db => "sqlite:/tmp/base_test/base.db",
}
-class WardenService < VCAP::Services::Base::Warden::Service
+class Wardenservice < VCAP::Services::Base::Warden::Service
include DataMapper::Resource
property :name, String, :key => true
property :container, String
View
13 spec/unit_test/warden_service_spec.rb
@@ -3,8 +3,8 @@
describe "Warden Service test" do
before :all do
- WardenService.init(DEF_OPTIONS)
- 5.times { WardenService.create }
+ Wardenservice.init(DEF_OPTIONS)
+ 5.times { Wardenservice.create }
end
after :all do
@@ -14,15 +14,14 @@
it "should store in_memory properties" do
verify = {}
- WardenService.all.each do |ins|
+ Wardenservice.all.each do |ins|
id = UUIDTools::UUID.random_create.to_s
- ins.my_mem_status = id
+ ins.failed_times = id
verify[ins.name] = id
end
- WardenService.all.each do |ins|
- ins.my_mem_status.should == verify[ins.name]
- ins.my_new_prop.should be_nil
+ Wardenservice.all.each do |ins|
+ ins.failed_times.should == verify[ins.name]
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.