Skip to content

Commit

Permalink
Run purgeFilesystem in async way when deleting repo with content (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruhan1 authored Jan 2, 2024
1 parent 1c6386a commit 93babb2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@
import org.commonjava.indy.service.repository.data.StoreValidator;
import org.commonjava.indy.service.repository.exception.IndyDataException;
import org.commonjava.indy.service.repository.exception.IndyWorkflowException;
import org.commonjava.indy.service.repository.exception.InvalidArtifactStoreException;
import org.commonjava.indy.service.repository.model.ArtifactStore;
import org.commonjava.indy.service.repository.model.RemoteRepository;
import org.commonjava.indy.service.repository.model.StoreKey;
import org.commonjava.indy.service.repository.model.StoreType;
import org.eclipse.microprofile.context.ManagedExecutor;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.core.Response;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -65,6 +64,9 @@ public class AdminController
@RestClient
StorageService storageService;

@Inject
ManagedExecutor executor;

protected AdminController()
{
}
Expand Down Expand Up @@ -194,8 +196,7 @@ public void delete( final StoreKey key, final String user, final String changelo
ArtifactStore store = storeManager.getArtifactStore( key ).orElse( null );
if ( store != null && deleteContent )
{
logger.info( "Delete content of {}", key );
purgeFilesystem( key.toString() );
purgeFilesystemAsync( key.toString() );
}
storeManager.deleteArtifactStore( key, new ChangeSummary( user, changelog ),
new EventMetadata().set( "deleteContent", deleteContent ) );
Expand All @@ -211,16 +212,19 @@ public void delete( final StoreKey key, final String user, final String changelo
}
}

private void purgeFilesystem(String filesystem)
private void purgeFilesystemAsync(final String filesystem)
{
try( Response resp = storageService.purge(filesystem) )
{
logger.info( "Purge filesystem done, code: {}, result: {}", resp.getStatus(), resp.getEntity() );
}
catch ( Exception e )
{
logger.warn( "Purge filesystem failed", e );
}
executor.runAsync(() -> {
logger.info( "Purge content of {}", filesystem );
try( Response resp = storageService.purge(filesystem) )
{
logger.info( "Purge filesystem done, code: {}, result: {}", resp.getStatus(), resp.getEntity() );
}
catch ( Exception e )
{
logger.warn( "Purge filesystem failed", e );
}
});
}

public boolean exists( final StoreKey key )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,26 @@ public void addMinimalRemoteRepositoryAndDeleteIt()

given().head( repoUrl ).then().statusCode( NOT_FOUND.getStatusCode() );
}

@Test
public void addRemoteRepositoryAndDeleteWithContent()
throws Exception
{
final String name = "build-" + newName();
final RemoteRepository repo = new RemoteRepository( MAVEN_PKG_KEY, name, "http://www.foo.com" );
final String json = mapper.writeValueAsString( repo );

given().body( json )
.contentType( APPLICATION_JSON )
.post( getRepoTypeUrl( repo.getKey() ) )
.then()
.body( "url", is( "http://www.foo.com" ) )
.body( new RepoEqualMatcher<>( mapper, repo, RemoteRepository.class ) );
final String repoUrl = getRepoUrl( repo.getKey() );
given().head( repoUrl ).then().statusCode( OK.getStatusCode() );

delete( repoUrl + "?deleteContent=true" ); // Delete with content

given().head( repoUrl ).then().statusCode( NOT_FOUND.getStatusCode() );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (C) 2022-2023 Red Hat, Inc. (https://github.com/Commonjava/indy-repository-service)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.commonjava.indy.service.repository.jaxrs.mock;

import io.quarkus.test.Mock;
import org.commonjava.indy.model.core.BatchDeleteResult;
import org.commonjava.indy.service.repository.client.storage.StorageService;
import org.commonjava.indy.service.repository.jaxrs.ResponseHelper;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import javax.inject.Inject;
import javax.ws.rs.core.Response;
import java.util.HashSet;
import java.util.Set;

@Mock
@RestClient
public class MockStorageService implements StorageService
{
@Inject ResponseHelper helper;

@Override
public Response purge(String filesystem) throws Exception
{
BatchDeleteResult result = new BatchDeleteResult();
result.setFilesystem( filesystem );
Set<String> succeeded = new HashSet<>();
succeeded.add( "foo/bar/1.0/bar-1.0.jar" );
result.setSucceeded( succeeded );
return helper.formatOkResponseWithJsonEntity( result );
}
}

0 comments on commit 93babb2

Please sign in to comment.