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