~dpigott/lava-dispatcher/port-closed-fix

« back to all changes in this revision

Viewing changes to lava_dispatcher/client/master.py

  • Committer: Michael Hudson-Doyle
  • Date: 2012-06-01 05:17:36 UTC
  • mfrom: (311.1.6 connection-dance)
  • Revision ID: michael.hudson@linaro.org-20120601051736-vf5fnonbul1wmzpu
watch for various messages from the connection_command that indicate
how successful the connection attempt has been, and do various things in response.

apologies for the lack of review, the code has been well tested on staging though

Show diffs side-by-side

added added

removed removed

Lines of Context:
297
297
        pre_connect = self.device_option("pre_connect_command")
298
298
        if pre_connect:
299
299
            logging_system(pre_connect)
 
300
        self.proc = self._connect_carefully()
 
301
        atexit.register(self._close_logging_spawn)
 
302
 
 
303
    def _connect_carefully(self):
300
304
        cmd = self.device_option("connection_command")
301
 
        proc = logging_spawn(cmd, timeout=1200)
302
 
        proc.logfile_read = self.sio
303
 
        #serial can be slow, races do funny things if you don't increase delay
304
 
        proc.delaybeforesend=1
305
 
        self.proc = proc
306
 
        atexit.register(self._close_logging_spawn)
 
305
 
 
306
        retry_count = 0
 
307
        retry_limit = 3
 
308
 
 
309
        port_stuck_message = 'Data Buffering Suspended\.'
 
310
        hot_key_message = 'Type the hot key to suspend the connection:.*\r'
 
311
        conn_closed_message = 'Connection closed by foreign host\.'
 
312
 
 
313
        expectations = {
 
314
            port_stuck_message: 'reset-port',
 
315
            'Connected\.\r': 'all-good',
 
316
            hot_key_message: 'all-good',
 
317
            conn_closed_message: 'retry',
 
318
            pexpect.TIMEOUT: 'all-good',
 
319
            }
 
320
        patterns = []
 
321
        results = []
 
322
        for pattern, result in expectations.items():
 
323
            patterns.append(pattern)
 
324
            results.append(result)
 
325
 
 
326
        while retry_count < retry_limit:
 
327
            proc = logging_spawn(cmd, timeout=1200)
 
328
            proc.logfile_read = self.sio
 
329
            #serial can be slow, races do funny things if you don't increase delay
 
330
            proc.delaybeforesend=1
 
331
            logging.info('Attempting to connect to device')
 
332
            match = proc.expect(patterns, timeout=10)
 
333
            result = results[match]
 
334
            logging.info('Matched %r which means %s', patterns[match], result)
 
335
            if result == 'retry':
 
336
                proc.close(True)
 
337
                retry_count += 1
 
338
                time.sleep(5)
 
339
                continue
 
340
            elif result == 'all-good':
 
341
                return proc
 
342
            elif result == 'reset-port':
 
343
                reset_port = self.device_option("reset_port_command")
 
344
                if reset_port:
 
345
                    logging_system(reset_port)
 
346
                else:
 
347
                    raise OperationFailed("no reset_port command configured")
 
348
                proc.close(True)
 
349
                retry_count += 1
 
350
                time.sleep(5)
 
351
        raise OperationFailed("could execute connection_command successfully")
307
352
 
308
353
    @property
309
354
    def master_str(self):