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.federatedcompute.services.sharedlibrary.spe;
18 
19 import static com.android.federatedcompute.services.common.FederatedComputeJobInfo.DELETE_EXPIRED_JOB_ID;
20 
21 import android.app.job.JobParameters;
22 
23 import com.android.adservices.shared.spe.framework.AbstractJobService;
24 import com.android.adservices.shared.spe.framework.JobServiceFactory;
25 import com.android.federatedcompute.internal.util.LogUtil;
26 import com.android.federatedcompute.services.common.Flags;
27 import com.android.federatedcompute.services.common.FlagsFactory;
28 import com.android.internal.annotations.VisibleForTesting;
29 
30 /** The FederatedCompute's implementation of {@link AbstractJobService}. */
31 public final class FederatedComputeJobService extends AbstractJobService {
32     private static final String TAG = FederatedComputeJobService.class.getSimpleName();
33 
34     @Override
getJobServiceFactory()35     protected JobServiceFactory getJobServiceFactory() {
36         return FederatedComputeJobServiceFactory.getInstance(this);
37     }
38 
39     @Override
onStartJob(JobParameters params)40     public boolean onStartJob(JobParameters params) {
41         int jobId = params.getJobId();
42 
43         // Switch to the legacy job scheduling if SPE is disabled. Since job ID remains the same,
44         // the scheduled job will be cancelled and rescheduled with the legacy method.
45         //
46         // And after the job is rescheduled, it will execute once instantly so don't log execution
47         // stats here.
48         if (shouldRescheduleWithLegacyMethod(jobId)) {
49             LogUtil.d(
50                     TAG,
51                     "SPE is disabled. Reschedule SPE job instance of jobId=%d with its legacy"
52                             + " JobService scheduling method.",
53                     jobId);
54 
55             FederatedComputeJobServiceFactory factory =
56                     (FederatedComputeJobServiceFactory) getJobServiceFactory();
57             factory.rescheduleJobWithLegacyMethod(this, jobId);
58 
59             return false;
60         }
61 
62         return super.onStartJob(params);
63     }
64 
65     // Determine whether we should cancel and reschedule current job with the legacy JobService
66     // class. It could happen when SPE has a production issue.
67     //
68     // The first batch job to migrate is,
69     // - DeleteExpiredJobService, job ID = 1001.
70     @VisibleForTesting
shouldRescheduleWithLegacyMethod(int jobId)71     boolean shouldRescheduleWithLegacyMethod(int jobId) {
72         Flags flags = FlagsFactory.getFlags();
73 
74         if (jobId == DELETE_EXPIRED_JOB_ID && !flags.getSpePilotJobEnabled()) {
75             return true;
76         }
77 
78         return false;
79     }
80 }
81