diff --git a/apps/terminal/models.py b/apps/terminal/models.py index 91ac8e052..5829fa2e1 100644 --- a/apps/terminal/models.py +++ b/apps/terminal/models.py @@ -127,7 +127,7 @@ class Session(models.Model): is_finished = models.BooleanField(default=False) has_replay = models.BooleanField(default=False, verbose_name=_("Replay")) has_command = models.BooleanField(default=False, verbose_name=_("Command")) - terminal = models.ForeignKey(Terminal, null=True, on_delete=models.CASCADE) + terminal = models.ForeignKey(Terminal, null=True, on_delete=models.SET_NULL) date_last_active = models.DateTimeField(verbose_name=_("Date last active"), default=timezone.now) date_start = models.DateTimeField(verbose_name=_("Date start"), db_index=True) date_end = models.DateTimeField(verbose_name=_("Date end"), null=True) @@ -148,7 +148,7 @@ class Task(models.Model): id = models.UUIDField(default=uuid.uuid4, primary_key=True) name = models.CharField(max_length=128, choices=NAME_CHOICES, verbose_name=_("Name")) args = models.CharField(max_length=1024, verbose_name=_("Args")) - terminal = models.ForeignKey(Terminal, null=True, on_delete=models.CASCADE) + terminal = models.ForeignKey(Terminal, null=True, on_delete=models.SET_NULL) is_finished = models.BooleanField(default=False) date_created = models.DateTimeField(auto_now_add=True) date_finished = models.DateTimeField(null=True) diff --git a/apps/terminal/tasks.py b/apps/terminal/tasks.py index 89f9cc5cc..e267b30b7 100644 --- a/apps/terminal/tasks.py +++ b/apps/terminal/tasks.py @@ -31,6 +31,6 @@ def delete_terminal_status_period(): def clean_orphan_session(): active_sessions = Session.objects.filter(is_finished=False) for session in active_sessions: - if not session.terminal.is_active: + if not session.terminal or not session.terminal.is_active: session.is_finished = True session.save()