def mock_sleep_kills_redis(seconds): # first time the Redis provider sleeps to wait for the # server to appear, we kill the server; after that # we make sleep into a no-op so we rapidly time out. if 'done' in killed: return pidfile = os.path.join(dirname, "services", "REDIS_URL", "redis.pid") count = 0 while count < 15: if os.path.exists(pidfile): break real_sleep(0.1) count = count + 1 assert os.path.exists(pidfile) with codecs.open(pidfile, 'r', 'utf-8') as f: for line in f.readlines(): try: import signal os.kill(int(line.strip()), signal.SIGKILL) except Exception: pass # be sure it's gone real_sleep(0.1) killed['done'] = True
def test_negative(self): self.switch_expected = False self.assertRaises(IOError, gevent.sleep, -1) if sys.platform != 'win32': from time import sleep as real_sleep try: real_sleep(-0.1) except IOError, real_ex: pass
def sleep_impl(sec): if sec <= _min_sec: raise ValueError(sec) times = int(sec / _min_sec) from time import sleep as real_sleep start = self._clock real_sleep(sec) self._clock = start + times
def m_sleep(seconds): """Sleep function but acelerated 100x.""" real_sleep(seconds/100)
def quota_supervisor(inspect): conn = sqlite3.connect(Settings.database_location) #check Quota Supervisor's state (overall execution time takes less than a second: 2 rows & 17 columns) with conn: conn.row_factory = sqlite3.Row cur = conn.cursor() cur.execute("SELECT * FROM QuotaPeaks WHERE recorded == (SELECT MAX(recorded) FROM QuotaPeaks)") #selecting the latest record data = cur.fetchone() data = dict (data) if data['state'] == 1: #start processing if verified record_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') #of course, who would prefer speed of quarter of a second should disable stochastic_flow if data['stochastic'] == 1: quota_supervisor_stochastic(data, record_time) #inspect and act if inspect == 'server_calls': peak_server_calls_daily = data ['server_calls_d'] peak_server_calls_hourly = data ['server_calls_h'] if peak_server_calls_daily is not None: cur.execute("SELECT SUM(server_calls) FROM statistics WHERE STRFTIME('%Y-%m-%d', created)==STRFTIME('%Y-%m-%d', 'now', 'localtime')") fetched_data = cur.fetchone() if fetched_data is None: update_activity('timeloop') #this is to prevent asynchronous 'now' (23:59:59 vs 00:00:01) between update_activity and quota_supervisor realtime- very rare, but can happen else: fetched_server_calls_daily = fetched_data[0] if fetched_server_calls_daily >= peak_server_calls_daily: if quota_supervisor_sleeper(protosleep=data['sleep'], interval='daily', check='server_calls') == True: forward_sleep_seconds = quota_supervisor_sleeper(protosleep=data['sleep'], interval='daily') print('{} - INFO - Quota Supervisor: daily server calls reached quotient.. (going to sleep {} hours long)\n\n~ time for IstaPy to take a big good nap :-)'.format(record_time, "%.1f" % (forward_sleep_seconds/60/60))) quota_supervisor_notifier(protonotify=data['notify'], alert='sleep', job='Server calls', interval='daily') real_sleep (forward_sleep_seconds) quota_supervisor_notifier(protonotify=data['notify'], alert='wake up', job='Server calls', interval='daily') else: print('{} - INFO - QuotaSupervisor: daily server calls reached quotient.. (..exiting)\n\n~ for *non-stop botting add sleep_after=[\'server_calls\'] arg on the go! ;)'.format(record_time)) quota_supervisor_notifier(protonotify=data['notify'], alert='exit', job='Server calls', interval='daily') exit() #if peak_server_calls is fairly not None and sleep_after has no 'server_calls', Quota Supervisor will end up session. if peak_server_calls_hourly is not None: cur.execute("SELECT server_calls, created FROM statistics WHERE STRFTIME('%Y-%m-%d %H', created)==STRFTIME('%Y-%m-%d %H', 'now', 'localtime')") fetched_data = cur.fetchone() if fetched_data is None: update_activity('timeloop') #this is to prevent asynchronous 'now' (59:59 vs 00:01) between update_activity and quota_supervisor realtime- rare, but can happen else: fetched_server_calls_hourly, fetched_raw_time = fetched_data[0], fetched_data[1] if fetched_server_calls_hourly >= peak_server_calls_hourly: if quota_supervisor_sleeper(protosleep=data['sleep'], interval='hourly', check='server_calls') == True: forward_sleep_seconds = quota_supervisor_sleeper(protosleep=data['sleep'], interval='hourly', fetched_time=fetched_raw_time) print('{} - INFO - Quota Supervisor: hourly server calls reached quotient.. (going to sleep {} minutes long)\n\n~ take a tea break? :>'.format(record_time, "%.0f" % (forward_sleep_seconds/60))) quota_supervisor_notifier(protonotify=data['notify'], alert='sleep', job='Server calls', interval='hourly') real_sleep (forward_sleep_seconds) quota_supervisor_notifier(protonotify=data['notify'], alert='wake up', job='Server calls', interval='hourly') else: print('{} - INFO - QuotaSupervisor: hourly server calls reached quotient.. (..exiting)\n\n~ for *non-stop botting add sleep_after=[\'server_calls\'] arg on the go! ;)'.format(record_time)) quota_supervisor_notifier(protonotify=data['notify'], alert='exit', job='Server calls', interval='hourly') exit() #to prevent quitting after peak values reached, just enable sleep_after=['server_calls'] or just put None to server_calls's peak value(s) if inspect == 'likes': peak_likes_daily = data['likes_d'] peak_likes_hourly = data['likes_h'] if peak_likes_daily is not None: cur.execute("SELECT SUM(likes) FROM statistics WHERE STRFTIME('%Y-%m-%d', created)==STRFTIME('%Y-%m-%d', 'now', 'localtime')") fetched_data = cur.fetchone() if fetched_data is None: update_activity('timeloop') else: fetched_likes_daily = fetched_data[0] if fetched_likes_daily >= peak_likes_daily: if quota_supervisor_sleeper(protosleep=data['sleep'], interval='daily', check='likes') == True: forward_sleep_seconds = quota_supervisor_sleeper(protosleep=data['sleep'], interval='daily') print('{} - INFO - Quota Supervisor: daily likes reached quotient.. (going to sleep {} hours long)\n\n~ time for IstaPy to take a big good nap :-)'.format(record_time, "%.1f" % (forward_sleep_seconds/60/60))) quota_supervisor_notifier(protonotify=data['notify'], alert='sleep', job='Likes', interval='daily') real_sleep (forward_sleep_seconds) quota_supervisor_notifier(protonotify=data['notify'], alert='wake up', job='Likes', interval='daily') else: print('{} - INFO - QuotaSupervisor: jumped a like out of daily quotient.. be fair with numbers, behave good! :]'.format(record_time)) return 'jump' if peak_likes_hourly is not None: cur.execute("SELECT likes, created FROM statistics WHERE STRFTIME('%Y-%m-%d %H', created)==STRFTIME('%Y-%m-%d %H', 'now', 'localtime')") fetched_data = cur.fetchone() if fetched_data is None: update_activity('timeloop') else: fetched_likes_hourly, fetched_raw_time = fetched_data[0], fetched_data[1] if fetched_likes_hourly >= peak_likes_hourly: if quota_supervisor_sleeper(protosleep=data['sleep'], interval='hourly', check='likes') == True: forward_sleep_seconds = quota_supervisor_sleeper(protosleep=data['sleep'], interval='hourly', fetched_time=fetched_raw_time) print('{} - INFO - Quota Supervisor: hourly likes reached quotient.. (going to sleep {} minutes long)\n\n~ take a tea break? :>'.format(record_time, "%.0f" % (forward_sleep_seconds/60))) quota_supervisor_notifier(protonotify=data['notify'], alert='sleep', job='Likes', interval='hourly') real_sleep (forward_sleep_seconds) quota_supervisor_notifier(protonotify=data['notify'], alert='wake up', job='Likes', interval='hourly') else: print('{} - INFO - QuotaSupervisor: jumped a like out of hourly quotient.. be fair with numbers, behave good! :]'.format(record_time)) return 'jump' elif inspect == 'comments': peak_comments_daily = data ['comments_d'] peak_comments_hourly = data ['comments_h'] peak_likes_daily = data ['likes_d'] peak_likes_hourly = data ['likes_h'] if peak_comments_daily is not None: cur.execute("SELECT SUM(comments), SUM(likes) FROM statistics WHERE STRFTIME('%Y-%m-%d', created)==STRFTIME('%Y-%m-%d', 'now', 'localtime')") fetched_data = cur.fetchone() if fetched_data is None: update_activity('timeloop') else: fetched_comments_daily, fetched_likes_daily = fetched_data[0], fetched_data[1] if (fetched_comments_daily >= peak_comments_daily or fetched_likes_daily >= peak_likes_daily): if (quota_supervisor_sleeper(protosleep=data['sleep'], interval='daily', check='comments') == True and fetched_comments_daily >= peak_comments_daily): forward_sleep_seconds = quota_supervisor_sleeper(protosleep=data['sleep'], interval='daily') print('{} - INFO - Quota Supervisor: daily comments reached quotient.. (going to sleep {} hours long)\n\n~ time for IstaPy to take a big good nap :-)'.format(record_time, "%.1f" % (forward_sleep_seconds/60/60))) quota_supervisor_notifier(protonotify=data['notify'], alert='sleep', job='Comments', interval='daily') real_sleep (forward_sleep_seconds) quota_supervisor_notifier(protonotify=data['notify'], alert='wake up', job='Comments', interval='daily') else: print('{} - INFO - QuotaSupervisor: jumped a comment out of daily quotient.. be fair with numbers, behave good! :]'.format(record_time)) return 'jump' if peak_comments_hourly is not None: cur.execute("SELECT comments, likes, created FROM statistics WHERE STRFTIME('%Y-%m-%d %H', created)==STRFTIME('%Y-%m-%d %H', 'now', 'localtime')") fetched_data = cur.fetchone() if fetched_data is None: update_activity('timeloop') else: fetched_comments_hourly, fetched_likes_hourly, fetched_raw_time = fetched_data[0], fetched_data[1], fetched_data[2] if (fetched_comments_hourly >= peak_comments_hourly or fetched_likes_hourly >= peak_likes_hourly): if (quota_supervisor_sleeper(protosleep=data['sleep'], interval='hourly', check='comments') == True and fetched_comments_hourly >= peak_comments_hourly): forward_sleep_seconds = quota_supervisor_sleeper(protosleep=data['sleep'], interval='hourly', fetched_time=fetched_raw_time) print('{} - INFO - Quota Supervisor: hourly comments reached quotient.. (going to sleep {} minutes long)\n\n~ take a tea break? :>'.format(record_time, "%.0f" % (forward_sleep_seconds/60))) quota_supervisor_notifier(protonotify=data['notify'], alert='sleep', job='Comments', interval='hourly') real_sleep (forward_sleep_seconds) quota_supervisor_notifier(protonotify=data['notify'], alert='wake up', job='Comments', interval='hourly') else: print('{} - INFO - QuotaSupervisor: jumped a comment out of hourly quotient.. be fair with numbers, behave good! :]'.format(record_time)) return 'jump' elif inspect == 'follows': peak_follows_daily = data ['follows_d'] peak_follows_hourly = data ['follows_h'] if peak_follows_daily is not None: cur.execute("SELECT SUM(follows) FROM statistics WHERE STRFTIME('%Y-%m-%d', created)==STRFTIME('%Y-%m-%d', 'now', 'localtime')") fetched_data = cur.fetchone() if fetched_data is None: update_activity('timeloop') else: fetched_follows_daily = fetched_data[0] if fetched_follows_daily >= peak_follows_daily: if quota_supervisor_sleeper(protosleep=data['sleep'], interval='daily', check='follows') == True: forward_sleep_seconds = quota_supervisor_sleeper(protosleep=data['sleep'], interval='daily') print('{} - INFO - Quota Supervisor: daily follows reached quotient.. (going to sleep {} hours long)\n\n~ time for IstaPy to take a big good nap :-)'.format(record_time, "%.1f" % (forward_sleep_seconds/60/60))) quota_supervisor_notifier(protonotify=data['notify'], alert='sleep', job='Follows', interval='daily') real_sleep (forward_sleep_seconds) quota_supervisor_notifier(protonotify=data['notify'], alert='wake up', job='Follows', interval='daily') else: print('{} - INFO - QuotaSupervisor: jumped a follow out of daily quotient.. be fair with numbers, behave good! :]'.format(record_time)) return 'jump' if peak_follows_hourly is not None: cur.execute("SELECT follows, created FROM statistics WHERE STRFTIME('%Y-%m-%d %H', created)==STRFTIME('%Y-%m-%d %H', 'now', 'localtime')") fetched_data = cur.fetchone() if fetched_data is None: update_activity('timeloop') else: fetched_follows_hourly, fetched_raw_time = fetched_data[0], fetched_data[1] if fetched_follows_hourly >= peak_follows_hourly: if quota_supervisor_sleeper(protosleep=data['sleep'], interval='hourly', check='follows') == True: forward_sleep_seconds = quota_supervisor_sleeper(protosleep=data['sleep'], interval='hourly', fetched_time=fetched_raw_time) print('{} - INFO - Quota Supervisor: hourly follows reached quotient.. (going to sleep {} minutes long)\n\n~ take a tea break? :>'.format(record_time, "%.0f" % (forward_sleep_seconds/60))) quota_supervisor_notifier(protonotify=data['notify'], alert='sleep', job='Follows', interval='hourly') real_sleep (forward_sleep_seconds) quota_supervisor_notifier(protonotify=data['notify'], alert='wake up', job='Follows', interval='hourly') else: print('{} - INFO - QuotaSupervisor: jumped a follow out of hourly quotient.. be fair with numbers, behave good! :]'.format(record_time)) return 'jump' elif inspect == 'unfollows': peak_unfollows_daily = data ['unfollows_d'] peak_unfollows_hourly = data ['unfollows_h'] if peak_unfollows_daily is not None: cur.execute("SELECT SUM(unfollows) FROM statistics WHERE STRFTIME('%Y-%m-%d', created)==STRFTIME('%Y-%m-%d', 'now', 'localtime')") fetched_data = cur.fetchone() if fetched_data is None: update_activity('timeloop') else: fetched_unfollows_daily = fetched_data[0] if fetched_unfollows_daily >= peak_unfollows_daily: if quota_supervisor_sleeper(protosleep=data['sleep'], interval='daily', check='unfollows') == True: forward_sleep_seconds = quota_supervisor_sleeper(protosleep=data['sleep'], interval='daily') print('{} - INFO - Quota Supervisor: daily unfollows reached quotient.. (going to sleep {} hours long)\n\n~ time for IstaPy to take a big good nap :-)'.format(record_time, "%.1f" % (forward_sleep_seconds/60/60))) quota_supervisor_notifier(protonotify=data['notify'], alert='sleep', job='Unfollows', interval='daily') real_sleep (forward_sleep_seconds) quota_supervisor_notifier(protonotify=data['notify'], alert='wake up', job='Unfollows', interval='daily') else: print('{} - INFO - QuotaSupervisor: jumped an unfollow out of daily quotient.. be fair with numbers, behave good! :]'.format(record_time)) return 'jump' if peak_unfollows_hourly is not None: cur.execute("SELECT unfollows, created FROM statistics WHERE STRFTIME('%Y-%m-%d %H', created)==STRFTIME('%Y-%m-%d %H', 'now', 'localtime')") fetched_data = cur.fetchone() if fetched_data is None: update_activity('timeloop') else: fetched_unfollows_hourly, fetched_raw_time = fetched_data[0], fetched_data[1] if fetched_unfollows_hourly >= peak_unfollows_hourly: if quota_supervisor_sleeper(protosleep=data['sleep'], interval='hourly', check='likes') == True: forward_sleep_seconds = quota_supervisor_sleeper(protosleep=data['sleep'], interval='hourly', fetched_time=fetched_raw_time) print('{} - INFO - Quota Supervisor: hourly unfollows reached quotient.. (going to sleep {} minutes long)\n\n~ take a tea break? :>'.format(record_time, "%.0f" % (forward_sleep_seconds/60))) quota_supervisor_notifier(protonotify=data['notify'], alert='sleep', job='Unfollows', interval='hourly') real_sleep (forward_sleep_seconds) quota_supervisor_notifier(protonotify=data['notify'], alert='wake up', job='Unfollows', interval='hourly') else: print('{} - INFO - QuotaSupervisor: jumped an unfollow out of hourly quotient.. be fair with numbers, behave good! :]'.format(record_time)) return 'jump'