-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #785 from amvanbaren/bugfix/issue-779
Use job instead of @async
- Loading branch information
Showing
9 changed files
with
244 additions
and
118 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
86 changes: 86 additions & 0 deletions
86
server/src/main/java/org/eclipse/openvsx/publish/PublishExtensionVersionJob.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,86 @@ | ||
/** ****************************************************************************** | ||
* Copyright (c) 2023 Precies. Software Ltd and others | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* ****************************************************************************** */ | ||
package org.eclipse.openvsx.publish; | ||
|
||
import org.eclipse.openvsx.ExtensionProcessor; | ||
import org.eclipse.openvsx.entities.FileResource; | ||
import org.eclipse.openvsx.migration.MigrationService; | ||
import org.eclipse.openvsx.util.NamingUtil; | ||
import org.jobrunr.jobs.lambdas.JobRequestHandler; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.AbstractMap; | ||
import java.util.function.Consumer; | ||
|
||
@Component | ||
public class PublishExtensionVersionJob implements JobRequestHandler<PublishExtensionVersionJobRequest> { | ||
|
||
protected final Logger logger = LoggerFactory.getLogger(PublishExtensionVersionJob.class); | ||
|
||
@Autowired | ||
ExtensionVersionIntegrityService integrityService; | ||
|
||
@Autowired | ||
PublishExtensionVersionJobService service; | ||
|
||
@Autowired | ||
MigrationService migrations; | ||
|
||
@Override | ||
public void run(PublishExtensionVersionJobRequest jobRequest) throws Exception { | ||
var download = service.getFileResource(jobRequest.getDownloadId()); | ||
var extVersion = download.getExtension(); | ||
logger.info("Processing files for {}", NamingUtil.toLogFormat(extVersion)); | ||
|
||
// Delete file resources in case job is retried | ||
service.deleteFileResources(extVersion); | ||
|
||
var content = migrations.getContent(download); | ||
var extensionFile = migrations.getExtensionFile(new AbstractMap.SimpleEntry<>(download, content)); | ||
try(var processor = new ExtensionProcessor(extensionFile)) { | ||
Consumer<FileResource> consumer = resource -> { | ||
service.storeResource(resource); | ||
service.persistResource(resource); | ||
}; | ||
|
||
if(integrityService.isEnabled()) { | ||
var keyPair = extVersion.getSignatureKeyPair(); | ||
if(keyPair != null) { | ||
var signature = integrityService.generateSignature(download, extensionFile, keyPair); | ||
consumer.accept(signature); | ||
} else { | ||
// Can happen when GenerateKeyPairJobRequestHandler hasn't run yet and there is no active SignatureKeyPair. | ||
// This extension version should be assigned a SignatureKeyPair and a signature FileResource should be created | ||
// by the ExtensionVersionSignatureJobRequestHandler migration. | ||
logger.warn("Integrity service is enabled, but {} did not have an active key pair", NamingUtil.toLogFormat(extVersion)); | ||
} | ||
} | ||
|
||
processor.processEachResource(extVersion, consumer); | ||
processor.getFileResources(extVersion).forEach(consumer); | ||
consumer.accept(processor.generateSha256Checksum(extVersion)); | ||
} | ||
|
||
service.storeDownload(download); | ||
|
||
// Update whether extension is active, the search index and evict cache | ||
service.activateExtension(extVersion); | ||
if(!download.getStorageType().equals(FileResource.STORAGE_DB)) { | ||
// Don't store the binary content in the DB - it's now stored externally | ||
download.setContent(null); | ||
} | ||
|
||
service.updateResource(download); | ||
logger.info("Published {}", NamingUtil.toLogFormat(extVersion)); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
server/src/main/java/org/eclipse/openvsx/publish/PublishExtensionVersionJobRequest.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,37 @@ | ||
/** ****************************************************************************** | ||
* Copyright (c) 2023 Precies. Software Ltd and others | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* ****************************************************************************** */ | ||
package org.eclipse.openvsx.publish; | ||
|
||
import org.jobrunr.jobs.lambdas.JobRequest; | ||
import org.jobrunr.jobs.lambdas.JobRequestHandler; | ||
|
||
public class PublishExtensionVersionJobRequest implements JobRequest { | ||
|
||
private long downloadId; | ||
|
||
public PublishExtensionVersionJobRequest() {} | ||
|
||
public PublishExtensionVersionJobRequest(long downloadId) { | ||
this.downloadId = downloadId; | ||
} | ||
|
||
public long getDownloadId() { | ||
return downloadId; | ||
} | ||
|
||
public void setDownloadId(long downloadId) { | ||
this.downloadId = downloadId; | ||
} | ||
|
||
@Override | ||
public Class<? extends JobRequestHandler> getJobRequestHandler() { | ||
return PublishExtensionVersionJob.class; | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
server/src/main/java/org/eclipse/openvsx/publish/PublishExtensionVersionJobService.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,87 @@ | ||
/** ****************************************************************************** | ||
* Copyright (c) 2023 Precies. Software Ltd and others | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* ****************************************************************************** */ | ||
package org.eclipse.openvsx.publish; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import jakarta.transaction.Transactional; | ||
import org.eclipse.openvsx.ExtensionService; | ||
import org.eclipse.openvsx.entities.ExtensionVersion; | ||
import org.eclipse.openvsx.entities.FileResource; | ||
import org.eclipse.openvsx.repositories.RepositoryService; | ||
import org.eclipse.openvsx.storage.StorageUtilService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.retry.annotation.Retryable; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class PublishExtensionVersionJobService { | ||
|
||
@Autowired | ||
EntityManager entityManager; | ||
|
||
@Autowired | ||
RepositoryService repositories; | ||
|
||
@Autowired | ||
StorageUtilService storageUtil; | ||
|
||
@Autowired | ||
ExtensionService extensions; | ||
|
||
public FileResource getFileResource(long id) { | ||
return entityManager.find(FileResource.class, id); | ||
} | ||
|
||
@Transactional | ||
public void deleteFileResources(ExtensionVersion extVersion) { | ||
repositories.findFiles(extVersion) | ||
.filter(f -> !f.getType().equals(FileResource.DOWNLOAD)) | ||
.forEach(entityManager::remove); | ||
} | ||
|
||
@Retryable | ||
public void storeDownload(FileResource resource) { | ||
// Store file resource in the DB or external storage | ||
if (storageUtil.shouldStoreExternally(resource)) { | ||
storageUtil.uploadFile(resource); | ||
} else { | ||
resource.setStorageType(FileResource.STORAGE_DB); | ||
} | ||
} | ||
|
||
@Retryable | ||
public void storeResource(FileResource resource) { | ||
// Store file resource in the DB or external storage | ||
if (storageUtil.shouldStoreExternally(resource)) { | ||
storageUtil.uploadFile(resource); | ||
// Don't store the binary content in the DB - it's now stored externally | ||
resource.setContent(null); | ||
} else { | ||
resource.setStorageType(FileResource.STORAGE_DB); | ||
} | ||
} | ||
|
||
@Transactional | ||
public void persistResource(FileResource resource) { | ||
entityManager.persist(resource); | ||
} | ||
|
||
@Transactional | ||
public void activateExtension(ExtensionVersion extVersion) { | ||
extVersion.setActive(true); | ||
extVersion = entityManager.merge(extVersion); | ||
extensions.updateExtension(extVersion.getExtension()); | ||
} | ||
|
||
@Transactional | ||
public void updateResource(FileResource resource) { | ||
entityManager.merge(resource); | ||
} | ||
} |
Oops, something went wrong.