b59859a82a529fffe72578e69f37c87dd527e542
[arvados.git] / services / api / lib / update_priorities.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 def row_lock_for_priority_update container_uuid
6   ActiveRecord::Base.connection.exec_query %{
7         select 1 from containers where containers.uuid in (select pri_container_uuid from container_tree($1)) order by containers.uuid for update
8   }, 'select_for_update_priorities', [[nil, container_uuid]]
9 end
10
11 def update_priorities starting_container_uuid
12   # Ensure the row locks were taken in order
13   row_lock_for_priority_update starting_container_uuid
14
15   ActiveRecord::Base.connection.exec_query %{
16 update containers set priority=computed.upd_priority from container_tree_priorities($1) as computed
17  where containers.uuid = computed.pri_container_uuid and priority != computed.upd_priority
18 }, 'update_priorities', [[nil, starting_container_uuid]]
19 end