def handle(self, *args, **options): log_handler = configure_log_handler('mirrorstate', options['loglevel'], options['output']) with logbook.NullHandler(): with log_handler.applicationbound(): local_timezone = pytz.timezone(settings.TIME_ZONE) now = pytz.datetime.datetime.now(tz=local_timezone) urls = list(ElectionUrl.objects.filter(state__in=args)) def timestamp_as_unix_timestamp(urlobj): mirror = urlobj.latest_mirror() if mirror is None: return 0 return time.mktime(mirror.timestamp.timetuple()) urls.sort(key=timestamp_as_unix_timestamp) for url in urls: previous = url.latest_mirror() if previous: # Don't mirror URLs too often since_last = now - previous.timestamp if since_last < settings.MIRROR_WAIT: wait = math.floor((settings.MIRROR_WAIT - since_last).total_seconds()) log.notice("Waiting {0} seconds before mirroring again: {1}".format( wait, url.url)) time.sleep(wait) mirror_url(url) if options['forever']: restart_process()
def handle(self, *args, **options): log_handler = configure_log_handler('mirrormirror', options['loglevel'], options['output']) with logbook.NullHandler(): with log_handler.applicationbound(): local_timezone = pytz.timezone(settings.TIME_ZONE) now = pytz.datetime.datetime.now(tz=local_timezone) if ElectionMirror.objects.count() == 0: urls = ElectionUrl.objects.all() else: # URLs that have never been mirrored urls = ElectionUrl.objects.filter( mirrors__isnull=True)[:settings.MIRROR_BATCH_SIZE] if not urls: mirror_timestamps = list( ElectionMirror.objects.values('election_url'). annotate(timestamp=Max('timestamp'))) mirror_timestamps.sort(key=itemgetter('timestamp')) batch_urls = [ m['election_url'] for m in mirror_timestamps ][:settings.MIRROR_BATCH_SIZE] urls = ElectionUrl.objects.filter(pk__in=batch_urls) for url in urls: previous = url.latest_mirror() if previous: # Don't mirror URLs too often since_last = now - previous.timestamp if since_last < settings.MIRROR_WAIT: wait = math.floor((settings.MIRROR_WAIT - since_last).total_seconds()) log.notice( "Waiting {0} seconds before mirroring again: {1}" .format(url.url, wait)) time.sleep(wait) mirror_url(url) restart_process()
def handle(self, *args, **options): ensure_phantomjs_is_runnable() log_handler = configure_log_handler('allthescreenshots', options['loglevel'], options['output']) starttime = datetime.datetime.now() with logbook.NullHandler(): with log_handler.applicationbound(): for url in ElectionUrl.objects.all(): url.take_screenshot() if options['forever']: now = datetime.datetime.now() cycle = datetime.timedelta(seconds=300) duration = now - starttime if duration <= cycle: remainder = cycle - duration log.notice("Sleeping {sec} seconds before cycling.", sec=remainder.total_seconds()) time.sleep(remainder.total_seconds()) restart_process()
def handle(self, *args, **options): log_handler = configure_log_handler('mirrormirror', options['loglevel'], options['output']) with logbook.NullHandler(): with log_handler.applicationbound(): local_timezone = pytz.timezone(settings.TIME_ZONE) now = pytz.datetime.datetime.now(tz=local_timezone) if ElectionMirror.objects.count() == 0: urls = ElectionUrl.objects.all() else: # URLs that have never been mirrored urls = ElectionUrl.objects.filter(mirrors__isnull=True)[:settings.MIRROR_BATCH_SIZE] if not urls: mirror_timestamps = list(ElectionMirror.objects .values('election_url') .annotate(timestamp=Max('timestamp'))) mirror_timestamps.sort(key=itemgetter('timestamp')) batch_urls = [m['election_url'] for m in mirror_timestamps][:settings.MIRROR_BATCH_SIZE] urls = ElectionUrl.objects.filter(pk__in=batch_urls) for url in urls: previous = url.latest_mirror() if previous: # Don't mirror URLs too often since_last = now - previous.timestamp if since_last < settings.MIRROR_WAIT: wait = math.floor((settings.MIRROR_WAIT - since_last).total_seconds()) log.notice("Waiting {0} seconds before mirroring again: {1}".format( url.url, wait)) time.sleep(wait) mirror_url(url) restart_process()
def handle(self, *args, **options): log_handler = configure_log_handler('mirrorstate', options['loglevel'], options['output']) with logbook.NullHandler(): with log_handler.applicationbound(): local_timezone = pytz.timezone(settings.TIME_ZONE) now = pytz.datetime.datetime.now(tz=local_timezone) urls = list(ElectionUrl.objects.filter(state__in=args)) def timestamp_as_unix_timestamp(urlobj): mirror = urlobj.latest_mirror() if mirror is None: return 0 return time.mktime(mirror.timestamp.timetuple()) urls.sort(key=timestamp_as_unix_timestamp) for url in urls: previous = url.latest_mirror() if previous: # Don't mirror URLs too often since_last = now - previous.timestamp if since_last < settings.MIRROR_WAIT: wait = math.floor((settings.MIRROR_WAIT - since_last).total_seconds()) log.notice( "Waiting {0} seconds before mirroring again: {1}" .format(wait, url.url)) time.sleep(wait) mirror_url(url) if options['forever']: restart_process()