Repository: MatterMiners/tardis

Issue:
TARDIS crashing due to Drones in `ShutdownState` having `remote_resource_uuid=None`
It seems that #247 introduced a bug in TARDIS. I have spotted two cases on the Compute4PUNCH infrastructure, where drones in `ShutdownState` do have a `remote_resource_uuid=None`. Which leads to continously crashing `TARDIS`.

```
cobald.runtime.tardis.resources.dronestates: 2022-05-25 10:40:03 Drone {'site_name': 'GridKa', 'machine_type': 'eightcore', 'obs_machine_meta_data_translation_mapping': {'Cores': 1, 'Memory': 1024, 'Disk': 1048576}, 'remote_resource_uui
d': None, 'created': datetime.datetime(2022, 5, 21, 15, 32, 27, 240837), 'updated': datetime.datetime(2022, 5, 21, 15, 55, 31, 606878), 'drone_uuid': 'gridka-d61765a93c'} in ShutDownState
....
Traceback (most recent call last):
  File "/home/compute4punch/venv/lib64/python3.6/site-packages/tardis/adapters/sites/htcondor.py", line 326, in handle_exceptions
    yield
  File "/home/compute4punch/venv/lib64/python3.6/site-packages/tardis/agents/siteagent.py", line 54, in resource_status
    return await self._site_adapter.resource_status(resource_attributes)
  File "/home/compute4punch/venv/lib64/python3.6/site-packages/tardis/adapters/sites/htcondor.py", line 285, in resource_status
    resource_uuid = _job_id(resource_attributes.remote_resource_uuid)
  File "/home/compute4punch/venv/lib64/python3.6/site-packages/tardis/adapters/sites/htcondor.py", line 33, in _job_id
    return resource_uuid if "." in resource_uuid else f"{resource_uuid}.0"
TypeError: argument of type 'NoneType' is not iterable
```

Discussion:

Patch:
diff --git a/docs/source/changelog.rst b/docs/source/changelog.rst
index 695e517..633e532 100644
--- a/docs/source/changelog.rst
+++ b/docs/source/changelog.rst
@@ -1,4 +1,4 @@
-.. Created by changelog.py at 2022-05-11, command
+.. Created by changelog.py at 2022-05-25, command
    '/Users/giffler/.cache/pre-commit/repor6pnmwlm/py_env-default/bin/changelog docs/source/changes compile --output=docs/source/changelog.rst'
    based on the format of 'https://keepachangelog.com/'
 
@@ -6,7 +6,7 @@
 CHANGELOG
 #########
 
-[Unreleased] - 2022-05-11
+[Unreleased] - 2022-05-25
 =========================
 
 Added
@@ -25,6 +25,7 @@ Fixed
 -----
 
 * Unique constraints in database schema have been fixed to allow same machine_type and remote_resource_uuid on multiple sites
+* Update the remote_resource_uuid in sqlite registry on a each update
 
 [0.6.0] - 2021-08-09
 ====================
diff --git a/docs/source/changes/249_fix_sqlite_registry_remote_uuid_update.yaml b/docs/source/changes/249_fix_sqlite_registry_remote_uuid_update.yaml
new file mode 100644
index 0000000..33627cb
--- /dev/null
+++ b/docs/source/changes/249_fix_sqlite_registry_remote_uuid_update.yaml
@@ -0,0 +1,10 @@
+category: fixed
+summary: "Update the remote_resource_uuid in sqlite registry on a each update"
+description: |
+  The change drone state initialisation update revealed a bug in TARDIS. The ``remote_resource_uuid`` in the 
+  ``SqliteRegistry`` plugin is not updated at all. As a result, TARDIS keeps crashing on restarts due to the missing 
+  ``remote_resource_uuid`` until the DB has been removed.
+pull_requests:
+  - 249
+issues:
+  - 248
diff --git a/tardis/plugins/sqliteregistry.py b/tardis/plugins/sqliteregistry.py
index 7e8d2a3..e3030f4 100644
--- a/tardis/plugins/sqliteregistry.py
+++ b/tardis/plugins/sqliteregistry.py
@@ -190,6 +190,7 @@ class SqliteRegistry(Plugin):
 
     async def update_resource(self, bind_parameters: Dict) -> None:
         sql_query = """UPDATE Resources SET updated = :updated,
+        remote_resource_uuid = :remote_resource_uuid,
         state_id = (SELECT state_id FROM ResourceStates WHERE state = :state)
         WHERE drone_uuid = :drone_uuid
         AND site_id = (SELECT site_id FROM Sites WHERE site_name = :site_name)"""


Test patch:
diff --git a/tests/plugins_t/test_sqliteregistry.py b/tests/plugins_t/test_sqliteregistry.py
index 9649e70..ae2585c 100644
--- a/tests/plugins_t/test_sqliteregistry.py
+++ b/tests/plugins_t/test_sqliteregistry.py
@@ -1,7 +1,6 @@
 import logging
 
 from tardis.resources.dronestates import BootingState
-from tardis.resources.dronestates import IntegrateState
 from tardis.resources.dronestates import RequestState, DownState
 from tardis.interfaces.state import State
 from tardis.plugins.sqliteregistry import SqliteRegistry
@@ -27,7 +26,7 @@ class TestSqliteRegistry(TestCase):
         cls.test_machine_type = "MyGreatTestMachineType"
         cls.tables_in_db = {"MachineTypes", "Resources", "ResourceStates", "Sites"}
         cls.test_resource_attributes = {
-            "remote_resource_uuid": "bf85022b-fdd6-42b1-932d-086c288d4755",
+            "remote_resource_uuid": None,
             "drone_uuid": f"{cls.test_site_name}-07af52405e",
             "site_name": cls.test_site_name,
             "machine_type": cls.test_machine_type,
@@ -66,7 +65,7 @@ class TestSqliteRegistry(TestCase):
         cls.test_updated_notify_result = (
             cls.test_updated_resource_attributes["remote_resource_uuid"],
             cls.test_updated_resource_attributes["drone_uuid"],
-            str(IntegrateState()),
+            str(BootingState()),
             cls.test_updated_resource_attributes["site_name"],
             cls.test_updated_resource_attributes["machine_type"],
             str(cls.test_updated_resource_attributes["created"]),
@@ -220,7 +219,7 @@ class TestSqliteRegistry(TestCase):
 
         run_async(
             self.registry.notify,
-            IntegrateState(),
+            BootingState(),
             self.test_updated_resource_attributes,
         )