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

Added inspection and removal of jobs to JobManager #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions jobqueue/src/com/path/android/jobqueue/JobManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.path.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;

/**
Expand Down Expand Up @@ -353,6 +354,50 @@ public JobStatus getJobStatus(long id, boolean isPersistent) {
return JobStatus.WAITING_READY;
}

public List<Long> getJobIds(boolean isPersistent) {
if (isPersistent) {
synchronized (persistentJobQueue) {
return persistentJobQueue.getJobIds();
}
} else {
synchronized (nonPersistentJobQueue) {
return nonPersistentJobQueue.getJobIds();
}
}
}

public BaseJob getJob(long id, boolean isPersistent) {
JobHolder holder;
if(isPersistent) {
synchronized (persistentJobQueue) {
holder = persistentJobQueue.findJobById(id);
}
} else {
synchronized (nonPersistentJobQueue) {
holder = nonPersistentJobQueue.findJobById(id);
}
}
if (holder != null) {
return holder.getBaseJob();
} else {
return null;
}
}

public void removeJob(long id, boolean isPersistent) {
JobHolder holder;
if(isPersistent) {
synchronized (persistentJobQueue) {
holder = persistentJobQueue.findJobById(id);
}
} else {
synchronized (nonPersistentJobQueue) {
holder = nonPersistentJobQueue.findJobById(id);
}
}
removeJob(holder);
}

private void removeJob(JobHolder jobHolder) {
if (jobHolder.getBaseJob().isPersistent()) {
synchronized (persistentJobQueue) {
Expand Down
7 changes: 7 additions & 0 deletions jobqueue/src/com/path/android/jobqueue/JobQueue.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.path.android.jobqueue;

import java.util.Collection;
import java.util.List;

/**
* Interface that any JobQueue should implement
Expand Down Expand Up @@ -79,4 +80,10 @@ public interface JobQueue {
*/
JobHolder findJobById(long id);

/**
* returns an array of all the job ids stored in the queue
* @return Array of the current job ids.
*/
List<Long> getJobIds();

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.path.android.jobqueue.JobQueue;

import java.util.Collection;
import java.util.List;

/**
* a class that implements {@link JobQueue} interface, wraps another {@link JobQueue} and caches
Expand Down Expand Up @@ -98,6 +99,12 @@ public JobHolder findJobById(long id) {
return delegate.findJobById(id);
}

@Override
public List<Long> getJobIds() {
return delegate.getJobIds();
}


private static class Cache {
Integer count;
DelayUntil delayUntil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.path.android.jobqueue.JobHolder;

import java.util.Collection;
import java.util.List;

/**
* An interface for Job Containers
Expand All @@ -12,6 +13,7 @@ public interface JobSet {
public JobHolder peek(Collection<String> excludeGroupIds);
public JobHolder poll(Collection<String> excludeGroupIds);
public JobHolder findById(long id);
public List<Long> getJobIds();
public boolean offer(JobHolder holder);
public boolean remove(JobHolder holder);
public void clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ public JobHolder findById(long id) {
return q0 == null ? queue1.findById(id) : q0;
}

@Override
public List<Long> getJobIds() {
List<Long> ids = queue0.getJobIds();
ids.addAll(queue1.getJobIds());
return ids;
}

/**
* simple enum to identify queues
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.path.android.jobqueue.JobHolder;
import com.path.android.jobqueue.log.JqLog;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -75,6 +77,11 @@ public JobHolder findById(long id) {
return idCache.get(id);
}

@Override
public List<Long> getJobIds() {
return new ArrayList<Long>(idCache.keySet());
}

@Override
public boolean offer(JobHolder holder) {
if(holder.getId() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public JobHolder findJobById(long id) {
return jobs.findById(id);
}

@Override
public List<Long> getJobIds() {
return jobs.getJobIds();
}

public final Comparator<JobHolder> jobComparator = new Comparator<JobHolder>() {
@Override
public int compare(JobHolder holder1, JobHolder holder2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
public class SqlHelper {

/**package**/ String FIND_BY_ID_QUERY;
String FIND_ALL_IDS_QUERY;

private SQLiteStatement insertStatement;
private SQLiteStatement insertOrReplaceStatement;
private SQLiteStatement deleteStatement;
private SQLiteStatement onJobFetchedForRunningStatement;
private SQLiteStatement countStatement;
private SQLiteStatement idsStatement;
private SQLiteStatement nextJobDelayedUntilWithNetworkStatement;
private SQLiteStatement nextJobDelayedUntilWithoutNetworkStatement;

Expand All @@ -33,6 +35,7 @@ public SqlHelper(SQLiteDatabase db, String tableName, String primaryKeyColumnNam
this.primaryKeyColumnName = primaryKeyColumnName;
this.sessionId = sessionId;
FIND_BY_ID_QUERY = "SELECT * FROM " + tableName + " WHERE " + DbOpenHelper.ID_COLUMN.columnName + " = ?";
FIND_ALL_IDS_QUERY = "Select " + DbOpenHelper.ID_COLUMN.columnName + " FROM " + tableName;
}

public static String create(String tableName, Property primaryKey, Property... properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* Persistent Job Queue that keeps its data in an sqlite database.
Expand Down Expand Up @@ -178,6 +180,22 @@ public JobHolder findJobById(long id) {
}
}

@Override
public List<Long> getJobIds() {
ArrayList<Long> ids = new ArrayList<Long>();
Cursor cursor = db.rawQuery(sqlHelper.FIND_ALL_IDS_QUERY, null);
try {
if (cursor.moveToFirst()) {
do {
ids.add(cursor.getLong(0));
} while (cursor.moveToNext());
}
} finally {
cursor.close();
}
return ids;
}

/**
* {@inheritDoc}
*/
Expand Down