# Path: job_runner/apps/job_runner/models.py
# class Worker(models.Model):
#     """
#     Workers
#     """
#     title = models.CharField(max_length=255)
#     description = models.TextField(blank=True)
#     api_key = models.CharField(max_length=255, db_index=True, unique=True)
#     secret = models.CharField(max_length=255, db_index=True)
#     enqueue_is_enabled = models.BooleanField(
#         default=True,
#         db_index=True,
#         help_text=(
#             'If unchecked, nothing for this worker will be added to '
#             'the worker queue. This will not affect already running jobs.'
#         )
#     )
#     ping_response_dts = models.DateTimeField(
#         blank=True, null=True, editable=False)
# 
#     # the worker will send back the worker version and concurrent jobs on
#     # ping response
#     worker_version = models.CharField(
#         max_length=100, blank=True, null=True, editable=False)
#     concurrent_jobs = models.PositiveIntegerField(
#         blank=True, null=True, editable=False)
# 
#     def __unicode__(self):
#         return self.title
# 
#     def log_name(self):
#         return u'"{0}"({1})'.format(self.title, self.pk)
# 
#     def is_responsive(self):
#         """
#         Return ``bool`` indicating if the worker is resposive.
#         """
#         unresponsive_intervals = \
#             settings.JOB_RUNNER_WORKER_UNRESPONSIVE_AFTER_INTERVALS
#         ping_interval = settings.JOB_RUNNER_WORKER_PING_INTERVAL
#         ping_margin = settings.JOB_RUNNER_WORKER_PING_MARGIN
# 
#         acceptable_delta = timedelta(
#             seconds=(unresponsive_intervals * ping_interval) + ping_margin)
# 
#         if self.ping_response_dts:
#             if self.ping_response_dts + acceptable_delta >= timezone.now():
#                 return True
# 
#         logger.error('Worker {0} is not responsive'.format(self.log_name()))
#         return False
# 
#     class Meta:
#         ordering = ('title', )
# 
# class WorkerPool(models.Model):
#     """
#     Worker-pool.
#     """
#     title = models.CharField(max_length=255, db_index=True)
#     description = models.TextField(blank=True)
#     notification_addresses = models.TextField(
#         help_text='Separate e-mail addresses by a newline',
#         blank=True,
#     )
#     enqueue_is_enabled = models.BooleanField(
#         default=True,
#         db_index=True,
#         help_text=(
#             'If unchecked, nothing for this worker-pool will be added to '
#             'the worker queue. This will not affect already running jobs.'
#         )
#     )
#     workers = models.ManyToManyField(Worker)
# 
#     def __unicode__(self):
#         return self.title
# 
#     def log_name(self):
#         return u'"{0}"({1})'.format(self.title, self.pk)
# 
#     def get_notification_addresses(self):
#         """
#         Return a ``list`` notification addresses.
#         """
#         addresses = self.notification_addresses.strip().split('\n')
#         return [x.strip() for x in addresses if x.strip() != '']
# 
#     class Meta:
#         ordering = ('title', )
# 
# Path: job_runner/apps/job_runner/notifications.py
# def reschedule_failed(job):
# def run_failed(run):
# def worker_pool_unresponsive(worker_pool):


import json
import logging
import time
import zmq
from datetime import datetime, timedelta
from django.conf import settings
from django.core.management.base import NoArgsCommand
from django.db import transaction
from job_runner.apps.job_runner.models import Worker, WorkerPool
from job_runner.apps.job_runner import notifications




logger = logging.getLogger(__name__)


class Command(NoArgsCommand):
    help = 'Monitor worker and worker-pool health'


# Path: job_runner/apps/job_runner/models.py
# class Worker(models.Model):
#     """
#     Workers
#     """
#     title = models.CharField(max_length=255)
#     description = models.TextField(blank=True)
#     api_key = models.CharField(max_length=255, db_index=True, unique=True)
#     secret = models.CharField(max_length=255, db_index=True)
#     enqueue_is_enabled = models.BooleanField(
#         default=True,
#         db_index=True,
#         help_text=(
#             'If unchecked, nothing for this worker will be added to '
#             'the worker queue. This will not affect already running jobs.'
#         )
#     )
#     ping_response_dts = models.DateTimeField(
#         blank=True, null=True, editable=False)
# 
#     # the worker will send back the worker version and concurrent jobs on
#     # ping response
#     worker_version = models.CharField(
#         max_length=100, blank=True, null=True, editable=False)
#     concurrent_jobs = models.PositiveIntegerField(
#         blank=True, null=True, editable=False)
# 
#     def __unicode__(self):
#         return self.title
# 
#     def log_name(self):
#         return u'"{0}"({1})'.format(self.title, self.pk)
# 
#     def is_responsive(self):
#         """
#         Return ``bool`` indicating if the worker is resposive.
#         """
#         unresponsive_intervals = \
#             settings.JOB_RUNNER_WORKER_UNRESPONSIVE_AFTER_INTERVALS
#         ping_interval = settings.JOB_RUNNER_WORKER_PING_INTERVAL
#         ping_margin = settings.JOB_RUNNER_WORKER_PING_MARGIN
# 
#         acceptable_delta = timedelta(
#             seconds=(unresponsive_intervals * ping_interval) + ping_margin)
# 
#         if self.ping_response_dts:
#             if self.ping_response_dts + acceptable_delta >= timezone.now():
#                 return True
# 
#         logger.error('Worker {0} is not responsive'.format(self.log_name()))
#         return False
# 
#     class Meta:
#         ordering = ('title', )
# 
# class WorkerPool(models.Model):
#     """
#     Worker-pool.
#     """
#     title = models.CharField(max_length=255, db_index=True)
#     description = models.TextField(blank=True)
#     notification_addresses = models.TextField(
#         help_text='Separate e-mail addresses by a newline',
#         blank=True,
#     )
#     enqueue_is_enabled = models.BooleanField(
#         default=True,
#         db_index=True,
#         help_text=(
#             'If unchecked, nothing for this worker-pool will be added to '
#             'the worker queue. This will not affect already running jobs.'
#         )
#     )
#     workers = models.ManyToManyField(Worker)
# 
#     def __unicode__(self):
#         return self.title
# 
#     def log_name(self):
#         return u'"{0}"({1})'.format(self.title, self.pk)
# 
#     def get_notification_addresses(self):
#         """
#         Return a ``list`` notification addresses.
#         """
#         addresses = self.notification_addresses.strip().split('\n')
#         return [x.strip() for x in addresses if x.strip() != '']
# 
#     class Meta:
#         ordering = ('title', )
# 
# Path: job_runner/apps/job_runner/notifications.py
# def reschedule_failed(job):
# def run_failed(run):
# def worker_pool_unresponsive(worker_pool):


# Path: job_runner/apps/job_runner/management/commands/health_check.py
import json
import logging
import time
import zmq
from datetime import datetime, timedelta
from django.conf import settings
from django.core.management.base import NoArgsCommand
from django.db import transaction
from job_runner.apps.job_runner.models import Worker, WorkerPool
from job_runner.apps.job_runner import notifications





logger = logging.getLogger(__name__)


class Command(NoArgsCommand):
    help = 'Monitor worker and worker-pool health'


    publisher = None