Skip to content

Commit 295ab42

Browse files
authored
Merge pull request #216 from ozer550/update-failed-task-state-in-database
synchornize database when a TaskRequest is failed
2 parents b0287d3 + 6ecc918 commit 295ab42

2 files changed

Lines changed: 46 additions & 6 deletions

File tree

python-for-android/dists/kolibri/src/main/java/org/kivy/android/PythonWorker.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
import androidx.annotation.NonNull;
88

9+
import org.learningequality.Kolibri.sqlite.JobStorage;
10+
import org.learningequality.sqlite.query.UpdateQuery;
11+
912
/**
1013
* Ideally this would be called `PythonWorkerImpl` but the name is used in the native code.
1114
*/
@@ -19,11 +22,11 @@ public class PythonWorker {
1922
private final String pythonHome;
2023
private final String pythonPath;
2124

25+
2226
public PythonWorker(@NonNull Context context, String pythonName, String workerEntrypoint) {
2327
PythonLoader.doLoad(context);
2428
this.pythonName = pythonName;
2529
this.workerEntrypoint = workerEntrypoint;
26-
2730
String appRoot = PythonUtil.getAppRoot(context);
2831
androidPrivate = appRoot;
2932
androidArgument = appRoot;
@@ -61,11 +64,18 @@ public boolean execute(String id, String arg) {
6164
serializedArg
6265
);
6366
Log.d(TAG, id + " Finished executing python work: " + res);
67+
if (res == 0) {
68+
// If the result is 0, execution was successful
69+
return true;
70+
} else {
71+
// For any result other than 0, log and treat as a failure
72+
Log.e(TAG, "Python work execution failed with result code: " + res);
73+
return false;
74+
}
6475
} catch (Exception e) {
76+
// Catch and log any exceptions, treating them as execution failures
6577
Log.e(TAG, "Error executing python work", e);
6678
return false;
6779
}
68-
69-
return res == 0;
7080
}
7181
}

python-for-android/dists/kolibri/src/main/java/org/learningequality/task/Worker.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
import androidx.work.WorkerParameters;
1010

1111
import org.kivy.android.PythonProvider;
12+
import org.learningequality.Kolibri.sqlite.JobStorage;
1213
import org.learningequality.Kolibri.task.TaskWorkerImpl;
1314
import org.learningequality.notification.Notifier;
1415
import org.learningequality.notification.NotificationRef;
16+
import org.learningequality.sqlite.query.UpdateQuery;
1517

1618
import java.util.UUID;
1719
import java.util.zip.CRC32;
@@ -25,6 +27,7 @@ abstract public class Worker extends androidx.work.Worker implements Notifier {
2527
private int lastProgressUpdateHash;
2628
private Notification lastNotification;
2729

30+
2831
public Worker(
2932
@NonNull Context context, @NonNull WorkerParameters workerParams
3033
) {
@@ -46,7 +49,6 @@ public Result doWork() {
4649
final String id = getId().toString();
4750
final String arg = getArgument();
4851
Result r;
49-
5052
Log.d(TAG, "Executing task implementation: " + getId());
5153
try (WorkerImpl<TaskWorkerImpl.Message> workerImpl = getWorkerImpl()) {
5254
workerImpl.addObserver(new Observer<TaskWorkerImpl.Message>() {
@@ -57,23 +59,51 @@ public void update(TaskWorkerImpl.Message message) {
5759
});
5860
// Provide context to PythonProvider
5961
try (PythonProvider ignored = PythonProvider.create(getApplicationContext())) {
60-
r = workerImpl.execute(id, arg) ? Result.success() : Result.failure();
62+
boolean result = workerImpl.execute(id,arg);
63+
if (!result) {
64+
if (updateTaskStatus(getArgument(), JobStorage.Jobs.State.FAILED.toString()) == 0) {
65+
Log.e(TAG, "Failed to update TaskStatus for remote Task " + getId());
66+
}
67+
}
68+
r = result ? Result.success() : Result.failure();
6169
}
6270
} catch (Exception e) {
6371
Log.e(TAG, "Error executing task implementation: " + getId(), e);
6472
r = Result.failure();
6573
}
6674
hideNotification();
67-
return r;
75+
return r;
6876
}
6977

7078
@Override
7179
public void onStopped() {
7280
Log.d(TAG, "Stopping background remote task " + getId());
81+
// Here we need to update the task in the database to be marked as failed
82+
if (updateTaskStatus(getArgument(), JobStorage.Jobs.State.CANCELED.toString()) == 0) {
83+
Log.e(TAG, "Failed to update TaskStatus for remote Task " + getId());
84+
}
7385
hideNotification();
7486
super.onStopped();
7587
}
7688

89+
protected int updateTaskStatus(String id, String stateToBeUpdatedTo) {
90+
int result = 0;
91+
try (JobStorage db = JobStorage.readwrite(getApplicationContext())) {
92+
if (db!=null) {
93+
Log.d(TAG, "Updating Task Status for job " + id);
94+
UpdateQuery q = new UpdateQuery(JobStorage.Jobs.TABLE_NAME)
95+
.where(JobStorage.Jobs.id, id)
96+
.set(JobStorage.Jobs.state, stateToBeUpdatedTo);
97+
result = q.execute(db);
98+
}
99+
Log.e(TAG, "Failed to initialize JobStorage");
100+
}catch (Exception e) {
101+
Log.e(TAG, "Error managing JobStorage", e);
102+
}
103+
return result;
104+
}
105+
106+
77107
protected Notification getLastNotification() {
78108
return lastNotification;
79109
}

0 commit comments

Comments
 (0)