# Path: yawn/worker/models.py
# class Worker(models.Model):
#     """Information about current and past workers"""
#     #
#     # NOTE: consider instead taking an advisory lock for each worker,
#     # and using it to check if a worker is still connected.
#     # See `pg_try_advisory_lock` and `select * from pg_locks where locktype = 'advisory'`
#     # That would give more immediate feedback, but its not clear we need to be faster.
#     #
#     ACTIVE = 'active'
#     EXITED = 'exited'
#     LOST = 'lost'
#     STATUS_CHOICES = [(x, x) for x in (ACTIVE, EXITED, LOST)]
# 
#     name = models.TextField(blank=False)
#     status = models.TextField(choices=STATUS_CHOICES, default=ACTIVE)
#     start_timestamp = models.DateTimeField(default=functions.Now)
#     last_heartbeat = models.DateTimeField(default=functions.Now)
# 
#     @staticmethod
#     def find_lost(timeout):
#         from yawn.task.models import Execution
# 
#         # Make a sparse index so looking up active workers is fast:
#         # CREATE INDEX yawn_worker_active ON yawn_worker (status) WHERE status = 'active'
#         lost = Worker.objects.filter(
#             status=Worker.ACTIVE, last_heartbeat__lt=functions.Now() - timedelta(seconds=timeout)
#         )
#         for worker in lost:
#             logger.warning('Marking %r as lost', worker)
#             worker.status = Worker.LOST
#             worker.save()
# 
#             executions = worker.execution_set.filter(status=Execution.RUNNING)
# 
#             for execution in executions:
#                 logger.warning('Marking %r as lost', execution)
#                 execution.mark_finished(lost=True)
# 
#     def __str__(self):
#         return self.name


from yawn.worker.models import Worker


def test_get_task(client, run):
    task = run.task_set.first()
    worker = Worker.objects.create(name='worker1')
    task.start_execution(worker)
    url = '/api/tasks/%s/' % task.id
    response = client.get(url)
    assert response.status_code == 200, response.data
    assert response.data['name'] == task.template.name
    assert response.data['status'] == task.status
    assert response.data['executions'][0]['worker']['name'] == worker.name
    assert response.data['messages'][0]['queue'] == task.message_set.first().queue.name


def test_patch_task_terminate(client, run):
    task = run.task_set.first()
    url = '/api/tasks/%s/' % task.id

# Path: yawn/worker/models.py
# class Worker(models.Model):
#     """Information about current and past workers"""
#     #
#     # NOTE: consider instead taking an advisory lock for each worker,
#     # and using it to check if a worker is still connected.
#     # See `pg_try_advisory_lock` and `select * from pg_locks where locktype = 'advisory'`
#     # That would give more immediate feedback, but its not clear we need to be faster.
#     #
#     ACTIVE = 'active'
#     EXITED = 'exited'
#     LOST = 'lost'
#     STATUS_CHOICES = [(x, x) for x in (ACTIVE, EXITED, LOST)]
# 
#     name = models.TextField(blank=False)
#     status = models.TextField(choices=STATUS_CHOICES, default=ACTIVE)
#     start_timestamp = models.DateTimeField(default=functions.Now)
#     last_heartbeat = models.DateTimeField(default=functions.Now)
# 
#     @staticmethod
#     def find_lost(timeout):
#         from yawn.task.models import Execution
# 
#         # Make a sparse index so looking up active workers is fast:
#         # CREATE INDEX yawn_worker_active ON yawn_worker (status) WHERE status = 'active'
#         lost = Worker.objects.filter(
#             status=Worker.ACTIVE, last_heartbeat__lt=functions.Now() - timedelta(seconds=timeout)
#         )
#         for worker in lost:
#             logger.warning('Marking %r as lost', worker)
#             worker.status = Worker.LOST
#             worker.save()
# 
#             executions = worker.execution_set.filter(status=Execution.RUNNING)
# 
#             for execution in executions:
#                 logger.warning('Marking %r as lost', execution)
#                 execution.mark_finished(lost=True)
# 
#     def __str__(self):
#         return self.name


# Path: yawn/task/tests/test_views.py
from yawn.worker.models import Worker



def test_get_task(client, run):
    task = run.task_set.first()
    worker = Worker.objects.create(name='worker1')
    task.start_execution(worker)
    url = '/api/tasks/%s/' % task.id
    response = client.get(url)
    assert response.status_code == 200, response.data
    assert response.data['name'] == task.template.name
    assert response.data['status'] == task.status
    assert response.data['executions'][0]['worker']['name'] == worker.name
    assert response.data['messages'][0]['queue'] == task.message_set.first().queue.name


def test_patch_task_terminate(client, run):
    task = run.task_set.first()
    url = '/api/tasks/%s/' % task.id

    worker = Worker.objects.create(name='worker1')