# Path: newsApp/jobManager.py
# class JobManager:
#     """
#     Helper class to enqueue and dequeue jobs to the job queue.
#     """
# 
#     def __init__(self, connectionStringKey):
#         """
#         Instantiates the job manager.
# 
#         'connectionStringKey' : name of environment variable containing the
#         connection string to use.
#         """
# 
#         self.queue = getQueue(os.environ[connectionStringKey])
# 
#     def enqueueJob(self, job):
#         """
#         Enqueue the job into the jobs queue.
#         """
# 
#         enqueueMessage(self.queue, job.serializeToString())
# 
#     def convertDequeuedMessageToJob(self, dequeuedMessage):
#         if dequeuedMessage is None:
#             return None
#         dequeuedJob = WorkerJob(None, None)
#         dequeuedJob.deserializeFromString(dequeuedMessage)
#         return dequeuedJob
# 
#     def dequeueJob(self):
#         """
#         Dequeue a job from the job queue.
#         """
# 
#         dequeuedMessage = dequeueMessage(self.queue)
#         return self.convertDequeuedMessageToJob(dequeuedMessage)
# 
#     def count(self):
#         """
#         Return the count of messages in queue.
#         """
# 
#         return self.queue.count()
# 
# Path: newsApp/workerJob.py
# class WorkerJob:
#     """
#     Represents a job which can be processed by one of the worker roles.
#     """
# 
#     def __init__(self, jobName, jobParams, jobId = None):
#         """
#         Instantiates a new worker job object.
#         Requires 'jobName': a string representing name of the job
#         Requires 'jobParams': a dictionary where keys represnt job parameter
#             names, and corresponding values the job parameter values.
#         Optional 'jobId': a identier for this job. If not provided a
#             randomly generated alphanumeric string is used.
#         """
# 
#         if (jobId is None):
#             self.jobId = _generateRandomJobId()
#         else :
#             self.jobId = jobId
# 
#         self.jobName = jobName
#         self.jobParams = jobParams
# 
#     def deserializeFromString(self, serializedJob):
#         """
#         Sets this worker job object to the specified serialized string
#         representation.
#         """
# 
#         tempDict = json.loads(serializedJob)
#         jobName = tempDict.pop('jobName', None)
#         jobId = tempDict.pop('jobId', None)
#         self.__init__(jobName, tempDict, jobId)
# 
#     def serializeToString(self):
#         """
#         Serialize to a human-readable string representation of this object
#         """
# 
#         tempDict = dict(self.jobParams)
#         tempDict['jobName'] = self.jobName
#         tempDict['jobId'] = self.jobId
#         return json.dumps(tempDict)


import getopt
import os
import sys
import time
import threading
from multiprocessing import Process
from .constants import *
from .loggingHelper import *
from .jobManager import JobManager
from .workerJob import WorkerJob
from .feedProcessor import *
from .linkProcessor import *
from . import clusteringJobs as cj
from . import dbJobs as dj
                job.jobParams[JOBARG_COMPAREDOCS_DOC1ID],
                job.jobParams[JOBARG_COMPAREDOCS_DOC2ID])
        elif job.jobName == JOB_COMPAREDOCSBATCH:
            cj.compareDocsBatch(
                job.jobId,
                job.jobParams[JOBARG_COMPAREDOCSBATCH_DOCID],
                job.jobParams[JOBARG_COMPAREDOCSBATCH_OTHERDOCS])
        elif job.jobName == JOB_CLUSTERDOCS:
            cj.clusterDocs(job.jobId)
        elif job.jobName == JOB_CLEANUPDOC:
            cj.cleanUpDoc(job.jobId, job.jobParams[JOBARG_CLEANUPDOC_DOCID])
        elif job.jobName == JOB_CLEANUPDOCSHINGLES:
            cj.cleanUpDocShingles(job.jobId, job.jobParams[JOBARG_CLEANUPDOCSHINGLES_DOCID])
        elif job.jobName == JOB_CLEANUPDOCENTITIES:
            cj.cleanUpDocEntities(job.jobId, job.jobParams[JOBARG_CLEANUPDOCENTITIES_DOCID])
        elif job.jobName == JOB_CLEANUPDOCDISTANCES:
            cj.cleanUpDocDistances(job.jobId, job.jobParams[JOBARG_CLEANUPDOCDISTANCES_DOCID])
        elif job.jobName == JOB_PROCESSNEWCLUSTER:
            cj.processNewCluster(job.jobId, job.jobParams[JOBARG_PROCESSNEWCLUSTER_CLUSTER])
        else:
            logging.exception("Unknown job type %s", job.jobName)
    except:
        logging.exception('Failed to execute worker job')

def DequeueAndStartJob(connectionStringKey):
    """
    Dequeue a job from the queue and start executing it.
    """

    logging.info("Dequeing a job.")

# Path: newsApp/jobManager.py
# class JobManager:
#     """
#     Helper class to enqueue and dequeue jobs to the job queue.
#     """
# 
#     def __init__(self, connectionStringKey):
#         """
#         Instantiates the job manager.
# 
#         'connectionStringKey' : name of environment variable containing the
#         connection string to use.
#         """
# 
#         self.queue = getQueue(os.environ[connectionStringKey])
# 
#     def enqueueJob(self, job):
#         """
#         Enqueue the job into the jobs queue.
#         """
# 
#         enqueueMessage(self.queue, job.serializeToString())
# 
#     def convertDequeuedMessageToJob(self, dequeuedMessage):
#         if dequeuedMessage is None:
#             return None
#         dequeuedJob = WorkerJob(None, None)
#         dequeuedJob.deserializeFromString(dequeuedMessage)
#         return dequeuedJob
# 
#     def dequeueJob(self):
#         """
#         Dequeue a job from the job queue.
#         """
# 
#         dequeuedMessage = dequeueMessage(self.queue)
#         return self.convertDequeuedMessageToJob(dequeuedMessage)
# 
#     def count(self):
#         """
#         Return the count of messages in queue.
#         """
# 
#         return self.queue.count()
# 
# Path: newsApp/workerJob.py
# class WorkerJob:
#     """
#     Represents a job which can be processed by one of the worker roles.
#     """
# 
#     def __init__(self, jobName, jobParams, jobId = None):
#         """
#         Instantiates a new worker job object.
#         Requires 'jobName': a string representing name of the job
#         Requires 'jobParams': a dictionary where keys represnt job parameter
#             names, and corresponding values the job parameter values.
#         Optional 'jobId': a identier for this job. If not provided a
#             randomly generated alphanumeric string is used.
#         """
# 
#         if (jobId is None):
#             self.jobId = _generateRandomJobId()
#         else :
#             self.jobId = jobId
# 
#         self.jobName = jobName
#         self.jobParams = jobParams
# 
#     def deserializeFromString(self, serializedJob):
#         """
#         Sets this worker job object to the specified serialized string
#         representation.
#         """
# 
#         tempDict = json.loads(serializedJob)
#         jobName = tempDict.pop('jobName', None)
#         jobId = tempDict.pop('jobId', None)
#         self.__init__(jobName, tempDict, jobId)
# 
#     def serializeToString(self):
#         """
#         Serialize to a human-readable string representation of this object
#         """
# 
#         tempDict = dict(self.jobParams)
#         tempDict['jobName'] = self.jobName
#         tempDict['jobId'] = self.jobId
#         return json.dumps(tempDict)


# Path: newsApp/worker.py
import getopt
import os
import sys
import time
import threading
from multiprocessing import Process
from .constants import *
from .loggingHelper import *
from .jobManager import JobManager
from .workerJob import WorkerJob
from .feedProcessor import *
from .linkProcessor import *
from . import clusteringJobs as cj
from . import dbJobs as dj

                job.jobParams[JOBARG_COMPAREDOCS_DOC1ID],
                job.jobParams[JOBARG_COMPAREDOCS_DOC2ID])
        elif job.jobName == JOB_COMPAREDOCSBATCH:
            cj.compareDocsBatch(
                job.jobId,
                job.jobParams[JOBARG_COMPAREDOCSBATCH_DOCID],
                job.jobParams[JOBARG_COMPAREDOCSBATCH_OTHERDOCS])
        elif job.jobName == JOB_CLUSTERDOCS:
            cj.clusterDocs(job.jobId)
        elif job.jobName == JOB_CLEANUPDOC:
            cj.cleanUpDoc(job.jobId, job.jobParams[JOBARG_CLEANUPDOC_DOCID])
        elif job.jobName == JOB_CLEANUPDOCSHINGLES:
            cj.cleanUpDocShingles(job.jobId, job.jobParams[JOBARG_CLEANUPDOCSHINGLES_DOCID])
        elif job.jobName == JOB_CLEANUPDOCENTITIES:
            cj.cleanUpDocEntities(job.jobId, job.jobParams[JOBARG_CLEANUPDOCENTITIES_DOCID])
        elif job.jobName == JOB_CLEANUPDOCDISTANCES:
            cj.cleanUpDocDistances(job.jobId, job.jobParams[JOBARG_CLEANUPDOCDISTANCES_DOCID])
        elif job.jobName == JOB_PROCESSNEWCLUSTER:
            cj.processNewCluster(job.jobId, job.jobParams[JOBARG_PROCESSNEWCLUSTER_CLUSTER])
        else:
            logging.exception("Unknown job type %s", job.jobName)
    except:
        logging.exception('Failed to execute worker job')

def DequeueAndStartJob(connectionStringKey):
    """
    Dequeue a job from the queue and start executing it.
    """

    logging.info("Dequeing a job.")

    jobManager = JobManager(connectionStringKey)