Skip to content

Commit

Permalink
Revert "Use job instead of @async"
Browse files Browse the repository at this point in the history
This reverts commit e3f00dd.

Set file_resource_seq value
Set download_seq value
Explicitly define GeneratedValue for entities
  • Loading branch information
amvanbaren committed Jul 18, 2023
1 parent f925747 commit 0a8da49
Show file tree
Hide file tree
Showing 24 changed files with 158 additions and 300 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,7 @@ public FileResource getBinary(ExtensionVersion extVersion, String binaryName) {
binary.setExtension(extVersion);
binary.setName(binaryName);
binary.setType(FileResource.DOWNLOAD);
try {
binary.setContent(Files.readAllBytes(extensionFile.getPath()));
} catch (IOException e) {
throw new RuntimeException(e);
}

binary.setContent(null);
return binary;
}

Expand Down
17 changes: 4 additions & 13 deletions server/src/main/java/org/eclipse/openvsx/ExtensionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@
import org.eclipse.openvsx.cache.CacheService;
import org.eclipse.openvsx.entities.*;
import org.eclipse.openvsx.publish.PublishExtensionVersionHandler;
import org.eclipse.openvsx.publish.PublishExtensionVersionJobRequest;
import org.eclipse.openvsx.repositories.RepositoryService;
import org.eclipse.openvsx.search.SearchUtilService;
import org.eclipse.openvsx.util.ErrorResultException;
import org.eclipse.openvsx.util.TempFile;
import org.eclipse.openvsx.util.TimeUtil;
import org.jobrunr.scheduling.JobRequestScheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
Expand All @@ -52,9 +50,6 @@ public class ExtensionService {
@Autowired
PublishExtensionVersionHandler publishHandler;

@Autowired
JobRequestScheduler scheduler;

@Value("${ovsx.publishing.require-license:false}")
boolean requireLicense;

Expand All @@ -66,14 +61,10 @@ public ExtensionVersion mirrorVersion(TempFile extensionFile, String signatureNa
}

public ExtensionVersion publishVersion(InputStream content, PersonalAccessToken token) {
try(var extensionFile = createExtensionFile(content)) {
var download = doPublish(extensionFile, null, token, TimeUtil.getCurrentUTC(), true);
publishHandler.persistDownload(download);
scheduler.enqueue(new PublishExtensionVersionJobRequest(download.getId()));
return download.getExtension();
} catch (IOException e) {
throw new RuntimeException(e);
}
var extensionFile = createExtensionFile(content);
var download = doPublish(extensionFile, null, token, TimeUtil.getCurrentUTC(), true);
publishHandler.publishAsync(download, extensionFile, this);
return download.getExtension();
}

private FileResource doPublish(TempFile extensionFile, String binaryName, PersonalAccessToken token, LocalDateTime timestamp, boolean checkDependencies) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
public class AdminStatistics {

@Id
@GeneratedValue
@GeneratedValue(generator = "adminStatisticsSeq")
@SequenceGenerator(name = "adminStatisticsSeq", sequenceName = "admin_statistics_seq")
long id;

int year;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
public class AzureDownloadCountProcessedItem {

@Id
@GeneratedValue
@GeneratedValue(generator = "azureDownloadCountProcessedItemSeq")
@SequenceGenerator(name = "azureDownloadCountProcessedItemSeq", sequenceName = "azure_download_count_processed_item_seq")
long id;

String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
public class Download {

@Id
@GeneratedValue
@GeneratedValue(generator = "downloadSeq")
@SequenceGenerator(name = "downloadSeq", sequenceName = "download_seq")
long id;

@Column(name = "file_resource_id_not_fk")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
public class Extension implements Serializable {

@Id
@GeneratedValue
@GeneratedValue(generator = "extensionSeq")
@SequenceGenerator(name = "extensionSeq", sequenceName = "extension_seq")
long id;

@Column(length = 128)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@

import java.time.LocalDateTime;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.*;

import org.eclipse.openvsx.json.ReviewJson;
import org.eclipse.openvsx.util.TimeUtil;
Expand All @@ -24,8 +20,9 @@
public class ExtensionReview {

@Id
@GeneratedValue
long id;
@GeneratedValue(generator = "extensionReviewSeq")
@SequenceGenerator(name = "extensionReviewSeq", sequenceName = "extension_review_seq")
long id;

@ManyToOne
Extension extension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public enum Type {
}

@Id
@GeneratedValue
@GeneratedValue(generator = "extensionVersionSeq")
@SequenceGenerator(name = "extensionVersionSeq", sequenceName = "extension_version_seq")
long id;

@ManyToOne
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public class FileResource {
public static final String STORAGE_AZURE = "azure-blob";

@Id
@GeneratedValue
@GeneratedValue(generator = "fileResourceSeq")
@SequenceGenerator(name = "fileResourceSeq", sequenceName = "file_resource_seq")
long id;

@OneToOne
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
* ****************************************************************************** */
package org.eclipse.openvsx.entities;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.*;

@Entity
public class MigrationItem {

@Id
@GeneratedValue
@GeneratedValue(generator = "migrationItemSeq")
@SequenceGenerator(name = "migrationItemSeq", sequenceName = "migration_item_seq")
long id;

String migrationScript;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ public class Namespace implements Serializable {
private static final String SL_TWITTER = "twitter";

@Id
@GeneratedValue
long id;
@GeneratedValue(generator = "namespaceSeq")
@SequenceGenerator(name = "namespaceSeq", sequenceName = "namespace_seq")
long id;

@Column(length = 128)
String publicId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
********************************************************************************/
package org.eclipse.openvsx.entities;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.*;

import org.eclipse.openvsx.json.NamespaceMembershipJson;

Expand All @@ -28,7 +23,8 @@ public class NamespaceMembership implements Serializable {
public static final String ROLE_CONTRIBUTOR = "contributor";

@Id
@GeneratedValue
@GeneratedValue(generator = "namespaceMembershipSeq")
@SequenceGenerator(name = "namespaceMembershipSeq", sequenceName = "namespace_membership_seq")
long id;

@ManyToOne
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@

import java.time.LocalDateTime;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.*;

@Entity
public class PersistedLog {

@Id
@GeneratedValue
@GeneratedValue(generator = "persistedLogSeq")
@SequenceGenerator(name = "persistedLogSeq", sequenceName = "persisted_log_seq")
long id;

LocalDateTime timestamp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@
import java.util.List;
import java.util.Objects;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import jakarta.persistence.*;

import org.eclipse.openvsx.json.AccessTokenJson;
import org.eclipse.openvsx.util.TimeUtil;
Expand All @@ -32,7 +24,8 @@
public class PersonalAccessToken implements Serializable {

@Id
@GeneratedValue
@GeneratedValue(generator = "personalAccessTokenSeq")
@SequenceGenerator(name = "personalAccessTokenSeq", sequenceName = "personal_access_token_seq")
long id;

@ManyToOne
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
* ****************************************************************************** */
package org.eclipse.openvsx.entities;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.*;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Arrays;
Expand All @@ -26,7 +24,8 @@ public class SignatureKeyPair implements Serializable {
public static final String KEYPAIR_MODE_DELETE = "delete";

@Id
@GeneratedValue
@GeneratedValue(generator = "signatureKeyPairSeq")
@SequenceGenerator(name = "signatureKeyPairSeq", sequenceName = "signature_key_pair_seq")
long id;

@Column(length = 128)
Expand Down
10 changes: 3 additions & 7 deletions server/src/main/java/org/eclipse/openvsx/entities/UserData.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@
import java.util.List;
import java.util.Objects;

import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.*;

import org.eclipse.openvsx.json.UserJson;

Expand All @@ -29,7 +24,8 @@ public class UserData implements Serializable {
public static final String ROLE_PRIVILEGED = "privileged";

@Id
@GeneratedValue
@GeneratedValue(generator = "userDataSeq")
@SequenceGenerator(name = "userDataSeq", sequenceName = "user_data_seq")
long id;

@Column(length = 32)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
package org.eclipse.openvsx.publish;

import com.google.common.base.Joiner;
import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import org.eclipse.openvsx.ExtensionProcessor;
import org.eclipse.openvsx.ExtensionService;
import org.eclipse.openvsx.ExtensionValidator;
import org.eclipse.openvsx.UserService;
import org.eclipse.openvsx.adapter.VSCodeIdService;
Expand All @@ -24,12 +23,18 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.annotation.Retryable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

@Component
Expand Down Expand Up @@ -196,6 +201,50 @@ private List<Extension> updateExistingPublicIds(Extension extension) {
return updatedExtensions;
}

@Async
@Retryable
public void publishAsync(FileResource download, TempFile extensionFile, ExtensionService extensionService) {
var extVersion = download.getExtension();

// Delete file resources in case publishAsync is retried
service.deleteFileResources(extVersion);
download.setId(0L);

service.storeDownload(download, extensionFile);
service.persistResource(download);
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));
}

// Update whether extension is active, the search index and evict cache
service.activateExtension(extVersion, extensionService);
try {
extensionFile.close();
} catch (IOException e) {
logger.error("failed to delete temp file", e);
}
}

public void mirror(FileResource download, TempFile extensionFile, String signatureName) {
var extVersion = download.getExtension();
service.mirrorResource(download);
Expand All @@ -216,10 +265,4 @@ private FileResource getSignatureResource(String signatureName, ExtensionVersion
resource.setType(FileResource.DOWNLOAD_SIG);
return resource;
}

@Transactional
public void persistDownload(FileResource download) {
download.setStorageType(FileResource.STORAGE_DB);
entityManager.persist(download);
}
}
Loading

0 comments on commit 0a8da49

Please sign in to comment.