@@ -2158,7 +2158,7 @@ def update_grid_cell(project_id, grid_id, w_i, h_i, d_i, cell_width,
21582158 cell_height , cell_depth , params , allow_empty , lod_levels ,
21592159 lod_bucket_size , lod_strategy , update_json_cache ,
21602160 update_json_text_cache , update_msgpack_cache , provider = None ,
2161- cursor = None , delete_empty = True ) -> bool :
2161+ cursor = None , delete_empty = True , ignore_locked = True ) -> bool :
21622162 params ['left' ] = w_i * cell_width
21632163 params ['right' ] = (w_i + 1 ) * cell_width
21642164 params ['top' ] = h_i * cell_height
@@ -2172,6 +2172,35 @@ def update_grid_cell(project_id, grid_id, w_i, h_i, d_i, cell_width,
21722172 if not cursor :
21732173 cursor = connection .cursor ()
21742174
2175+ # Put a lock on this grid cache_cell. If ignore_locked is true, this update
2176+ # call is canceled if no lock can be acquired right away.
2177+ try :
2178+ cursor .execute (f"""
2179+ SELECT grid_id
2180+ FROM node_grid_cache_cell
2181+ WHERE grid_id = %(grid_id)s AND x_index = %(x_index)s AND
2182+ y_index = %(y_index)s AND z_index = %(z_index)s
2183+ FOR ${ '' if delete_empty and not allow_empty else 'NO KEY' } UPDATE
2184+ ${ 'NOWAIT' if ignore_locked else '' }
2185+ LIMIT 1;
2186+ """ , {
2187+ 'grid_id' : grid_id ,
2188+ 'x_index' : w_i ,
2189+ 'y_index' : h_i ,
2190+ 'z_index' : d_i ,
2191+ })
2192+ except e as Error :
2193+ logger .error (e )
2194+ # We couldn't get the lock.
2195+ if ignore_locked :
2196+ return True
2197+ pass
2198+
2199+ n_cells = cursor .fetchall ()[0 ]
2200+ if n_cells == 0 :
2201+ # If this cell is already locked,
2202+ return
2203+
21752204 result_tuple = _node_list_tuples_query (params , project_id , provider ,
21762205 include_labels = True )
21772206
0 commit comments