import logging

from app import image_diff_queue
from data import model
from endpoints.v1.registry import process_image_changes
from workers.queueworker import QueueWorker


logger = logging.getLogger(__name__)


class DiffsWorker(QueueWorker):
  def process_queue_item(self, job_details):
    image_id = job_details['image_id']
    repository = job_details['repository']
    namespace = model.user.get_namespace_by_user_id(job_details['namespace_user_id'])

    try:
      process_image_changes(namespace, repository, image_id)
    except model.DataModelException:
      # This exception is unrecoverable, and the item should continue and be
      # marked as complete.
      msg = ('Image does not exist in database \'%s\' for repo \'%s/\'%s\'' %
             (image_id, namespace, repository))
      logger.warning(msg)

    return True

if __name__ == "__main__":
  worker = DiffsWorker(image_diff_queue)
  worker.start()
