Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: persistent errors for job processing [DHIS2-15276] #15575

Merged
merged 14 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,40 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.text.MessageFormat;
import java.util.List;
import java.util.stream.Stream;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import lombok.Getter;
import lombok.ToString;

/**
* @author Morten Olav Hansen <[email protected]>
*/
@Getter
@ToString
public class ErrorMessage {
private final ErrorCode errorCode;

private final Object[] args;

private final String message;
@JsonProperty private final ErrorCode errorCode;
@JsonProperty private final List<String> args;
@JsonProperty private String message;

public ErrorMessage(ErrorCode errorCode, Object... args) {
this.errorCode = errorCode;
this.args = args;
this.message = MessageFormat.format(errorCode.getMessage(), this.args);
this.args =
Stream.of(args)
.map(obj -> obj == null ? null : obj.toString())
.toList(); // OBS! Must support null values!
this.message = MessageFormat.format(errorCode.getMessage(), args);
}

@JsonCreator
public ErrorMessage(
@JsonProperty("message") String message, @JsonProperty("errorCode") ErrorCode errorCode) {
@Nonnull @JsonProperty("message") String message,
@Nonnull @JsonProperty("errorCode") ErrorCode errorCode,
@CheckForNull @JsonProperty("args") List<String> args) {
this.errorCode = errorCode;
this.args = null;
this.args = args == null ? List.of() : args;
this.message = message;
}

public ErrorCode getErrorCode() {
return errorCode;
}

public String getMessage() {
return message;
}

@Override
public String toString() {
return String.format("[%s: '%s']", errorCode.name(), message);
}
}
118 changes: 26 additions & 92 deletions dhis-2/dhis-api/src/main/java/org/hisp/dhis/feedback/ErrorReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,133 +29,67 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.google.common.base.MoreObjects;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.hisp.dhis.common.DxfNamespaces;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
* @author Morten Olav Hansen <[email protected]>
*/
@JacksonXmlRootElement(localName = "errorReport", namespace = DxfNamespaces.DXF_2_0)
@ToString
@Getter
@Setter
@Accessors(chain = true)
public class ErrorReport {
protected final ErrorMessage message;

protected final Class<?> mainKlass;
private final ErrorMessage message;
@JsonProperty private final Class<?> mainKlass;
@JsonProperty private String mainId;
@JsonProperty private Class<?> errorKlass;
@JsonProperty private String errorProperty;
@Nonnull @JsonProperty private List<?> errorProperties;
@JsonProperty private Object value;

protected String mainId;

protected Class<?> errorKlass;

protected String errorProperty;

protected List<Object> errorProperties = new ArrayList<>();

protected Object value;

public ErrorReport(Class<?> mainKlass, ErrorCode errorCode, Object... args) {
public ErrorReport(@Nonnull Class<?> mainKlass, @Nonnull ErrorCode errorCode, Object... args) {
this.mainKlass = mainKlass;
this.message = new ErrorMessage(errorCode, args);
this.errorProperties.addAll(Arrays.asList(args));
this.errorProperties = Arrays.asList(args); // OBS! Must support null values!
}

public ErrorReport(Class<?> mainKlass, ErrorMessage message) {
public ErrorReport(@Nonnull Class<?> mainKlass, @Nonnull ErrorMessage message) {
this.mainKlass = mainKlass;
this.message = message;
this.errorProperties = message.getArgs();
}

@JsonCreator
public ErrorReport(
@JsonProperty("message") String message,
@CheckForNull @JsonProperty("args") List<String> args,
@JsonProperty("mainKlass") Class<?> mainKlass,
@JsonProperty("errorCode") ErrorCode errorCode) {
this.mainKlass = mainKlass;
this.message = new ErrorMessage(message, errorCode);
this.message = new ErrorMessage(message, errorCode, args);
this.errorProperties = args == null ? List.of() : args;
}

@JsonProperty
@JacksonXmlProperty(namespace = DxfNamespaces.DXF_2_0)
public ErrorCode getErrorCode() {
return message.getErrorCode();
}

@JsonProperty
@JacksonXmlProperty(namespace = DxfNamespaces.DXF_2_0)
public String getMessage() {
return message.getMessage();
}

@JsonProperty
@JacksonXmlProperty(namespace = DxfNamespaces.DXF_2_0)
public Class<?> getMainKlass() {
return mainKlass;
}

@JsonProperty
@JacksonXmlProperty(namespace = DxfNamespaces.DXF_2_0)
public String getMainId() {
return mainId;
}

public ErrorReport setMainId(String mainId) {
this.mainId = mainId;
return this;
}

@JsonProperty
@JacksonXmlProperty(namespace = DxfNamespaces.DXF_2_0)
public Class<?> getErrorKlass() {
return errorKlass;
}

public ErrorReport setErrorKlass(Class<?> errorKlass) {
this.errorKlass = errorKlass;
return this;
}

@JsonProperty
@JacksonXmlProperty(namespace = DxfNamespaces.DXF_2_0)
public String getErrorProperty() {
return errorProperty;
}

public ErrorReport setErrorProperty(String errorProperty) {
this.errorProperty = errorProperty;
return this;
}

@JsonProperty
@JacksonXmlProperty(namespace = DxfNamespaces.DXF_2_0)
public List<Object> getErrorProperties() {
return errorProperties;
}

public void setErrorProperties(List<Object> errorProperties) {
this.errorProperties = errorProperties;
}

@JsonProperty
@JacksonXmlProperty(namespace = DxfNamespaces.DXF_2_0)
public Object getValue() {
return value;
}

public ErrorReport setValue(Object value) {
this.value = value;
return this;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("message", getMessage())
.add("errorCode", message.getErrorCode())
.add("mainKlass", mainKlass)
.add("errorKlass", errorKlass)
.add("value", value)
.toString();
public List<String> getArgs() {
return message.getArgs();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Set;
import javax.annotation.Nonnull;
import org.hisp.dhis.dataset.CompleteDataSetRegistration;
import org.hisp.dhis.fileresource.FileResource;
import org.hisp.dhis.user.User;
Expand All @@ -56,7 +57,7 @@ long sendValidationMessage(

long sendMessage(MessageConversationParams params);

long sendSystemErrorNotification(String subject, Throwable t);
void asyncSendSystemErrorNotification(@Nonnull String subject, @Nonnull Throwable t);

void sendReply(
MessageConversation conversation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ public PreheatIdentifier getPreheatIdentifier() {
}

public IdentifiableObject getObjectReference() {
return value != null ? (IdentifiableObject) value : null;
return getValue() != null ? (IdentifiableObject) getValue() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ public class JobConfiguration extends BaseIdentifiableObject implements Secondar
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Integer queuePosition;

@CheckForNull
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private String errorCodes;

public JobConfiguration() {}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ String create(JobConfiguration config, MimeType contentType, InputStream content
*/
int createDefaultJobs();

/**
* Make sure the {@link JobType#HEARTBEAT} entry exists as it is responsible for spawning the
* other system jobs when needed using {@link #createDefaultJobs()}.
*/
void createHeartbeatJob();
void createDefaultJob(JobType type);

/**
* Updates all {@link JobConfiguration}s that are not {@link JobConfiguration#isEnabled()} to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public interface JobConfigurationStore extends GenericDimensionalObjectStore<Job
@CheckForNull
String getProgress(@Nonnull String jobId);

@CheckForNull
String getErrors(@Nonnull String jobId);

/**
* @return UIDs of all existing job configurations.
*/
Expand Down Expand Up @@ -201,7 +204,8 @@ public interface JobConfigurationStore extends GenericDimensionalObjectStore<Job
*/
boolean trySkip(@Nonnull String queue);

void updateProgress(@Nonnull String jobId, @CheckForNull String progressJson);
void updateProgress(
@Nonnull String jobId, @CheckForNull String progressJson, @CheckForNull String errorCodes);

/**
* Switches {@link JobConfiguration#getJobStatus()} to {@link JobStatus#DISABLED} for any job that
Expand Down
Loading
Loading