99import androidx .work .WorkerParameters ;
1010
1111import org .kivy .android .PythonProvider ;
12+ import org .learningequality .Kolibri .sqlite .JobStorage ;
1213import org .learningequality .Kolibri .task .TaskWorkerImpl ;
1314import org .learningequality .notification .Notifier ;
1415import org .learningequality .notification .NotificationRef ;
16+ import org .learningequality .sqlite .query .UpdateQuery ;
1517
1618import java .util .UUID ;
1719import 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