Repository: pulp/pulpcore

Issue:
logging_cid in task response is an empty string
Sometimes the logging_cid in the task response is an empty string:

```
        {
            "child_tasks": [],
            "created_resources": [],
            "error": null,
            "finished_at": "2022-07-25T21:32:09.435330Z",
            "logging_cid": "",
            "name": "pulpcore.app.tasks.telemetry.post_telemetry",
            "parent_task": null,
            "progress_reports": [],
            "pulp_created": "2022-07-25T21:32:09.383372Z",
            "pulp_href": "/pulp/api/v3/tasks/7f4c54d6-fcda-4006-9da1-3c6910a18171/",
            "reserved_resources_record": [],
            "started_at": "2022-07-25T21:32:09.419785Z",
            "state": "completed",
            "task_group": null,
            "worker": "/pulp/api/v3/workers/c609a0d5-45cc-4756-af3c-4b50cd7520b1/"
        }
```

Since logging_cid represents a uuid, I'd expect it to be null or a uuid. I don't think an empty string makes sense. It also makes our code that parses out uuid more complex because we have handle the possibility of empty strings.


Discussion:

Resolving patch:
diff --git a/pulpcore/app/migrations/0096_alter_task_logging_cid.py b/pulpcore/app/migrations/0096_alter_task_logging_cid.py
new file mode 100644
--- /dev/null
+++ b/pulpcore/app/migrations/0096_alter_task_logging_cid.py
@@ -0,0 +1,31 @@
+# Generated by Django 3.2.15 on 2022-10-18 09:51
+
+from django.db import migrations, models
+from django_guid.utils import generate_guid
+
+
+def migrate_empty_string_logging_cid(apps, schema_editor):
+
+    Task = apps.get_model("core", "Task")
+
+    for task in Task.objects.filter(logging_cid=""):
+        task.logging_cid = generate_guid()
+        task.save()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('core', '0095_artifactdistribution'),
+    ]
+
+    operations = [
+        migrations.RunPython(
+            migrate_empty_string_logging_cid, reverse_code=migrations.RunPython.noop
+        ),
+        migrations.AlterField(
+            model_name='task',
+            name='logging_cid',
+            field=models.TextField(db_index=True),
+        ),
+    ]
diff --git a/pulpcore/app/models/task.py b/pulpcore/app/models/task.py
--- a/pulpcore/app/models/task.py
+++ b/pulpcore/app/models/task.py
@@ -176,7 +176,7 @@ class Task(BaseModel, AutoAddObjPermsMixin):
 
     state = models.TextField(choices=TASK_CHOICES)
     name = models.TextField()
-    logging_cid = models.TextField(db_index=True, default="")
+    logging_cid = models.TextField(db_index=True)
 
     started_at = models.DateTimeField(null=True)
     finished_at = models.DateTimeField(null=True)
diff --git a/pulpcore/tasking/tasks.py b/pulpcore/tasking/tasks.py
--- a/pulpcore/tasking/tasks.py
+++ b/pulpcore/tasking/tasks.py
@@ -4,7 +4,8 @@
 from django.db import transaction, connection
 from django.db.models import Model, Q
 from django.utils import timezone
-from django_guid import get_guid
+from django_guid import get_guid, set_guid
+from django_guid.utils import generate_guid
 
 from pulpcore.app.models import Task, TaskSchedule
 from pulpcore.app.util import get_url
@@ -87,7 +88,7 @@ def dispatch(
     with transaction.atomic():
         task = Task.objects.create(
             state=TASK_STATES.WAITING,
-            logging_cid=(get_guid() or ""),
+            logging_cid=(get_guid()),
             task_group=task_group,
             name=func,
             args=args,
@@ -116,6 +117,7 @@ def dispatch_scheduled_tasks():
                 while task_schedule.next_dispatch < now:
                     # Do not schedule in the past
                     task_schedule.next_dispatch += task_schedule.dispatch_interval
+            set_guid(generate_guid())
             with transaction.atomic():
                 task_schedule.last_task = dispatch(
                     task_schedule.task_name,


Test patch: