From: martyntaylor <[email protected]>

---
 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
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel

Reply via email to