1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.adservices.shared.spe.scheduling; 18 19 import java.util.Objects; 20 21 // TODO(b/324247018): Support a more comprehensive Backoff Policy for SPE. 22 /** The backoff policy if a job execution is failed. */ 23 public final class BackoffPolicy { 24 public static final BackoffPolicy DEFAULT = new BackoffPolicy.Builder().build(); 25 26 private final boolean mShouldRetryOnExecutionFailure; 27 private final boolean mShouldRetryOnExecutionStop; 28 BackoffPolicy( boolean shouldRetryOnExecutionFailure, boolean shouldRetryOnExecutionStop)29 private BackoffPolicy( 30 boolean shouldRetryOnExecutionFailure, boolean shouldRetryOnExecutionStop) { 31 mShouldRetryOnExecutionFailure = shouldRetryOnExecutionFailure; 32 mShouldRetryOnExecutionStop = shouldRetryOnExecutionStop; 33 } 34 35 /** 36 * Indicates if the execution needs to be retried if it encounters errors happening during the 37 * execution of a job. 38 * 39 * @return whether to retry the execution. 40 */ shouldRetryOnExecutionFailure()41 public boolean shouldRetryOnExecutionFailure() { 42 return mShouldRetryOnExecutionFailure; 43 } 44 45 /** 46 * Indicates if the execution needs to be retried if it's stopped by {@link 47 * android.app.job.JobScheduler} due to reasons like device issues, constraint not met, etc. 48 * 49 * @return whether to retry the execution. 50 */ shouldRetryOnExecutionStop()51 public boolean shouldRetryOnExecutionStop() { 52 return mShouldRetryOnExecutionStop; 53 } 54 55 @Override hashCode()56 public int hashCode() { 57 return Objects.hash(mShouldRetryOnExecutionFailure, mShouldRetryOnExecutionStop); 58 } 59 60 @Override equals(Object o)61 public boolean equals(Object o) { 62 if (this == o) { 63 return true; 64 } 65 if (!(o instanceof BackoffPolicy that)) { 66 return false; 67 } 68 return mShouldRetryOnExecutionFailure == that.mShouldRetryOnExecutionFailure 69 && mShouldRetryOnExecutionStop == that.mShouldRetryOnExecutionStop; 70 } 71 72 @Override toString()73 public String toString() { 74 return "BackoffPolicy{" 75 + "mShouldRetryOnExecutionFailure=" 76 + mShouldRetryOnExecutionFailure 77 + ", mShouldRetryOnExecutionStop=" 78 + mShouldRetryOnExecutionStop 79 + '}'; 80 } 81 82 /** Builder class for {@link BackoffPolicy}. */ 83 public static final class Builder { 84 // By default, the job should not retry. 85 private boolean mShouldRetryOnExecutionFailure; 86 private boolean mShouldRetryOnExecutionStop; 87 88 /** Setter for {@link #shouldRetryOnExecutionFailure()} */ setShouldRetryOnExecutionFailure(boolean value)89 public Builder setShouldRetryOnExecutionFailure(boolean value) { 90 mShouldRetryOnExecutionFailure = value; 91 return this; 92 } 93 94 /** Setter for {@link #shouldRetryOnExecutionStop()} */ setShouldRetryOnExecutionStop(boolean value)95 public Builder setShouldRetryOnExecutionStop(boolean value) { 96 mShouldRetryOnExecutionStop = value; 97 return this; 98 } 99 100 /** Build an instance of {@link BackoffPolicy}. */ build()101 public BackoffPolicy build() { 102 return new BackoffPolicy(mShouldRetryOnExecutionFailure, mShouldRetryOnExecutionStop); 103 } 104 } 105 } 106