def test_in_future(self): # Example: pass_time is 10 seconds in the future. Return True now_plus_ten = self.now + self.add_ten_seconds now_plus_ten_ts = time.mktime(now_plus_ten.timetuple( )) # Python 3 has a datetime.timestamp() function, but... self.assertTrue(in_future(now_plus_ten_ts))
def request_and_enqueue_next_pass(): ''' Make request, get next pass time enqueue task slack task at that pass time enqueue get next task task at that pass time Work in UTC, not local time ''' logging.info('Requesting set of next pass times') next_passes = iss.get_next_pass(config.lat, config.lon, 3) or [ ] # Returns None in event of error, replace with [] logging.debug('next passes received: ' + str(next_passes)) # If the ISS is above right now, the first time returned by the ISS API may be # the current time. This should be ignored as have just posted this. next_future_pass = [ p for p in next_passes if in_future(p['risetime'], config.min_seconds_in_future) ] logging.debug('next passes more than ' + str(config.min_seconds_in_future) + ' seconds in the future: ' + str(next_future_pass)) if next_future_pass: next_time = next_future_pass[0]['risetime'] seconds = next_future_pass[0]['duration'] utc = timezone('UTC') eta = datetime.fromtimestamp(next_time, utc) message = config.slack_message_text % seconds logging.info( 'Enqueuing new slack task for a pass at %f, of %s seconds, with ETA of %s' % (next_time, seconds, eta)) name = 'iss_at_%.0f' % next_time try: task = taskqueue.add( queue_name='slack', name=name, # prevent duplicates url='/post_to_slack', target='iss_worker', eta=eta, params={ 'message': message, 'risetime': next_time }) except (DuplicateTaskNameError, TaskAlreadyExistsError, TombstonedTaskError) as e: logging.warning( 'ISS Task with name ' + name + ' already exists (or has existed) in slack notification queue. ' + str(e)) logging.info( 'Enqueuing new task to query for next pass time, with ETA of %s' % eta) name = 'get_next_pass_%.0f' % next_time try: # And add a task to do this again, at the same time as the slack post task task = taskqueue.add(queue_name='passtimes', name=name, url='/enqueue_next_pass_time', target='passtimes_worker', eta=eta) except (DuplicateTaskNameError, TaskAlreadyExistsError, TombstonedTaskError) as e: logging.warning( 'Task with name ' + name + ' already exists (or has existed) in next pass notification queue. ' + str(e)) logging.info('Next pass enqueued') return 'Next pass time ' + str(eta) + ' enqueued' else: # Cause task to fail. The queue will retry after the min-retry-time specified in queue.yaml raise NoFutureTimesFoundException()
def test_in_future_in_past_beyond_negative_min(self): # Example: pass_time is 40 seconds in the past, min_time_delta is -10. return False now_minus_fourty = self.now - self.add_fourty_seconds now_minus_fourty_ts = time.mktime(now_minus_fourty.timetuple()) self.assertFalse(in_future(now_minus_fourty_ts, -10))
def test_in_future_in_past(self): # Example: pass_time is in the past. return False now_minus_ten = self.now - self.add_ten_seconds now_minus_ten_ts = time.mktime(now_minus_ten.timetuple()) self.assertFalse(in_future(now_minus_ten_ts))
def test_in_future_at_min(self): # Example: pass_time is 10 seconds in the future. min_time_in_future is 10. Return False now_plus_ten = self.now + self.add_ten_seconds now_plus_ten_ts = time.mktime(now_plus_ten.timetuple()) self.assertFalse(in_future(now_plus_ten_ts, 10))
def test_in_future_beyond_min(self): # Example: pass_time is 10 seconds in the future. min_time_in_future is 5. Return True now_plus_ten = self.now + self.add_ten_seconds now_plus_ten_ts = time.mktime(now_plus_ten.timetuple()) self.assertTrue(in_future(now_plus_ten_ts, 5))
def test_in_future_now(self): # Example: pass_time is now. Return False now_ts = time.mktime(self.now.timetuple()) self.assertFalse(in_future(now_ts))