def start_recording(quality, bitrate): global recorder global state global menu_timer_id recorder.start(quality, bitrate, VIDEO_LENGTH * 60) # menu hide_menu() # timers Watchdog.clear_timeout(menu_timer_id) state = 'recording'
def start(self, quality, bitrate, duration): """ Start recording a video of quality and duration """ # organize params for raspivid command filename = '%s/%s_%smbps_%s.h264' % (self._working_directory, quality, bitrate, datetime.now().strftime('%Y.%m.%d_%H%M')) params = {'filename': filename, 't': int(duration * 1000), 'b': int(bitrate * 10**6)} params.update(self._quality_map[quality]) raspivid_command = 'raspivid --hflip --vflip --vstab --width %(w)s --height %(h)s --framerate 25 --bitrate %(b)s --timeout %(t)s --output %(filename)s' % params self._pid = self._run_and_get_pid(raspivid_command) print 'pid is', self._pid self._active = True self.started_at = time.time() # memorize recording options self.filename = filename self.quality = quality self.bitrate = bitrate # don't interrupt raspivid, it will finish itself # we're just interested when recording is finished self._timer_id = Watchdog.set_timeout(duration + 1, self.stop) print 'recorder started'
def _get_watchdog(self, check_freq): watchdog = None watchdog_multiplier = int( self._agentConfig.get("watchdog_multiplier", WATCHDOG_MULTIPLIER)) if self._agentConfig.get("watchdog", True): watchdog = Watchdog.create(check_freq * watchdog_multiplier) watchdog.reset() return watchdog
def __init__(self, port, agentConfig, watchdog=True, skip_ssl_validation=False, use_simple_http_client=False): self._port = int(port) self._agentConfig = agentConfig self._metrics = {} self._dns_cache = None AgentTransaction.set_application(self) AgentTransaction.set_endpoints(agentConfig['endpoints']) if agentConfig['endpoints'] == {}: log.warning( u"No valid endpoint found. Forwarder will drop all incoming payloads." ) AgentTransaction.set_request_timeout(agentConfig['forwarder_timeout']) max_parallelism = self.NO_PARALLELISM # Multiple endpoints => enable parallelism if len(agentConfig['endpoints']) > 1: max_parallelism = self.DEFAULT_PARALLELISM self._tr_manager = TransactionManager(MAX_WAIT_FOR_REPLAY, MAX_QUEUE_SIZE, THROTTLING_DELAY, max_parallelism=max_parallelism) AgentTransaction.set_tr_manager(self._tr_manager) self._watchdog = None self.skip_ssl_validation = skip_ssl_validation or _is_affirmative( agentConfig.get('skip_ssl_validation')) self.agent_dns_caching = _is_affirmative( agentConfig.get('dns_caching')) self.agent_dns_ttl = int(agentConfig.get('dns_ttl', DEFAULT_DNS_TTL)) if self.agent_dns_caching: self._dns_cache = DNSCache(ttl=self.agent_dns_ttl) self.use_simple_http_client = use_simple_http_client if self.skip_ssl_validation: log.info( "Skipping SSL hostname validation, useful when using a transparent proxy" ) # Monitor activity if watchdog: watchdog_timeout = TRANSACTION_FLUSH_INTERVAL * WATCHDOG_INTERVAL_MULTIPLIER / 1000 self._watchdog = Watchdog.create( watchdog_timeout, max_resets=WATCHDOG_HIGH_ACTIVITY_THRESHOLD)
def __init__(self, interval, metrics_aggregator, api_host, api_key=None, use_watchdog=False, event_chunk_size=None): threading.Thread.__init__(self) self.interval = int(interval) self.finished = threading.Event() self.metrics_aggregator = metrics_aggregator self.flush_count = 0 self.log_count = 0 self.hostname = get_hostname() self.watchdog = None if use_watchdog: self.watchdog = Watchdog.create(WATCHDOG_TIMEOUT) self.api_key = api_key self.api_host = api_host self.event_chunk_size = event_chunk_size or EVENT_CHUNK_SIZE
def test_watchdog_frenesy_detection(self, mock_restarted): """ Watchdog restarts the process on suspicious high activity. """ # Limit the restart timeframe for test purpose Watchdog._RESTART_TIMEFRAME = 1 # Create a watchdog with a low activity tolerancy process_watchdog = Watchdog(10, max_resets=3) ping_watchdog = process_watchdog.reset with self.set_time(1): # Can be reset 3 times within the watchdog timeframe for x in xrange(0, 3): ping_watchdog() # On the 4th attempt, the watchdog detects a suspicously high activity self.assertRaises(WatchdogKill, ping_watchdog) with self.set_time(3): # Gets back to normal when the activity timeframe expires. ping_watchdog()
def __init__(self, port, agentConfig, watchdog=True, skip_ssl_validation=False, use_simple_http_client=False): self._port = int(port) self._agentConfig = agentConfig self._metrics = {} self._dns_cache = None AgentTransaction.set_application(self) AgentTransaction.set_endpoints(agentConfig['endpoints']) if agentConfig['endpoints'] == {}: log.warning(u"No valid endpoint found. Forwarder will drop all incoming payloads.") AgentTransaction.set_request_timeout(agentConfig['forwarder_timeout']) max_parallelism = self.NO_PARALLELISM # Multiple endpoints => enable parallelism if len(agentConfig['endpoints']) > 1: max_parallelism = self.DEFAULT_PARALLELISM self._tr_manager = TransactionManager(MAX_WAIT_FOR_REPLAY, MAX_QUEUE_SIZE, THROTTLING_DELAY, max_parallelism=max_parallelism) AgentTransaction.set_tr_manager(self._tr_manager) self._watchdog = None self.skip_ssl_validation = skip_ssl_validation or _is_affirmative(agentConfig.get('skip_ssl_validation')) self.agent_dns_caching = _is_affirmative(agentConfig.get('dns_caching')) self.agent_dns_ttl = int(agentConfig.get('dns_ttl', DEFAULT_DNS_TTL)) if self.agent_dns_caching: self._dns_cache = DNSCache(ttl=self.agent_dns_ttl) self.use_simple_http_client = use_simple_http_client if self.skip_ssl_validation: log.info("Skipping SSL hostname validation, useful when using a transparent proxy") # Monitor activity if watchdog: watchdog_timeout = TRANSACTION_FLUSH_INTERVAL * WATCHDOG_INTERVAL_MULTIPLIER / 1000 self._watchdog = Watchdog.create(watchdog_timeout, max_resets=WATCHDOG_HIGH_ACTIVITY_THRESHOLD)
def _get_watchdog(self, check_freq): watchdog = None if self._agentConfig.get("watchdog", True): watchdog = Watchdog.create(check_freq * WATCHDOG_MULTIPLIER) watchdog.reset() return watchdog
def fast_tornado(self): a = Application(12345, self.AGENT_CONFIG) a._watchdog = Watchdog(6) a._tr_manager = MockTxManager() a.run()
def normal_run(self): w = Watchdog(2) w.reset() for i in range(5): time.sleep(1) w.reset()
def hanging_net(self): w = Watchdog(5) w.reset() x = url.urlopen("http://localhost:31834") print "ERROR Net call returned", x return True
def busy_run(self): w = Watchdog(5) w.reset() while True: random()
def redraw_menu(btn): # boooooo global state global menu_timer_id if state != 'menu': show_menu() # timers Watchdog.clear_timeout(menu_timer_id) menu_timer_id = Watchdog.set_timeout(MENU_TIMEOUT, hide_menu) stop_select_propagation = False # first, allow to go up if it's possible and requested if btn == 'up': if menu['level']: menu['level'].pop() # second, go down to the selected item item, prev_item = menu, menu for level in menu['level']: prev_item = item item = item['items'][level] # third, allow to go even lower, if it's possible and requested if btn in ['down', 'select']: if 'items' in item['items'][item['selected']]: menu['level'].append(item['selected']) prev_item = item item = item['items'][item['selected']] stop_select_propagation = True # allow to go through items of this level if btn in ['left', 'right']: if 'left' == btn: delta = -1 if 'right' == btn: delta = +1 item['selected'] = (item['selected'] + delta) % len(item['items']) # prepare item meta to be printed to screen prev_name = '' name = item['items'][item['selected']]['name'] description = item['items'][item['selected']].get('description', '') if not description: if item != prev_item: prev_name = '%s%s' % (lcd.CHAR_ARROW_UP, prev_item['items'][prev_item['selected']]['name']) position = '%s%d/%d%s' % (lcd.CHAR_ARROW_LEFT, item['selected'] + 1, len(item['items']), lcd.CHAR_ARROW_RIGHT) lcd.home() lcd.clear() line1 = '%-16s' % (name, ) line2 = '%-11s%5s' % (description or prev_name, position) lcd.message('%s\n%s' % (line1, line2)) # finally process SELECT if btn == 'select' and not stop_select_propagation: selected_item = item['items'][item['selected']] if 'call' in selected_item: func, args = selected_item['call'], selected_item['args'] func(*args)
elif btn_down: redraw_menu('down') elif btn_left: redraw_menu('left') elif btn_right: redraw_menu('right') elif btn_select: redraw_menu('select') if state == 'recording': lcd.home() lcd.message(recording_screen(recorder)) if not recorder.is_active(): state = 'idle' if state == 'idle': # update status message lcd.home() lcd.message(status_screen()) # process all timeouts that we have Watchdog.process_timeouts() # throttle frame rate, keeps screen legible while True: t = time.time() if (t - lastTime) > (1.0 / MAX_FPS): break lastTime = t