import time
from logging_config import get_logger

logger = get_logger(__name__)

class Scheduler:

    def __init__(self, processor, config):
        self.processor = processor
        self.config = config

    def run(self):
        logger.info("Scheduler started")

        while True:
            try:
                count = self.processor.process()

                logger.info("Inserted count %s", count)

                if count > 0:
                    logger.info("Calling procedure")
                    self.processor.repo.call_proc()

            except Exception:
                logger.exception("Scheduler error")

            logger.info("Sleeping for next cycle")
            time.sleep(self.config.poll_interval_minutes * 60)
