@@ -35,13 +35,11 @@ async def process_idle_volumes(batch_size: int = 10):
3535 .with_for_update (skip_locked = True )
3636 )
3737 volume_models = list (res .unique ().scalars ().all ())
38- # Manually load relationships to avoid outer join in the locked query
3938 for volume_model in volume_models :
4039 await session .refresh (volume_model , ["project" , "attachments" ])
4140 if not volume_models :
4241 return
4342
44- # Add to lockset
4543 for volume_model in volume_models :
4644 lockset .add (volume_model .id )
4745
@@ -55,7 +53,6 @@ async def process_idle_volumes(batch_size: int = 10):
5553 await _delete_idle_volumes (session , volumes_to_delete )
5654
5755 finally :
58- # Remove from lockset
5956 for volume_model in volume_models :
6057 lockset .difference_update ([volume_model .id ])
6158
@@ -64,28 +61,22 @@ async def _should_delete_idle_volume(volume_model: VolumeModel) -> bool:
6461 """
6562 Check if a volume should be deleted based on its idle duration.
6663 """
67- # Get volume configuration
6864 configuration = get_volume_configuration (volume_model )
6965
70- # If no idle_duration is configured, don't delete
7166 if configuration .idle_duration is None :
7267 return False
7368
74- # If idle_duration is disabled (negative value), don't delete
7569 if isinstance (configuration .idle_duration , int ) and configuration .idle_duration < 0 :
7670 return False
7771
78- # Parse idle duration
7972 idle_duration_seconds = parse_duration (configuration .idle_duration )
8073 if idle_duration_seconds is None or idle_duration_seconds <= 0 :
8174 return False
8275
83- # Check if volume is currently attached to any instance
8476 if len (volume_model .attachments ) > 0 :
8577 logger .debug ("Volume %s is still attached to instances, not deleting" , volume_model .name )
8678 return False
8779
88- # Calculate how long the volume has been idle
8980 idle_duration = _get_volume_idle_duration (volume_model )
9081 idle_threshold = datetime .timedelta (seconds = idle_duration_seconds )
9182
@@ -117,15 +108,13 @@ async def _delete_idle_volumes(session: AsyncSession, volume_models: List[Volume
117108 """
118109 Delete volumes that have exceeded their idle duration.
119110 """
120- # Group volumes by project
121111 volumes_by_project = {}
122112 for volume_model in volume_models :
123113 project = volume_model .project
124114 if project not in volumes_by_project :
125115 volumes_by_project [project ] = []
126116 volumes_by_project [project ].append (volume_model .name )
127117
128- # Delete volumes by project
129118 for project , volume_names in volumes_by_project .items ():
130119 logger .info ("Deleting idle volumes for project %s: %s" , project .name , volume_names )
131120 try :
0 commit comments