# Path: core/consts.py
# IN_PROGRESS = 'in_progress'
# COMPLETED = 'completed'
# STOPPED = 'stopped'
# FAIL = 'fail'
# WAIT = 'wait'
# STATUSES = [IN_PROGRESS, COMPLETED, STOPPED, FAIL, WAIT]
# STATUS_CHOICES = [(status, status) for status in STATUSES]
# NOT_RUN_STATUSES = [COMPLETED, STOPPED, FAIL]
# RUN_STATUSES = [IN_PROGRESS, WAIT]
# VERBOSE_CHOICES = (
#     ('', 'silent'),
#     ('v', 'standard'),
#     ('vv', 'detail'),
#     ('vvv', 'very detail'),
#     ('vvvv', 'all'),
# )
# 
# Path: core/models.py
# def create_auth_token(sender, instance=None, created=False, **kwargs):
# def validate_cron(value):
#     def __init__(self, *args, **kwargs):
#     def formfield(self, **kwargs):
#     def validate(self, value, model_instance):
#     def get_actual_hosts(self) -> models.QuerySet:
#     def get_playbook_name(self) -> str:
#     def get_hosts_without_groups(self) -> models.QuerySet:
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def get_vars(self):
#     def __str__(self):
#     def __str__(self):
#     def create_task(self, user, is_automatically_created=False, repeat_number=0):
#     def have_uncompleted_task(self):
#     def __str__(self):
#     def get_ansible_command(self):
#     def get_duration(self) -> datetime.timedelta:
#     def stop(self):
#     def __str__(self):
# class CronField(models.CharField):
# class TaskOperationsMixin:
# class Variable(models.Model):
#     class Meta:
# class HostGroup(models.Model):
#     class Meta:
# class Host(models.Model):
#     class Meta:
# class AnsibleUser(models.Model):
#     class Meta:
# class TaskTemplate(TaskOperationsMixin, models.Model):
#     class Meta:
# class RepeatSetting(models.Model):
# class Task(TaskOperationsMixin, models.Model):
#     class Meta:
# class TaskLog(models.Model):
#     class Meta:


import os
import signal
import traceback
import asyncio
import core.datatools.ansible
from time import sleep
from multiprocessing import Process
from asyncio.subprocess import PIPE, create_subprocess_shell
from django.conf import settings
from core import consts
from core import models
        from django.db import connection
        from django.db import connection
        """ Tasks monitoring and check status"""
        while True:
            self.check_in_progress_tasks()
            self.start_waiting_tasks()
            sleep(1)

    def check_in_progress_tasks(self):
        tasks = models.Task.objects.filter(status=consts.IN_PROGRESS, pid__isnull=False)
        not_run_tasks = []
        for task in tasks:
            if not task.pid:
                task.logs.create(
                    status=consts.FAIL,
                    message='Error "pid is None"',
                )
                continue

            if not self._is_process_run(task.pid):
                not_run_tasks.append(task)

        for task in not_run_tasks:
            task.status = consts.FAIL
            task.save()

            task.logs.create(
                status=consts.FAIL,
                message='Task with pid %s is not running' % task.pid
            )

    def start_waiting_tasks(self):

# Path: core/consts.py
# IN_PROGRESS = 'in_progress'
# COMPLETED = 'completed'
# STOPPED = 'stopped'
# FAIL = 'fail'
# WAIT = 'wait'
# STATUSES = [IN_PROGRESS, COMPLETED, STOPPED, FAIL, WAIT]
# STATUS_CHOICES = [(status, status) for status in STATUSES]
# NOT_RUN_STATUSES = [COMPLETED, STOPPED, FAIL]
# RUN_STATUSES = [IN_PROGRESS, WAIT]
# VERBOSE_CHOICES = (
#     ('', 'silent'),
#     ('v', 'standard'),
#     ('vv', 'detail'),
#     ('vvv', 'very detail'),
#     ('vvvv', 'all'),
# )
# 
# Path: core/models.py
# def create_auth_token(sender, instance=None, created=False, **kwargs):
# def validate_cron(value):
#     def __init__(self, *args, **kwargs):
#     def formfield(self, **kwargs):
#     def validate(self, value, model_instance):
#     def get_actual_hosts(self) -> models.QuerySet:
#     def get_playbook_name(self) -> str:
#     def get_hosts_without_groups(self) -> models.QuerySet:
#     def __str__(self):
#     def __str__(self):
#     def __str__(self):
#     def get_vars(self):
#     def __str__(self):
#     def __str__(self):
#     def create_task(self, user, is_automatically_created=False, repeat_number=0):
#     def have_uncompleted_task(self):
#     def __str__(self):
#     def get_ansible_command(self):
#     def get_duration(self) -> datetime.timedelta:
#     def stop(self):
#     def __str__(self):
# class CronField(models.CharField):
# class TaskOperationsMixin:
# class Variable(models.Model):
#     class Meta:
# class HostGroup(models.Model):
#     class Meta:
# class Host(models.Model):
#     class Meta:
# class AnsibleUser(models.Model):
#     class Meta:
# class TaskTemplate(TaskOperationsMixin, models.Model):
#     class Meta:
# class RepeatSetting(models.Model):
# class Task(TaskOperationsMixin, models.Model):
#     class Meta:
# class TaskLog(models.Model):
#     class Meta:


# Path: core/datatools/tasks.py
import os
import signal
import traceback
import asyncio
import core.datatools.ansible
from time import sleep
from multiprocessing import Process
from asyncio.subprocess import PIPE, create_subprocess_shell
from django.conf import settings
from core import consts
from core import models
        from django.db import connection
        from django.db import connection

        """ Tasks monitoring and check status"""
        while True:
            self.check_in_progress_tasks()
            self.start_waiting_tasks()
            sleep(1)

    def check_in_progress_tasks(self):
        tasks = models.Task.objects.filter(status=consts.IN_PROGRESS, pid__isnull=False)
        not_run_tasks = []
        for task in tasks:
            if not task.pid:
                task.logs.create(
                    status=consts.FAIL,
                    message='Error "pid is None"',
                )
                continue

            if not self._is_process_run(task.pid):
                not_run_tasks.append(task)

        for task in not_run_tasks:
            task.status = consts.FAIL
            task.save()

            task.logs.create(
                status=consts.FAIL,
                message='Task with pid %s is not running' % task.pid
            )

    def start_waiting_tasks(self):

        tasks = models.Task.objects.filter(status=consts.WAIT)