-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance Archive service storage cleanup
- Loading branch information
Showing
6 changed files
with
81 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/org/commonjava/indy/service/archive/schedule/ArchiveFileCleanup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.commonjava.indy.service.archive.schedule; | ||
|
||
import io.quarkus.scheduler.Scheduled; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import org.commonjava.indy.service.archive.config.PreSeedConfig; | ||
import org.commonjava.indy.service.archive.controller.ArchiveController; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.commonjava.indy.service.archive.controller.ArchiveController.ARCHIVE_DIR; | ||
|
||
@ApplicationScoped | ||
public class ArchiveFileCleanup | ||
{ | ||
private final Logger logger = LoggerFactory.getLogger( getClass() ); | ||
|
||
@Inject | ||
ArchiveController controller; | ||
|
||
@Inject | ||
PreSeedConfig preSeedConfig; | ||
|
||
@Scheduled( delayed = "1h", every = "P1D" ) | ||
void cleanup() | ||
throws IOException | ||
{ | ||
logger.info( "Start not used archive files cleanup." ); | ||
String storeDir = preSeedConfig.storageDir().orElse( "data" ); | ||
String archiveDir = String.format( "%s%s", storeDir, ARCHIVE_DIR ); | ||
|
||
List<File> artifacts = controller.walkAllFiles( archiveDir ); | ||
for ( File artifact : artifacts ) | ||
{ | ||
BasicFileAttributes attrs = Files.readAttributes( artifact.toPath(), BasicFileAttributes.class ); | ||
Long notUsedDays = preSeedConfig.notUsedDaysCleanup().orElse( null ); | ||
if ( notUsedDays == null ) | ||
{ | ||
return; | ||
} | ||
Long days = TimeUnit.MILLISECONDS.toDays( System.currentTimeMillis() - attrs.lastAccessTime().toMillis() ); | ||
logger.debug( "file: {}, not used days: {}.", artifact.getPath(), days ); | ||
if ( days >= notUsedDays ) | ||
{ | ||
artifact.delete(); | ||
logger.info( | ||
"Not used archive files cleanup is finished for archive file: {}, not used days: {}, config: {}.", | ||
artifact.getPath(), days, notUsedDays ); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters