Skip to content

Commit

Permalink
Add task reconciler class, refactor Task.java
Browse files Browse the repository at this point in the history
  • Loading branch information
bjester committed Jan 9, 2024
1 parent e1f8fb5 commit cb69f18
Show file tree
Hide file tree
Showing 17 changed files with 939 additions and 381 deletions.
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.9.13
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,11 @@ private void createNotificationChannels() {

private void reconcileTasks() {
// Reconcile tasks on startup, in this main thread (blocking!)
CompletableFuture<Boolean> f = Task.reconcile(this, null);
f.whenCompleteAsync((result, throwable) -> {
if (throwable != null) {
Log.e("Kolibri", "Main thread task reconciliation failed", throwable);
} else {
Log.i("Kolibri", "Main thread task reconciliation completed");
}
});
boolean result = Task.reconcile(this, null);
if (result) {
Log.i("Kolibri", "Main thread task reconciliation completed");
} else {
Log.d("Kolibri", "Main thread task reconciliation no-op");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,13 @@ public ListenableFuture<Result> startWork() {
SettableFuture<Result> future = SettableFuture.create();
Executor executor = getBackgroundExecutor();

CompletableFuture<Boolean> f = Task.reconcile(getApplicationContext(), executor);

f.whenCompleteAsync((result, throwable) -> {
if (throwable != null) {
Log.e(TAG, "Reconcile task failed", throwable);
future.setException(throwable);
} else {
Log.i(TAG, "Reconcile task completed: " + (result ? "success" : "failure"));
future.set(result ? Result.success() : Result.failure());
}
}, executor);

boolean result = Task.reconcile(getApplicationContext(), executor);
if (!result) {
Log.e(TAG, "Failed to reconcile tasks");
future.set(Result.failure());
return future;
}
future.set(Result.success());
return future;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,6 @@ public TaskworkerWorker(
mWorker = this;
}

public static Data buildInputData(String workerArgument) {
String dataArgument = workerArgument == null ? "" : workerArgument;
Data data = new Data.Builder()
.putString(ARGUMENT_WORKER_ARGUMENT, dataArgument)
.putString(ARGUMENT_PACKAGE_NAME, "org.learningequality.Kolibri")
.putString(ARGUMENT_CLASS_NAME,
TaskworkerWorkerService.class.getName())
.build();
Log.v(TAG, "Request data: " + data.toString());
return data;
}

protected void cleanup() {
hideNotification();
mWorker = null;
Expand Down Expand Up @@ -80,4 +68,16 @@ public static void clearNotification() {
mWorker.hideNotification();
}
}

public static Data buildInputData(String workerArgument) {
String dataArgument = workerArgument == null ? "" : workerArgument;
Data data = new Data.Builder()
.putString(ARGUMENT_WORKER_ARGUMENT, dataArgument)
.putString(ARGUMENT_PACKAGE_NAME, "org.learningequality.Kolibri")
.putString(ARGUMENT_CLASS_NAME,
TaskworkerWorkerService.class.getName())
.build();
Log.v(TAG, "Request data: " + data.toString());
return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void onCreate() {
);
// Initialize the work manager
WorkManager.getInstance(getApplicationContext());
enqueueTaskReconciliation();
// enqueueTaskReconciliation();
super.onCreate();
// We could potentially remove this and leave the notification up to long-running workers
// bound to the service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package org.learningequality.Kolibri.sqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import org.learningequality.sqlite.schema.DatabaseTable;
import org.learningequality.sqlite.Database;

import java.io.File;

public class JobStorage {
public class JobStorage extends Database {
public static final String DATABASE_NAME = "job_storage.sqlite3";

public static class Jobs implements DatabaseTable {
Expand All @@ -13,13 +19,15 @@ public String getTableName() {
}

public static final StringColumn id = new StringColumn("id");
public static final StringColumn func = new StringColumn("func");
public static final IntegerColumn priority = new IntegerColumn("priority");
public static final StringColumn worker_process = new StringColumn("worker_process");
public static final StringColumn worker_thread = new StringColumn("worker_thread");
public static final StringColumn worker_extra = new StringColumn("worker_extra");
public static final StringColumn time_updated = new StringColumn("time_updated");
public static final StringColumn state = new StringColumn("state");

public enum State implements ColumnEnum<String> {
public enum State implements StringChoiceEnum {
PENDING,
QUEUED,
SCHEDULED,
Expand All @@ -35,5 +43,40 @@ public StringColumn getColumn() {
return state;
}
}

public enum Priority implements ColumnEnum<Integer> {
LOW(15),
REGULAR(10),
HIGH(5)
;

private final int value;

Priority(int val) {
this.value = val;
}

public Integer getValue() {
return this.value;
}

public IntegerColumn getColumn() {
return priority;
}


}
}

protected JobStorage(String path, int flags) {
super(DATABASE_NAME, path, flags);
}

public static JobStorage readwrite(Context context) {
File f = getDatabasePath(context, DATABASE_NAME);

return f != null
? new JobStorage(f.getPath(), SQLiteDatabase.OPEN_READWRITE)
: null;
}
}
Loading

0 comments on commit cb69f18

Please sign in to comment.