Skip to content

Commit

Permalink
Cleanup and preparation for worker context reading.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtibbles committed Feb 10, 2023
1 parent 1d216bb commit 119c20a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
14 changes: 14 additions & 0 deletions src/android_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ def is_service_context():
return "PYTHON_SERVICE_ARGUMENT" in os.environ


def is_worker_context():
return "PYTHON_WORKER_ARGUMENT" in os.environ


def get_worker():
assert (
is_worker_context()
), "Cannot get a worker, as we are not in a worker context."
PythonWorker = autoclass("org.kivy.android.PythonWorker")
return PythonWorker.mWorker


def get_service():
assert (
is_service_context()
Expand Down Expand Up @@ -57,6 +69,8 @@ def get_activity():
if ACTIVITY is None:
if is_service_context():
ACTIVITY = cast("android.app.Service", get_service())
elif is_worker_context():
ACTIVITY = cast("androidx.work.Worker", get_worker())
else:
PythonActivity = autoclass("org.kivy.android.PythonActivity")
ACTIVITY = PythonActivity.mActivity
Expand Down
30 changes: 17 additions & 13 deletions src/kolibri_tasks.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
from datetime import datetime
from datetime import timedelta

from android_utils import PythonActivity
from android_utils import get_activity
from jnius import autoclass


WorkManager = autoclass("androidx.work.WorkManager")
OneTimeWorkRequestBuilder = autoclass("androidx.work.OneTimeWorkRequest$Builder")
PeriodicWorkRequestBuilder = autoclass("androidx.work.PeriodicWorkRequest$Builder")
BackoffPolicy = autoclass("androidx.work.BackoffPolicy")
ExistingWorkPolicy = autoclass("androidx.work.ExistingWorkPolicy")
ExistingPeriodicWorkPolicy = autoclass("androidx.work.ExistingPeriodicWorkPolicy")
TimeUnit = autoclass("java.util.concurrent$TimeUnit")

TaskWorker = autoclass("org.learningequality.Kolibri.TaskWorker")


def get_work_manager():
return WorkManager.getInstance(PythonActivity.mActivity)
WorkManager = autoclass("androidx.work.WorkManager")
return WorkManager.getInstance(get_activity().mActivity)


def queue_task(
Expand All @@ -30,6 +20,20 @@ def queue_task(
keep=True,
):
if id:
OneTimeWorkRequestBuilder = autoclass(
"androidx.work.OneTimeWorkRequest$Builder"
)
PeriodicWorkRequestBuilder = autoclass(
"androidx.work.PeriodicWorkRequest$Builder"
)
BackoffPolicy = autoclass("androidx.work.BackoffPolicy")
ExistingWorkPolicy = autoclass("androidx.work.ExistingWorkPolicy")
ExistingPeriodicWorkPolicy = autoclass(
"androidx.work.ExistingPeriodicWorkPolicy"
)
TimeUnit = autoclass("java.util.concurrent.TimeUnit")

TaskWorker = autoclass("org.learningequality.Kolibri.TaskworkerWorker")
id = str(id)
data = TaskWorker.buildInputData(id)

Expand Down
1 change: 0 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def SERVING(self, port):
"http://127.0.0.1:{port}".format(port=port) + interface.get_initialize_url()
)
loadUrl(start_url)
start_service("workers")


logging.info("Initializing Kolibri and running any upgrade routines")
Expand Down

0 comments on commit 119c20a

Please sign in to comment.