Here's the original situation:
begin . . . task.instance.state = dcloud_to_instance_state(dcloud_instance.state) task.instance.save! task.state = Task::STATE_FINISHED rescue Exception => ex task.state = Task::STATE_FAILED task.message = ex.message ensure task.save! end
Due to optimistic locking, the above code gives stale object exceptions if the task is successfully deleted. My understanding is that this is because the after_save method in instance_observer.rb will (under certain circumstances), delete itself and its tasks, meaning task.save! will fail.
The patch replaces
task.save!
with task.save! if Task.exists?(task.id)
If there's a better way to handle this (or if my logic is wrong), please let me know!
Mainn
--- src/app/util/taskomatic.rb | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/src/app/util/taskomatic.rb b/src/app/util/taskomatic.rb index b8a7809..5b8dd97 100644 --- a/src/app/util/taskomatic.rb +++ b/src/app/util/taskomatic.rb @@ -70,7 +70,7 @@ module Taskomatic task.state = Task::STATE_FAILED task.message = ex.message ensure - task.save! + task.save! if Task.exists?(task.id) end end
On 02/03/2012 04:09 PM, Tzu-Mainn Chen wrote:
Here's the original situation:
begin . . . task.instance.state = dcloud_to_instance_state(dcloud_instance.state) task.instance.save! task.state = Task::STATE_FINISHED rescue Exception => ex task.state = Task::STATE_FAILED task.message = ex.message ensure task.save! end
Due to optimistic locking, the above code gives stale object exceptions if the task is successfully deleted. My understanding is that this is because the after_save method in instance_observer.rb will (under certain circumstances), delete itself and its tasks, meaning task.save! will fail.
The patch replaces
task.save!
with task.save! if Task.exists?(task.id)
If there's a better way to handle this (or if my logic is wrong), please let me know!
Mainn
ACK
BTW, I'm surprised by behaviour of "dcloud_instance.send(action)" - I thought that it's async call, but it updates dcloud_instance.state directly.
Then it's possible that an instance's state is changed to 'stopped' right after "dcloud_instance.send" call.And if it's last instance in a deployment, the deployment is destroyed (with instances).
Jan
aeolus-devel@lists.fedorahosted.org