Skip to content
This repository has been archived by the owner on Feb 20, 2018. It is now read-only.

Commit

Permalink
Merge pull request #203 from 2gis/screencast-recorder-gracefully-shut…
Browse files Browse the repository at this point in the history
…down

Screencast recorder gracefully shutdown
  • Loading branch information
sh0ked authored Mar 30, 2017
2 parents d3c6e4e + 314432b commit a0d1328
Showing 1 changed file with 42 additions and 29 deletions.
71 changes: 42 additions & 29 deletions core/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import os
import sys
import time
import signal
import socket
import logging
import os.path
import websockify
Expand Down Expand Up @@ -36,7 +36,7 @@ def _flvrec(filename, host='localhost', port=5900,
framerate=12, keyframe=120,
preferred_encoding=(0,),
blocksize=32, clipping=None,
debug=0, verbose=0):
debug=0):
fp = file(filename, 'wb')
pwdcache = rfb.PWDCache('%s:%d' % (host, port))
writer = flv.FLVWriter(fp, framerate=framerate, debug=debug)
Expand All @@ -48,33 +48,36 @@ def _flvrec(filename, host='localhost', port=5900,
host, port, sink, timeout=500/framerate,
pwdcache=pwdcache, preferred_encoding=preferred_encoding,
debug=debug)
if verbose:
log.debug('Start vnc recording to %s' % filename)
retval = 0
log.debug('Start vnc recording to %s' % filename)
return_code = 0
try:
def sigint_handler(sig, frame):
def sigterm_handler(sig, frame):
log.debug("%s %s" % (sig, frame))
raise KeyboardInterrupt
signal.signal(signal.SIGINT, sigint_handler)
raise SystemExit
signal.signal(signal.SIGTERM, sigterm_handler)
client.open()
try:
while 1:
current_time = time.time()
max_duration = getattr(config, "SCREENCAST_RECORDER_MAX_DURATION", 1800)
while True:
if time.time() - current_time > max_duration:
log.warning("VNC recorder for {} has been stopped "
"because max duration({}) was exceeded".format(filename, max_duration))
raise SystemExit
client.idle()
finally:
client.close()
except KeyboardInterrupt:
pass
except socket.error, e:
log.debug('Socket error: %s' % str(e))
retval = 1
except rfb.RFBError, e:
log.debug('RFB error: %s' % str(e))
retval = 1
if verbose:
log.debug('Stop vnc recording to %s' % filename)
writer.close()
fp.close()
return retval
except Exception as e:
if isinstance(e, SystemExit):
log.info("VNC recorder process({}): Got SIGTERM. stopping...".format(filename))
else:
log.exception("Error in VNC recorder process({})".format(filename))
return_code = 1
finally:
writer.close()
fp.close()
exit(return_code)
log.info('Stopped vnc recording to %s' % filename)

def delete_source_video(self):
if self.__filepath and os.path.isfile(self.__filepath):
Expand Down Expand Up @@ -114,16 +117,26 @@ def start_recording(self, framerate=5, size=(800, 600)):
kwargs = {
'framerate': framerate,
'clipping': video.str2clip("%sx%s+0-0" % (size[0], size[1])),
'debug': 1,
'verbose': 1
'debug': 1
}
self.recorder = multiprocessing.Process(target=self._flvrec,
args=(self.__filepath,
self.host,
self.port),
kwargs=kwargs)
self.recorder = multiprocessing.Process(
target=self._flvrec,
args=(self.__filepath, self.host, self.port),
kwargs=kwargs
)
self.recorder.daemon = True
self.recorder.start()
log.info(
"Started screencast recording(pid:{}) for {}:{} to {}".format(
self.recorder.pid, self.host, self.port, self.dir_path
)
)

def stop_recording(self):
if self.recorder and self.recorder.is_alive():
self.recorder.terminate()
log.info(
"Stopped screencast recording(pid:{}) for {}:{} to {}".format(
self.recorder.pid, self.host, self.port, self.dir_path
)
)

0 comments on commit a0d1328

Please sign in to comment.