@Test public void verifyGetAllJobResultsReturnsAllResults() { JobExecutor executor = new JobExecutor(); executor.markJobAsFinished(createJobMock(1)); executor.markJobAsFinished(createJobMock(2)); assertThat(executor.getAllJobs()).hasSize(2); assertThat(executor.getAllJobResults().size()).isEqualTo(2); }
@VisibleForTesting /*package*/ synchronized void markJobAsFinished(Job job) { int id = job.getParams().getId(); mJobs.remove(id); cleanUpRoutine(mFinishedJobsCache); mFinishedJobResults.put(id, job.getResult()); mFinishedJobsCache.put(id, new WeakReference<>(job)); }
public synchronized Set<Job> getAllJobs() { return getAllJobsForTag(null); }
@Test public void verifyCleanUpRoutine() { LruCache<Integer, WeakReference<Job>> cache = new LruCache<>(20); cache.put(1, new WeakReference<>(createJobMock(1))); cache.put(2, new WeakReference<Job>(null)); new JobExecutor().cleanUpRoutine(cache); assertThat(cache.size()).isEqualTo(1); }
/** * Finished jobs are kept in memory until the garbage collector cleans them up. This method returns * the results of all finished jobs even after they have been cleaned up. However, neither finished jobs * nor their results are restored after the app has been relaunched. * * @return The results of all finished jobs. They key is the corresponding job ID. */ @NonNull public SparseArray<Job.Result> getAllJobResults() { return mJobExecutor.getAllJobResults(); }
@Override public Job.Result call() throws Exception { try { // just in case something was blocking and the wake lock is no longer acquired WakeLockUtil.acquireWakeLock(mJob.getContext(), mWakeLock, WAKE_LOCK_TIMEOUT); return runJob(); } finally { markJobAsFinished(mJob); if (mWakeLock == null || !mWakeLock.isHeld()) { CAT.w("Wake lock was not held after job %s was done. The job took too long to complete. This could have unintended side effects on your app.", mJob); } WakeLockUtil.releaseWakeLock(mWakeLock); } }
/** * Jobs are cached in memory even if they already have finished. But finished jobs are never * restored after the app has relaunched. Since finished jobs could cause memory leaks, they wrapped * inside of a {@link WeakReference} and can be removed from memory. If you need to know the results * of finished jobs or whether a job has been run, you can call {@link #getAllJobResults()}. * * @return A duplicate {@link Set} containing all running and cached finished jobs or an empty set. * Never returns {@code null}. The set may be modified without direct effects to the actual * backing store. */ @NonNull public Set<Job> getAllJobs() { return mJobExecutor.getAllJobs(); }
private JobManager(final Context context) { mContext = context; mJobCreatorHolder = new JobCreatorHolder(); mJobExecutor = new JobExecutor(); if (!JobConfig.isSkipJobReschedule()) { JobRescheduleService.startService(mContext); } mJobStorageLatch = new CountDownLatch(1); new Thread("AndroidJob-storage-init") { @Override public void run() { mJobStorage = new JobStorage(context); mJobStorageLatch.countDown(); } }.start(); }
/** * Jobs are cached in memory even if they already have finished. But finished jobs are never * restored after the app has launched. Since finished jobs could cause memory leaks, they wrapped * inside of a {@link WeakReference} and can be removed from memory. If you need to know the results * of finished jobs or whether a job has been run, you can call {@link #getAllJobResults()}. * * @param jobId The unique ID of the running or finished {@link Job}. * @return The {@link Job} if it's running or has been finished and is still cached. Returns * {@code null} otherwise. */ public Job getJob(int jobId) { return mJobExecutor.getJob(jobId); }
public void markStarting(@NonNull JobRequest request) { mJobManager.getJobExecutor().markJobRequestStarting(request); }
Future<Job.Result> future = jobExecutor.execute(mContext, request, job, transientExtras); if (future == null) { return Job.Result.FAILURE;
/** * Finished jobs are kept in memory until the garbage collector cleans them up. This method returns * the results of all finished jobs even after they have been cleaned up. However, neither finished jobs * nor their results are restored after the app has been relaunched. * * @return The results of all finished jobs. They key is the corresponding job ID. */ @NonNull public SparseArray<Job.Result> getAllJobResults() { return mJobExecutor.getAllJobResults(); }
@Override public Job.Result call() throws Exception { try { // just in case something was blocking and the wake lock is no longer acquired WakeLockUtil.acquireWakeLock(mJob.getContext(), mWakeLock, WAKE_LOCK_TIMEOUT); return runJob(); } finally { markJobAsFinished(mJob); if (mWakeLock == null || !mWakeLock.isHeld()) { CAT.w("Wake lock was not held after job %s was done. The job took too long to complete. This could have unintended side effects on your app.", mJob); } WakeLockUtil.releaseWakeLock(mWakeLock); } }
/** * Jobs are cached in memory even if they already have finished. But finished jobs are never * restored after the app has relaunched. Since finished jobs could cause memory leaks, they wrapped * inside of a {@link WeakReference} and can be removed from memory. If you need to know the results * of finished jobs or whether a job has been run, you can call {@link #getAllJobResults()}. * * @return A duplicate {@link Set} containing all running and cached finished jobs or an empty set. * Never returns {@code null}. The set may be modified without direct effects to the actual * backing store. */ @NonNull public Set<Job> getAllJobs() { return mJobExecutor.getAllJobs(); }
private JobManager(Context context) { mContext = context; mJobCreatorHolder = new JobCreatorHolder(); mJobStorage = new JobStorage(context); mJobExecutor = new JobExecutor(); if (!JobConfig.isSkipJobReschedule()) { JobRescheduleService.startService(mContext); } }
/** * Jobs are cached in memory even if they already have finished. But finished jobs are never * restored after the app has launched. Since finished jobs could cause memory leaks, they wrapped * inside of a {@link WeakReference} and can be removed from memory. If you need to know the results * of finished jobs or whether a job has been run, you can call {@link #getAllJobResults()}. * * @param jobId The unique ID of the running or finished {@link Job}. * @return The {@link Job} if it's running or has been finished and is still cached. Returns * {@code null} otherwise. */ public Job getJob(int jobId) { return mJobExecutor.getJob(jobId); }
public void markStarting(@NonNull JobRequest request) { mJobManager.getJobExecutor().markJobRequestStarting(request); }
Future<Job.Result> future = jobExecutor.execute(mContext, request, job, transientExtras); if (future == null) { return Job.Result.FAILURE;
@VisibleForTesting /*package*/ synchronized void markJobAsFinished(Job job) { int id = job.getParams().getId(); mJobs.remove(id); cleanUpRoutine(mFinishedJobsCache); mFinishedJobResults.put(id, job.getResult()); mFinishedJobsCache.put(id, new WeakReference<>(job)); }
/** * Jobs are cached in memory even if they already have finished. But finished jobs are never * restored after the app has relaunched. Since finished jobs could cause memory leaks, they wrapped * inside of a {@link WeakReference} and can be removed from memory. If you need to know the results * of finished jobs or whether a job has been run, you can call {@link #getAllJobResults()}. * * @param tag The tag of the running or finished jobs. * @return A duplicate {@link Set} containing all running and cached finished jobs associated with * this tag or an empty set. Never returns {@code null}. The set may be modified without direct * effects to the actual backing store. */ @NonNull public Set<Job> getAllJobsForTag(@NonNull String tag) { return mJobExecutor.getAllJobsForTag(tag); }