• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.service.measurement;
18 
19 import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_EXTSERVICES_JOB_ON_TPLUS;
20 import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON;
21 import static com.android.adservices.spe.AdservicesJobInfo.MEASUREMENT_DELETE_EXPIRED_JOB;
22 
23 import android.app.job.JobInfo;
24 import android.app.job.JobParameters;
25 import android.app.job.JobScheduler;
26 import android.app.job.JobService;
27 import android.content.ComponentName;
28 import android.content.Context;
29 
30 import com.android.adservices.LogUtil;
31 import com.android.adservices.concurrency.AdServicesExecutors;
32 import com.android.adservices.data.measurement.DatastoreManagerFactory;
33 import com.android.adservices.service.AdServicesConfig;
34 import com.android.adservices.service.FlagsFactory;
35 import com.android.adservices.service.common.compat.ServiceCompatUtils;
36 import com.android.adservices.spe.AdservicesJobServiceLogger;
37 import com.android.internal.annotations.VisibleForTesting;
38 
39 import java.util.concurrent.Executor;
40 
41 /** Service for scheduling delete-expired-records job. */
42 public final class DeleteExpiredJobService extends JobService {
43     private static final int MEASUREMENT_DELETE_EXPIRED_JOB_ID =
44             MEASUREMENT_DELETE_EXPIRED_JOB.getJobId();
45 
46     private static final Executor sBackgroundExecutor = AdServicesExecutors.getBackgroundExecutor();
47 
48     @Override
onStartJob(JobParameters params)49     public boolean onStartJob(JobParameters params) {
50         // Always ensure that the first thing this job does is check if it should be running, and
51         // cancel itself if it's not supposed to be.
52         if (ServiceCompatUtils.shouldDisableExtServicesJobOnTPlus(this)) {
53             LogUtil.d(
54                     "Disabling DeleteExpiredJobService job because it's running in ExtServices on"
55                             + " T+");
56             return skipAndCancelBackgroundJob(
57                     params,
58                     AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_EXTSERVICES_JOB_ON_TPLUS);
59         }
60 
61         AdservicesJobServiceLogger.getInstance(this)
62                 .recordOnStartJob(MEASUREMENT_DELETE_EXPIRED_JOB_ID);
63 
64         if (FlagsFactory.getFlags().getMeasurementJobDeleteExpiredKillSwitch()) {
65             LogUtil.e("DeleteExpiredJobService is disabled");
66             return skipAndCancelBackgroundJob(
67                     params,
68                     AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON);
69         }
70 
71         LogUtil.d("DeleteExpiredJobService.onStartJob");
72         sBackgroundExecutor.execute(
73                 () -> {
74                     long earliestValidInsertion =
75                             System.currentTimeMillis()
76                                     - FlagsFactory.getFlags().getMeasurementDataExpiryWindowMs();
77                     DatastoreManagerFactory.getDatastoreManager(this)
78                             .runInTransaction(
79                                     dao -> dao.deleteExpiredRecords(earliestValidInsertion));
80 
81                     boolean shouldRetry = false;
82                     AdservicesJobServiceLogger.getInstance(DeleteExpiredJobService.this)
83                             .recordJobFinished(
84                                     MEASUREMENT_DELETE_EXPIRED_JOB_ID,
85                                     /* isSuccessful */ true,
86                                     shouldRetry);
87 
88                     jobFinished(params, shouldRetry);
89                 });
90         return true;
91     }
92 
93     @Override
onStopJob(JobParameters params)94     public boolean onStopJob(JobParameters params) {
95         LogUtil.d("DeleteExpiredJobService.onStopJob");
96         boolean shouldRetry = false;
97 
98         AdservicesJobServiceLogger.getInstance(this)
99                 .recordOnStopJob(params, MEASUREMENT_DELETE_EXPIRED_JOB_ID, shouldRetry);
100         return shouldRetry;
101     }
102 
103     /** Schedule the job. */
104     @VisibleForTesting
schedule(Context context, JobScheduler jobScheduler)105     static void schedule(Context context, JobScheduler jobScheduler) {
106         final JobInfo job =
107                 new JobInfo.Builder(
108                                 MEASUREMENT_DELETE_EXPIRED_JOB_ID,
109                                 new ComponentName(context, DeleteExpiredJobService.class))
110                         .setRequiresDeviceIdle(true)
111                         .setPeriodic(AdServicesConfig.getMeasurementDeleteExpiredJobPeriodMs())
112                         .setPersisted(true)
113                         .build();
114         jobScheduler.schedule(job);
115     }
116 
117     /**
118      * Schedule Delete Expired Job Service if it is not already scheduled
119      *
120      * @param context the context
121      * @param forceSchedule flag to indicate whether to force rescheduling the job.
122      */
scheduleIfNeeded(Context context, boolean forceSchedule)123     public static void scheduleIfNeeded(Context context, boolean forceSchedule) {
124         if (FlagsFactory.getFlags().getMeasurementJobDeleteExpiredKillSwitch()) {
125             LogUtil.e("DeleteExpiredJobService is disabled, skip scheduling");
126             return;
127         }
128 
129         final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
130         if (jobScheduler == null) {
131             LogUtil.e("JobScheduler not found");
132             return;
133         }
134 
135         final JobInfo job = jobScheduler.getPendingJob(MEASUREMENT_DELETE_EXPIRED_JOB_ID);
136         // Schedule if it hasn't been scheduled already or force rescheduling
137         if (job == null || forceSchedule) {
138             schedule(context, jobScheduler);
139             LogUtil.d("Scheduled DeleteExpiredJobService");
140         } else {
141             LogUtil.d("DeleteExpiredJobService already scheduled, skipping reschedule");
142         }
143     }
144 
skipAndCancelBackgroundJob(final JobParameters params, int skipReason)145     private boolean skipAndCancelBackgroundJob(final JobParameters params, int skipReason) {
146         final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class);
147         if (jobScheduler != null) {
148             jobScheduler.cancel(MEASUREMENT_DELETE_EXPIRED_JOB_ID);
149         }
150 
151         AdservicesJobServiceLogger.getInstance(this)
152                 .recordJobSkipped(MEASUREMENT_DELETE_EXPIRED_JOB_ID, skipReason);
153 
154         // Tell the JobScheduler that the job has completed and does not need to be rescheduled.
155         jobFinished(params, false);
156 
157         // Returning false means that this job has completed its work.
158         return false;
159     }
160 }
161