Repository: learningequality/kolibri
Issue: Tasks don't complete in Device - Channels module

<!--
Instructions:
 * Fill out the sections below, replace …'s with information about your issue
 * Use the 'preview' function above this text box to verify formatting before submitting
-->

### Observed behavior
<!--
Description of the behavior that was observed, including screenshots or other references when applicable
-->
Tasks are no longer completing after an abrupt shutdown (power to the server unplugged) while an update task was ongoing.
The tasks were completing and had even cleared them prior to the abrupt shutdown.

### Expected behavior
<!--
Description of what behavior was expected but did not occur
-->
Tasks should complete.

### User-facing consequences
<!--
Implications and real-world consequences for learners, coaches, admins, and other users of the application
-->
Can't accomplish any task in the Device -> Channels module.

### Errors and logs
<!--
Relevant logs from:
 * the command line
 * ~/.kolibri/logs/kolibri.txt
 * the browser console

Please wrap errors in triple backticks for clean formatting like this:
```
01:10 info: something happened
01:12 error: something bad happened
```
-->
Herein are attached the logs of the last few days ever since the problem started.
[kolibri-2020-01-25.txt](https://github.com/learningequality/kolibri/files/4133387/kolibri-2020-01-25.txt)
[kolibri-2020-01-26.txt](https://github.com/learningequality/kolibri/files/4133388/kolibri-2020-01-26.txt)
[kolibri-2020-01-27.txt](https://github.com/learningequality/kolibri/files/4133389/kolibri-2020-01-27.txt)
[kolibri-2020-01-23.txt](https://github.com/learningequality/kolibri/files/4133390/kolibri-2020-01-23.txt)
[kolibri-2020-01-24.txt](https://github.com/learningequality/kolibri/files/4133391/kolibri-2020-01-24.txt)


### Steps to reproduce
<!--
Precise steps that someone else can follow in order to see this behavior
-->

1. Start a channel content update task
2. Simulate an abrupt shutdown of Kolibri while the task is still underway (Unplug the device)
3. Start device and Kolibri
4. No new tasks shall complete thereafter

### Context
<!--
Tell us about your environment, including:
 * Kolibri version
 * Operating system
 * Browser
-->

Kolibri server - 0.3.6
Kolibri - 0.13.0
Ubuntu 18.04.3


Changed files:
kolibri/deployment/default/cache.py, kolibri/utils/cli.py

diff --git a/kolibri/deployment/default/cache.py b/kolibri/deployment/default/cache.py
index 3649b1cb2a..14354f33c7 100644
--- a/kolibri/deployment/default/cache.py
+++ b/kolibri/deployment/default/cache.py
@@ -14,13 +14,21 @@ cache_options = OPTIONS["Cache"]
 pickle_protocol = OPTIONS["Python"]["PICKLE_PROTOCOL"]
 
 diskcache_location = os.path.join(KOLIBRI_HOME, "process_cache")
-try:
-    diskcache_cache = Cache(diskcache_location, disk_pickle_protocol=pickle_protocol)
-except DatabaseError:
+
+
+def recreate_cache():
     shutil.rmtree(diskcache_location, ignore_errors=True)
     os.mkdir(diskcache_location)
     diskcache_cache = Cache(diskcache_location, disk_pickle_protocol=pickle_protocol)
-diskcache_cache.close()
+    diskcache_cache.close()
+
+
+try:
+    diskcache_cache = Cache(diskcache_location, disk_pickle_protocol=pickle_protocol)
+    diskcache_cache.close()
+except DatabaseError:
+    recreate_cache()
+
 # Default to LocMemCache, as it has the simplest configuration
 default_cache = {
     "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
diff --git a/kolibri/utils/cli.py b/kolibri/utils/cli.py
index 3bda6f0c13..db2b2b6732 100644
--- a/kolibri/utils/cli.py
+++ b/kolibri/utils/cli.py
@@ -36,6 +36,7 @@ from .system import become_daemon
 from kolibri.core.deviceadmin.utils import IncompatibleDatabase
 from kolibri.core.upgrade import matches_version
 from kolibri.core.upgrade import run_upgrades
+from kolibri.deployment.default.cache import recreate_cache
 from kolibri.plugins import config
 from kolibri.plugins import DEFAULT_PLUGINS
 from kolibri.plugins.utils import autoremove_unavailable_plugins
@@ -433,6 +434,7 @@ def start(port, background):
 
     # Clear old sessions up
     call_command("clearsessions")
+    recreate_cache()
 
     # On Mac, Python crashes when forking the process, so prevent daemonization until we can figure out
     # a better fix. See https://github.com/learningequality/kolibri/issues/4821
@@ -586,6 +588,7 @@ def services(port, background):
     """
 
     create_startup_lock(None)
+    recreate_cache()
 
     logger.info("Starting Kolibri background services")