From: martyntaylor <[email protected]>
---
src/app/models/instance.rb | 7 +++++
src/app/models/instance_observer.rb | 22 ++++++++++++++++
src/app/models/task_observer.rb | 1 -
src/db/migrate/20090804142049_create_instances.rb | 26 ++++++++++--------
src/spec/factories/instance.rb | 4 +++
src/spec/models/instance_observer_spec.rb | 29 +++++++++++++++++++++
6 files changed, 76 insertions(+), 13 deletions(-)
create mode 100644 src/app/models/instance_observer.rb
create mode 100644 src/spec/models/instance_observer_spec.rb
diff --git a/src/app/models/instance.rb b/src/app/models/instance.rb
index fa89844..25d17bd 100644
--- a/src/app/models/instance.rb
+++ b/src/app/models/instance.rb
@@ -101,5 +101,12 @@ class Instance < ActiveRecord::Base
self.realm = provider.realms.find_by_name(realm_name) unless
realm_name.nil?
end
+ def total_run_time
+ if state == STATE_RUNNING
+ acc_run_time + (Time.now - time_last_start)
+ else
+ acc_run_time
+ end
+ end
end
diff --git a/src/app/models/instance_observer.rb
b/src/app/models/instance_observer.rb
new file mode 100644
index 0000000..81e75ea
--- /dev/null
+++ b/src/app/models/instance_observer.rb
@@ -0,0 +1,22 @@
+class InstanceObserver < ActiveRecord::Observer
+
+ def after_save(an_instance)
+ if an_instance.changed?
+ change = an_instance.changes['state']
+ if change
+ update_timestamps(change[0], change[1], an_instance)
+ end
+ end
+ end
+
+ def update_timestamps(state_from, state_to, an_instance)
+ if state_to == Instance::STATE_RUNNING
+ an_instance.time_last_start = Time.now
+ elsif state_from == Instance::STATE_RUNNING && state_to ==
Instance::STATE_STOPPED
+ an_instance.acc_run_time = an_instance.acc_run_time + (Time.now -
an_instance.time_last_start)
+ end
+ end
+
+end
+
+InstanceObserver.instance
\ No newline at end of file
diff --git a/src/app/models/task_observer.rb b/src/app/models/task_observer.rb
index 3e2d803..ca11bc1 100644
--- a/src/app/models/task_observer.rb
+++ b/src/app/models/task_observer.rb
@@ -9,7 +9,6 @@ class TaskObserver < ActiveRecord::Observer
end
def update_timestamp(state_from, state_to, a_task)
- puts state_to
if state_to == Task::STATE_RUNNING
a_task.time_started = Time.now
elsif state_to == Task::STATE_PENDING
diff --git a/src/db/migrate/20090804142049_create_instances.rb
b/src/db/migrate/20090804142049_create_instances.rb
index 714a7e4..a571b68 100644
--- a/src/db/migrate/20090804142049_create_instances.rb
+++ b/src/db/migrate/20090804142049_create_instances.rb
@@ -21,18 +21,20 @@
class CreateInstances < ActiveRecord::Migration
def self.up
- create_table :instances do |t|
- t.string :external_key
- t.string :name, :null => false, :limit => 1024
- t.integer :hardware_profile_id, :null => false
- t.integer :image_id, :null => false
- t.integer :realm_id
- t.integer :pool_id, :null => false
- t.integer :cloud_account_id
- t.string :public_address
- t.string :private_address
- t.string :state
- t.integer :lock_version, :default => 0
+ create_table :instances do |t|
+ t.string :external_key
+ t.string :name, :null => false, :limit => 1024
+ t.integer :hardware_profile_id, :null => false
+ t.integer :image_id, :null => false
+ t.integer :realm_id
+ t.integer :pool_id, :null => false
+ t.integer :cloud_account_id
+ t.string :public_address
+ t.string :private_address
+ t.string :state
+ t.integer :lock_version, :default => 0
+ t.integer :acc_run_time, :default => 0
+ t.timestamp :time_last_start
t.timestamps
end
end
diff --git a/src/spec/factories/instance.rb b/src/spec/factories/instance.rb
index 0cd1ea2..12156ec 100644
--- a/src/spec/factories/instance.rb
+++ b/src/spec/factories/instance.rb
@@ -6,3 +6,7 @@ Factory.define :instance do |i|
i.pool_id 1
i.state "running"
end
+
+Factory.define :pending_instance, :parent => :instance do |pi|
+ pi.state "pending"
+end
\ No newline at end of file
diff --git a/src/spec/models/instance_observer_spec.rb
b/src/spec/models/instance_observer_spec.rb
new file mode 100644
index 0000000..65e0d50
--- /dev/null
+++ b/src/spec/models/instance_observer_spec.rb
@@ -0,0 +1,29 @@
+require 'spec_helper'
+
+describe InstanceObserver do
+
+ before(:each) do
+ @timestamp = Time.now
+ @instance = Factory :pending_instance
+ end
+
+ it "should set started at timestamp when instance goes to state running" do
+ @instance.state = Instance::STATE_RUNNING
+ @instance.save
+
+ @instance.time_last_start.should >= @timestamp
+ end
+
+ it "should set accumlated run time when instance goes to from state running
to state stopped" do
+ @instance.state = Instance::STATE_RUNNING
+ @instance.save;
+
+ sleep(2)
+
+ @instance.state = Instance::STATE_STOPPED
+ @instance.save
+
+ @instance.acc_run_time.should >= 2
+ end
+
+end
\ No newline at end of file
--
1.6.6.1
_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel