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

Add RetryPolicy configuration #462

Merged
merged 2 commits into from
Jun 4, 2016
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
8 changes: 8 additions & 0 deletions src/main/java/org/acra/annotation/ReportsCrashes.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.acra.ReportingInteractionMode;
import org.acra.builder.NoOpReportPrimer;
import org.acra.builder.ReportPrimer;
import org.acra.config.DefaultRetryPolicy;
import org.acra.config.RetryPolicy;
import org.acra.dialog.BaseCrashReportDialog;
import org.acra.dialog.CrashReportDialog;
import org.acra.security.KeyStoreFactory;
Expand Down Expand Up @@ -592,4 +594,10 @@
* @return specify the type of the certificate set in either {@link #certificatePath()} or {@link #resCertificate()}
*/
@NonNull String certificateType() default ACRAConstants.DEFAULT_CERTIFICATE_TYPE;


/**
* @return a Class that decides if a report should be resent (usually if one or more senders failed).
*/
@NonNull Class<? extends RetryPolicy> retryPolicyClass() default DefaultRetryPolicy.class;
}
9 changes: 9 additions & 0 deletions src/main/java/org/acra/config/ACRAConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public final class ACRAConfiguration implements Serializable {
private final int resCertificate;
private final String certificatePath;
private final String certificateType;
private final Class<? extends RetryPolicy> retryPolicyClass;

/**
* @param builder ConfigurationBuilder with which to initialise this {@link ACRAConfiguration}.
Expand Down Expand Up @@ -165,6 +166,7 @@ public final class ACRAConfiguration implements Serializable {
resCertificate = builder.resCertificate();
certificatePath = builder.certificatePath();
certificateType = builder.certificateType();
retryPolicyClass = builder.retryPolicyClass();
}

/**
Expand Down Expand Up @@ -407,11 +409,18 @@ public int resCertificate() {
return resCertificate;
}

@NonNull
public String certificatePath() {
return certificatePath;
}

@NonNull
public String certificateType() {
return certificateType;
}

@NonNull
public Class<? extends RetryPolicy> retryPolicyClass() {
return retryPolicyClass;
}
}
16 changes: 16 additions & 0 deletions src/main/java/org/acra/config/ConfigurationBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public final class ConfigurationBuilder {
@RawRes private Integer resCertificate;
private String certificatePath;
private String certificateType;
private Class<? extends RetryPolicy> retryPolicyClass;


/**
Expand Down Expand Up @@ -181,6 +182,7 @@ public ConfigurationBuilder(@NonNull Application app) {
resCertificate = annotationConfig.resCertificate();
certificatePath = annotationConfig.certificatePath();
certificateType = annotationConfig.certificateType();
retryPolicyClass = annotationConfig.retryPolicyClass();
} else {
annotationType = null;
}
Expand Down Expand Up @@ -794,6 +796,12 @@ public ConfigurationBuilder setReportPrimerClass(@NonNull Class<? extends Report
return this;
}

@NonNull
public ConfigurationBuilder setRetryPolicyClass(@NonNull Class<? extends RetryPolicy> retryPolicyClass) {
this.retryPolicyClass = retryPolicyClass;
return this;
}


// Getters - used to provide values and !DEFAULTS! to ACRConfiguration during construction

Expand Down Expand Up @@ -1205,4 +1213,12 @@ String certificateType() {
Map<String, String> httpHeaders() {
return httpHeaders;
}

@NonNull
Class<? extends RetryPolicy> retryPolicyClass(){
if(retryPolicyClass != null){
return retryPolicyClass;
}
return DefaultRetryPolicy.class;
}
}
32 changes: 32 additions & 0 deletions src/main/java/org/acra/config/DefaultRetryPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2016
*
* 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.acra.config;

import org.acra.sender.ReportSender;

import java.util.List;

/**
* Default {@link RetryPolicy}. Only resend if all senders failed.
* @author F43nd1r
* @since 4.9.0
*/
public class DefaultRetryPolicy implements RetryPolicy {
@Override
public boolean shouldRetrySend(List<ReportSender> senders, List<FailedSender> failedSenders) {
return senders.size() == failedSenders.size();
}
}
55 changes: 55 additions & 0 deletions src/main/java/org/acra/config/RetryPolicy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2016
*
* 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.acra.config;

import org.acra.sender.ReportSender;
import org.acra.sender.ReportSenderException;

import java.util.List;

/**
* A policy which determines if a report should be resent.
*
* @author F43nd1r
* @since 4.9.0
*/
public interface RetryPolicy {

/**
* @param senders a list of all senders
* @param failedSenders a list of all failed senders with the thrown exceptions
* @return if the request should be resent later
*/
boolean shouldRetrySend(List<ReportSender> senders, List<FailedSender> failedSenders);

class FailedSender {
private final ReportSender sender;
private final ReportSenderException exception;

public FailedSender(ReportSender sender, ReportSenderException exception) {
this.sender = sender;
this.exception = exception;
}

public ReportSender getSender() {
return sender;
}

public ReportSenderException getException() {
return exception;
}
}
}
46 changes: 30 additions & 16 deletions src/main/java/org/acra/sender/ReportDistributor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@
import org.acra.ACRA;
import org.acra.collector.CrashReportData;
import org.acra.config.ACRAConfiguration;
import org.acra.config.DefaultRetryPolicy;
import org.acra.config.RetryPolicy;
import org.acra.file.CrashReportPersister;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import static org.acra.ACRA.LOG_TAG;
Expand Down Expand Up @@ -93,33 +97,43 @@ public void distribute(@NonNull File reportFile) {
*/
private void sendCrashReport(@NonNull CrashReportData errorContent) throws ReportSenderException {
if (!isDebuggable() || config.sendReportsInDevMode()) {
boolean sentAtLeastOnce = false;
ReportSenderException sendFailure = null;
String failedSender = null;
List<RetryPolicy.FailedSender> failedSenders = new LinkedList<RetryPolicy.FailedSender>();
for (ReportSender sender : reportSenders) {
try {
if (ACRA.DEV_LOGGING) ACRA.log.d(LOG_TAG, "Sending report using " + sender.getClass().getName());
sender.send(context, errorContent);
if (ACRA.DEV_LOGGING) ACRA.log.d(LOG_TAG, "Sent report using " + sender.getClass().getName());

// If at least one sender worked, don't re-send the report later.
sentAtLeastOnce = true;
} catch (ReportSenderException e) {
sendFailure = e;
failedSender = sender.getClass().getName();
failedSenders.add(new RetryPolicy.FailedSender(sender, e));
}
}

if (sendFailure != null) {
// We had some failure
if (!sentAtLeastOnce) {
throw sendFailure; // Don't log here because we aren't dealing with the Exception here.
} else {
ACRA.log.w(LOG_TAG,
"ReportSender of class "
+ failedSender
+ " failed but other senders completed their task. ACRA will not send this report again.");
RetryPolicy policy = null;
try {
policy = config.retryPolicyClass().newInstance();
} catch (InstantiationException e) {
ACRA.log.e(LOG_TAG, "Failed to create policy instance of class " + config.retryPolicyClass().getName(), e);
} catch (IllegalAccessException e) {
ACRA.log.e(LOG_TAG, "Failed to create policy instance of class " + config.retryPolicyClass().getName(), e);
}
if(policy == null){
policy = new DefaultRetryPolicy();
}

if (policy.shouldRetrySend(reportSenders, failedSenders)) {
ReportSenderException exception = new ReportSenderException("Policy marked this task as incomplete. ACRA will try to send this report again.");
if(failedSenders.size() > 0) exception.initCause(failedSenders.get(failedSenders.size() - 1).getException());
throw exception;
} else {
StringBuilder builder = new StringBuilder("ReportSenders of classes [");
for (Iterator<RetryPolicy.FailedSender> iterator = failedSenders.iterator(); iterator.hasNext();){
RetryPolicy.FailedSender failedSender = iterator.next();
builder.append(failedSender.getSender().getClass().getName());
if(iterator.hasNext()) builder.append(", ");
}
builder.append("] failed, but Policy marked this task as complete. ACRA will not send this report again.");
ACRA.log.w(LOG_TAG, builder.toString());
}
}
}
Expand Down