Skip to content

Commit

Permalink
Enhance Archive service storage cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yma96 committed May 14, 2024
1 parent ea5ca78 commit 9f4a70f
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 7 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-scheduler</artifactId>
</dependency>
<!-- quarkus otel deps -->
<dependency>
<groupId>io.quarkus</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithName;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import java.util.Optional;

Expand All @@ -29,4 +28,7 @@ public interface PreSeedConfig

@WithName( "storage-dir" )
public Optional<String> storageDir();

@WithName( "not-used-days-cleanup" )
Optional<Long> notUsedDaysCleanup();
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ public class ArchiveController

public final static String EVENT_GENERATE_ARCHIVE = "generate-archive";

private final Logger logger = LoggerFactory.getLogger( getClass() );
public final static String CONTENT_DIR = "/content";

private final String CONTENT_DIR = "/content";
public final static String ARCHIVE_DIR = "/archive";

private final String ARCHIVE_DIR = "/archive";
private final Logger logger = LoggerFactory.getLogger( getClass() );

private final String ARCHIVE_SUFFIX = ".zip";

Expand Down Expand Up @@ -325,7 +325,8 @@ private boolean renderArchive( File part, final String buildConfigId )
return true;
}

private List<File> walkAllFiles( String path ) throws IOException
public List<File> walkAllFiles( String path )
throws IOException
{
List<File> contents = Files.walk( Paths.get( path ) )
.filter( Files::isRegularFile )
Expand Down
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 );
}
}
}
}
3 changes: 2 additions & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ quarkus:

pre-seed:
main-indy: https://indy-gateway-master-devel.psi.redhat.com
storage-dir: data
storage-dir: data
not-used-days-cleanup: 10
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public Optional<String> mainIndy()
@Override
public Optional<String> storageDir()
{
return Optional.of("data");
return Optional.of( "data" );
}

@Override
public Optional<Long> notUsedDaysCleanup()
{
return Optional.of( 365l );
}

}

0 comments on commit 9f4a70f

Please sign in to comment.