From: martyntaylor mtaylor@redhat.com
--- src/app/models/task.rb | 8 +++++++- src/app/models/task_observer.rb | 22 ++++++++++++++++++++++ src/db/migrate/20090831140000_create_tasks.rb | 1 + src/spec/models/task_observer_spec.rb | 24 ++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletions(-) create mode 100644 src/app/models/task_observer.rb create mode 100644 src/spec/models/task_observer_spec.rb
diff --git a/src/app/models/task.rb b/src/app/models/task.rb index 6cb907e..9f23ef2 100644 --- a/src/app/models/task.rb +++ b/src/app/models/task.rb @@ -28,6 +28,7 @@ class Task < ActiveRecord::Base # moved associations here so that nested set :include directives work
STATE_QUEUED = "queued" + STATE_PENDING = "pending" STATE_RUNNING = "running" STATE_FINISHED = "finished" STATE_PAUSED = "paused" @@ -35,7 +36,7 @@ class Task < ActiveRecord::Base STATE_CANCELED = "canceled"
COMPLETED_STATES = [STATE_FINISHED, STATE_FAILED, STATE_CANCELED] - WORKING_STATES = [STATE_QUEUED, STATE_RUNNING, STATE_PAUSED] + WORKING_STATES = [STATE_QUEUED, STATE_RUNNING, STATE_PAUSED, STATE_PENDING]
validates_inclusion_of :type, :in => %w( InstanceTask ) @@ -48,6 +49,7 @@ class Task < ActiveRecord::Base # depending on subclass, action, state
TASK_STATES_OPTIONS = [["Queued", Task::STATE_QUEUED], + ["Pending", Task::STATE_PENDING], ["Running", Task::STATE_RUNNING], ["Paused", Task::STATE_PAUSED], ["Finished", Task::STATE_FINISHED], @@ -92,6 +94,10 @@ class Task < ActiveRecord::Base ret_val end
+ def submission_time + time.started - time.submitted + end + def validate errors.add("created_at", "Task started but does not have the creation time set") if time_started and created_at.nil? errors.add("time_started", "Task ends but does not have the start time set") if time_ended and time_started.nil? diff --git a/src/app/models/task_observer.rb b/src/app/models/task_observer.rb new file mode 100644 index 0000000..3e2d803 --- /dev/null +++ b/src/app/models/task_observer.rb @@ -0,0 +1,22 @@ +class TaskObserver < ActiveRecord::Observer + def after_save(a_task) + if a_task.changed? + change = a_task.changes['state'] + if change + update_timestamp(change[0], change[1], a_task) + end + end + 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 + a_task.time_submitted = Time.now + end + end + +end + +TaskObserver.instance \ No newline at end of file diff --git a/src/db/migrate/20090831140000_create_tasks.rb b/src/db/migrate/20090831140000_create_tasks.rb index a9e099f..2c3e0e2 100644 --- a/src/db/migrate/20090831140000_create_tasks.rb +++ b/src/db/migrate/20090831140000_create_tasks.rb @@ -28,6 +28,7 @@ class CreateTasks < ActiveRecord::Migration t.string :task_target_type t.string :args t.timestamp :created_at + t.timestamp :time_submitted t.timestamp :time_started t.timestamp :time_ended t.text :message diff --git a/src/spec/models/task_observer_spec.rb b/src/spec/models/task_observer_spec.rb new file mode 100644 index 0000000..5d31693 --- /dev/null +++ b/src/spec/models/task_observer_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper' + +describe TaskObserver do + + before(:each) do + @timestamp = Time.now + @task = InstanceTask.new({}) + end + + it "should set started at timestamp when the task goes to state running" do + @task.state = Task::STATE_RUNNING + @task.save + + @task.time_started.should >= @timestamp + end + + it "should set time submitted timestamp when the task goes to state pending" do + @task.state = Task::STATE_PENDING + @task.save + + @task.time_submitted.should >= @timestamp + end + +end \ No newline at end of file
From: martyntaylor mtaylor@redhat.com
--- 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
From: martyntaylor mtaylor@redhat.com
--- src/app/models/instance.rb | 47 +++++++++-- src/app/models/instance_observer.rb | 24 ++++-- src/db/migrate/20090804142049_create_instances.rb | 12 +++- src/spec/factories/instance.rb | 6 +- src/spec/models/instance_observer_spec.rb | 90 +++++++++++++++++++- src/spec/models/instance_spec.rb | 23 +++++ 6 files changed, 177 insertions(+), 25 deletions(-)
diff --git a/src/app/models/instance.rb b/src/app/models/instance.rb index 25d17bd..a17cf91 100644 --- a/src/app/models/instance.rb +++ b/src/app/models/instance.rb @@ -52,10 +52,12 @@ class Instance < ActiveRecord::Base STATE_STOPPED = "stopped" STATE_CREATE_FAILED = "create_failed"
- validates_inclusion_of :state, - :in => [STATE_NEW, STATE_PENDING, STATE_RUNNING, + STATES = [STATE_NEW, STATE_PENDING, STATE_RUNNING, STATE_SHUTTING_DOWN, STATE_STOPPED, STATE_CREATE_FAILED]
+ validates_inclusion_of :state, + :in => STATES + def get_action_list(user=nil) # return empty list rather than nil # FIXME: not handling pending state now -- only current state @@ -101,11 +103,42 @@ 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 + # Returns the total time that this instance has been in the state + def total_state_time(state) + case state + when STATE_NEW + if self.state == STATE_NEW + return acc_new_time + (Time.now - time_last_new) + else + return acc_new_time + end + when STATE_PENDING + if self.state == STATE_PENDING + return acc_pending_time + (Time.now - time_last_pending) + else + return acc_pending_time + end + + when STATE_RUNNING + if self.state == STATE_RUNNING + return acc_running_time + (Time.now - time_last_running) + else + return acc_running_time + end + + when STATE_SHUTTING_DOWN + if self.state == STATE_SHUTTING_DOWN + return acc_shutting_down_time + (Time.now - time_last_shutting_down) + else + return acc_shutting_down_time + end + + when STATE_STOPPED + if self.state == STATE_STOPPED + return acc_stopped_time + (Time.now - time_last_stopped) + else + return acc_stopped_time + end end end
diff --git a/src/app/models/instance_observer.rb b/src/app/models/instance_observer.rb index 81e75ea..95f8d19 100644 --- a/src/app/models/instance_observer.rb +++ b/src/app/models/instance_observer.rb @@ -4,19 +4,31 @@ class InstanceObserver < ActiveRecord::Observer if an_instance.changed? change = an_instance.changes['state'] if change - update_timestamps(change[0], change[1], an_instance) + update_state_timestamps(change[1], an_instance) + update_accumulative_state_time(change[0], 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) + def update_state_timestamps(state_to, an_instance) + case state_to + when Instance::STATE_NEW then an_instance.time_last_new = Time.now + when Instance::STATE_PENDING then an_instance.time_last_pending = Time.now + when Instance::STATE_RUNNING then an_instance.time_last_running = Time.now + when Instance::STATE_SHUTTING_DOWN then an_instance.time_last_shutting_down = Time.now + when Instance::STATE_STOPPED then an_instance.time_last_stopped = Time.now end end
+ def update_accumulative_state_time(state_from, an_instance) + case state_from + when Instance::STATE_NEW then an_instance.acc_new_time += Time.now - an_instance.time_last_new + when Instance::STATE_PENDING then an_instance.acc_pending_time += Time.now - an_instance.time_last_pending + when Instance::STATE_RUNNING then an_instance.acc_running_time += Time.now - an_instance.time_last_running + when Instance::STATE_SHUTTING_DOWN then an_instance.acc_shutting_down_time += Time.now - an_instance.time_last_shutting_down + when Instance::STATE_STOPPED then an_instance.acc_stopped_time += Time.now - an_instance.time_last_stopped + end + end end
InstanceObserver.instance \ No newline at end of file diff --git a/src/db/migrate/20090804142049_create_instances.rb b/src/db/migrate/20090804142049_create_instances.rb index a571b68..216f091 100644 --- a/src/db/migrate/20090804142049_create_instances.rb +++ b/src/db/migrate/20090804142049_create_instances.rb @@ -33,8 +33,16 @@ class CreateInstances < ActiveRecord::Migration 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.integer :acc_new_time, :default => 0 + t.integer :acc_pending_time, :default => 0 + t.integer :acc_running_time, :default => 0 + t.integer :acc_shutting_down_time, :default => 0 + t.integer :acc_stopped_time, :default => 0 + t.timestamp :time_last_new + t.timestamp :time_last_pending + t.timestamp :time_last_running + t.timestamp :time_last_shutting_down + t.timestamp :time_last_stopped t.timestamps end end diff --git a/src/spec/factories/instance.rb b/src/spec/factories/instance.rb index 12156ec..611212d 100644 --- a/src/spec/factories/instance.rb +++ b/src/spec/factories/instance.rb @@ -4,9 +4,5 @@ Factory.define :instance do |i| i.hardware_profile_id 1 i.image_id 1 i.pool_id 1 - i.state "running" -end - -Factory.define :pending_instance, :parent => :instance do |pi| - pi.state "pending" + i.state Instance::STATE_RUNNING 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 index 65e0d50..a58d55e 100644 --- a/src/spec/models/instance_observer_spec.rb +++ b/src/spec/models/instance_observer_spec.rb @@ -4,26 +4,106 @@ describe InstanceObserver do
before(:each) do @timestamp = Time.now - @instance = Factory :pending_instance + @instance = Factory(:instance, :state => Instance::STATE_CREATE_FAILED) + end + + it "should set new at timestamp when instance goes to state new" do + @instance.state = Instance::STATE_NEW + @instance.save + + @instance.time_last_new.should >= @timestamp + end + + it "should set started at timestamp when instance goes to state pending" do + @instance.state = Instance::STATE_PENDING + @instance.save + + @instance.time_last_pending.should >= @timestamp 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 + @instance.time_last_running.should >= @timestamp + end + + it "should set started at timestamp when instance goes to state shutting down" do + @instance.state = Instance::STATE_SHUTTING_DOWN + @instance.save + + @instance.time_last_shutting_down.should >= @timestamp + end + + it "should set started at timestamp when instance goes to state stopped" do + @instance.state = Instance::STATE_STOPPED + @instance.save + + @instance.time_last_stopped.should >= @timestamp + end + + it "should set accumlated new time when instance changes state from state new" do + @instance.state = Instance::STATE_NEW + @instance.save + + sleep(1) + + @instance.state = Instance::STATE_PENDING + @instance.save + + @instance.acc_new_time.should >= 1 + @instance.acc_new_time.should <= 2 + end + + it "should set accumlated pending time when instance changes state from state pending" do + @instance.state = Instance::STATE_PENDING + @instance.save + + sleep(1) + + @instance.state = Instance::STATE_RUNNING + @instance.save + + @instance.acc_pending_time.should >= 1 + @instance.acc_pending_time.should <= 2 end
- it "should set accumlated run time when instance goes to from state running to state stopped" do + it "should set accumlated running time when instance changes state from state running" do @instance.state = Instance::STATE_RUNNING + @instance.save + + sleep(1) + + @instance.state = Instance::STATE_SHUTTING_DOWN + @instance.save + + @instance.acc_running_time.should >= 1 + @instance.acc_running_time.should <= 2 + end + + it "should set accumlated shutting down time when instance changes state from state shutting down" do + @instance.state = Instance::STATE_SHUTTING_DOWN @instance.save;
- sleep(2) + sleep(1)
@instance.state = Instance::STATE_STOPPED @instance.save
- @instance.acc_run_time.should >= 2 + @instance.acc_shutting_down_time.should >= 1 + @instance.acc_shutting_down_time.should <= 2 end
+ it "should set accumlated stopped time when instance changes state from state stopped" do + @instance.state = Instance::STATE_STOPPED + @instance.save + + sleep(1) + + @instance.state = Instance::STATE_PENDING + @instance.save + + @instance.acc_stopped_time.should >= 1 + @instance.acc_stopped_time.should <= 2 + end end \ No newline at end of file diff --git a/src/spec/models/instance_spec.rb b/src/spec/models/instance_spec.rb index 84cd234..70530a8 100644 --- a/src/spec/models/instance_spec.rb +++ b/src/spec/models/instance_spec.rb @@ -109,5 +109,28 @@ describe Instance do @instance.front_end_realm.should eql('mock2:john doe/different realm') end
+ it "should properly calculate the total time that the instance has been in a monitored state" do + [ Instance::STATE_NEW, Instance::STATE_PENDING, Instance::STATE_RUNNING, Instance::STATE_SHUTTING_DOWN, Instance::STATE_STOPPED ].each do |s| + # Use State create failed as default since it is not monitored + @instance = Factory(:instance, :state => Instance::STATE_CREATE_FAILED) + + # Test when instance is still in the same state + @instance.state = s + @instance.save + + sleep(1) + + @instance.total_state_time(s).should >= 1 + @instance.total_state_time(s).should <= 2 + + # Test when instance has changed state + sleep(1) + @instance.state = Instance::STATE_CREATE_FAILED + @instance.save + + @instance.total_state_time(s).should >= 2 + @instance.total_state_time(s).should <= 3 + end + end
end
From: martyntaylor mtaylor@redhat.com
--- src/app/models/task_observer.rb | 7 ++++++- src/spec/models/task_observer_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletions(-)
diff --git a/src/app/models/task_observer.rb b/src/app/models/task_observer.rb index ca11bc1..d1834c4 100644 --- a/src/app/models/task_observer.rb +++ b/src/app/models/task_observer.rb @@ -1,5 +1,8 @@ class TaskObserver < ActiveRecord::Observer - def after_save(a_task) + + END_STATES = [ Task::STATE_CANCELED, Task::STATE_FAILED, Task::STATE_FINISHED ] + + def after_save(a_task) if a_task.changed? change = a_task.changes['state'] if change @@ -13,6 +16,8 @@ class TaskObserver < ActiveRecord::Observer a_task.time_started = Time.now elsif state_to == Task::STATE_PENDING a_task.time_submitted = Time.now + elsif END_STATES.include?(state_to) + a_task.time_ended = Time.now end end
diff --git a/src/spec/models/task_observer_spec.rb b/src/spec/models/task_observer_spec.rb index 5d31693..98f899a 100644 --- a/src/spec/models/task_observer_spec.rb +++ b/src/spec/models/task_observer_spec.rb @@ -21,4 +21,25 @@ describe TaskObserver do @task.time_submitted.should >= @timestamp end
+ it "should set ended timestamp when the task has finished" do + @task.state = Task::STATE_FINISHED + @task.save + + @task.time_ended.should >= @timestamp + end + + it "should set ended timestamp when the task is cancelled" do + @task.state = Task::STATE_CANCELED + @task.save + + @task.time_ended.should >= @timestamp + end + + it "should set ended timestamp when the task has failed" do + @task.state = Task::STATE_FAILED + @task.save + + @task.time_ended.should >= @timestamp + end + end \ No newline at end of file
From: martyntaylor mtaylor@redhat.com
--- src/app/models/task.rb | 6 +++++- 1 files changed, 5 insertions(+), 1 deletions(-)
diff --git a/src/app/models/task.rb b/src/app/models/task.rb index 9f23ef2..5c5bf34 100644 --- a/src/app/models/task.rb +++ b/src/app/models/task.rb @@ -95,7 +95,11 @@ class Task < ActiveRecord::Base end
def submission_time - time.started - time.submitted + time_started - time_submitted + end + + def runtime + time_ended - time_started end
def validate
From: martyntaylor mtaylor@redhat.com
--- src/app/models/task.rb | 10 ++++++++++ src/db/migrate/20090831140000_create_tasks.rb | 1 + 2 files changed, 11 insertions(+), 0 deletions(-)
diff --git a/src/app/models/task.rb b/src/app/models/task.rb index 5c5bf34..f3aaa63 100644 --- a/src/app/models/task.rb +++ b/src/app/models/task.rb @@ -38,6 +38,16 @@ class Task < ActiveRecord::Base COMPLETED_STATES = [STATE_FINISHED, STATE_FAILED, STATE_CANCELED] WORKING_STATES = [STATE_QUEUED, STATE_RUNNING, STATE_PAUSED, STATE_PENDING]
+ # Failures Codes + FAILURE_PROVIDER_NOT_FOUND = "provider_not_found" + FAILURE_PROVIDER_CONTACT_FAILED = "provider_contact_failed" + FAILURE_PROVIDER_RETURNED_FAILED = "provider_returned_failed" + + FAILURE_CODES = [FAILURE_PROVIDER_NOT_FOUND, FAILURE_PROVIDER_CONTACT_FAILED, FAILURE_PROVIDER_RETURNED_FAILED] + + validates_inclusion_of :failure_code, + :in => FAILURE_CODES + [nil] + validates_inclusion_of :type, :in => %w( InstanceTask )
diff --git a/src/db/migrate/20090831140000_create_tasks.rb b/src/db/migrate/20090831140000_create_tasks.rb index 2c3e0e2..612b0b5 100644 --- a/src/db/migrate/20090831140000_create_tasks.rb +++ b/src/db/migrate/20090831140000_create_tasks.rb @@ -32,6 +32,7 @@ class CreateTasks < ActiveRecord::Migration t.timestamp :time_started t.timestamp :time_ended t.text :message + t.string :failure_code t.integer :lock_version, :default => 0 end end
On Fri, Apr 30, 2010 at 03:26:31PM +0100, mtaylor@redhat.com wrote:
From: martyntaylor mtaylor@redhat.com
src/app/models/task.rb | 8 +++++++- src/app/models/task_observer.rb | 22 ++++++++++++++++++++++ src/db/migrate/20090831140000_create_tasks.rb | 1 + src/spec/models/task_observer_spec.rb | 24 ++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletions(-) create mode 100644 src/app/models/task_observer.rb create mode 100644 src/spec/models/task_observer_spec.rb
diff --git a/src/app/models/task.rb b/src/app/models/task.rb index 6cb907e..9f23ef2 100644 --- a/src/app/models/task.rb +++ b/src/app/models/task.rb @@ -28,6 +28,7 @@ class Task < ActiveRecord::Base # moved associations here so that nested set :include directives work
STATE_QUEUED = "queued"
- STATE_PENDING = "pending" STATE_RUNNING = "running" STATE_FINISHED = "finished" STATE_PAUSED = "paused"
@@ -35,7 +36,7 @@ class Task < ActiveRecord::Base STATE_CANCELED = "canceled"
COMPLETED_STATES = [STATE_FINISHED, STATE_FAILED, STATE_CANCELED]
- WORKING_STATES = [STATE_QUEUED, STATE_RUNNING, STATE_PAUSED]
WORKING_STATES = [STATE_QUEUED, STATE_RUNNING, STATE_PAUSED, STATE_PENDING]
validates_inclusion_of :type, :in => %w( InstanceTask )
@@ -48,6 +49,7 @@ class Task < ActiveRecord::Base # depending on subclass, action, state
TASK_STATES_OPTIONS = [["Queued", Task::STATE_QUEUED],
["Pending", Task::STATE_PENDING], ["Running", Task::STATE_RUNNING], ["Paused", Task::STATE_PAUSED], ["Finished", Task::STATE_FINISHED],
@@ -92,6 +94,10 @@ class Task < ActiveRecord::Base ret_val end
- def submission_time
- time.started - time.submitted
- end
- def validate errors.add("created_at", "Task started but does not have the creation time set") if time_started and created_at.nil? errors.add("time_started", "Task ends but does not have the start time set") if time_ended and time_started.nil?
diff --git a/src/app/models/task_observer.rb b/src/app/models/task_observer.rb new file mode 100644 index 0000000..3e2d803 --- /dev/null +++ b/src/app/models/task_observer.rb @@ -0,0 +1,22 @@ +class TaskObserver < ActiveRecord::Observer
- def after_save(a_task)
- if a_task.changed?
change = a_task.changes['state']
if change
update_timestamp(change[0], change[1], a_task)
end
- end
- 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
a_task.time_submitted = Time.now
- end
- end
+end
+TaskObserver.instance \ No newline at end of file diff --git a/src/db/migrate/20090831140000_create_tasks.rb b/src/db/migrate/20090831140000_create_tasks.rb index a9e099f..2c3e0e2 100644 --- a/src/db/migrate/20090831140000_create_tasks.rb +++ b/src/db/migrate/20090831140000_create_tasks.rb @@ -28,6 +28,7 @@ class CreateTasks < ActiveRecord::Migration t.string :task_target_type t.string :args t.timestamp :created_at
t.timestamp :time_submitted t.timestamp :time_started t.timestamp :time_ended t.text :message
diff --git a/src/spec/models/task_observer_spec.rb b/src/spec/models/task_observer_spec.rb new file mode 100644 index 0000000..5d31693 --- /dev/null +++ b/src/spec/models/task_observer_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper'
+describe TaskObserver do
- before(:each) do
- @timestamp = Time.now
- @task = InstanceTask.new({})
- end
- it "should set started at timestamp when the task goes to state running" do
- @task.state = Task::STATE_RUNNING
- @task.save
- @task.time_started.should >= @timestamp
- end
- it "should set time submitted timestamp when the task goes to state pending" do
- @task.state = Task::STATE_PENDING
- @task.save
- @task.time_submitted.should >= @timestamp
- end
+end \ No newline at end of file -- 1.6.6.1
deltacloud-devel mailing list deltacloud-devel@lists.fedorahosted.org https://fedorahosted.org/mailman/listinfo/deltacloud-devel
Martyn,
I'm trying to test this patch set, but I'm not having any luck getting a Mock instance up via the aggregator. Do you have any suggestions on how to apply this to 'next' and get it working?
Thanks, Steve.
deltacloud-devel@lists.fedorahosted.org