def run(self): ''' Running logic starts here ''' sema.acquire() try: # Check whether current host is broadcasting for another event if self._host_id not in replay_running_host: logging.info('Begin to do upload for media_id = %d', self._media_id) # add host to current running set replay_running_host.add(self._host_id) logging.info('Currently replay running hosts: %s', str(replay_running_host)) # get replay broadcasting url, if the scheduled event no live, this step raise exception temp_playlist=self._get_replay_url() update_broadcast_info(self._channel_id, self._cover_url, self._title) # '-vcodec libx264 -b:v 1024k -preset fast -force_key_frames expr:gte(t,n_forced*1)' cmd = 'ffmpeg -re -i "{playlist}" ' \ ' -c:v copy ' \ '-c:a aac ' \ '-strict -2 -ac 1 -ar 22050 -b:a 48k -f flv {rmtp}'.format( playlist=temp_playlist, rmtp=self._get_rmtp_address() ) logging.info("For media id %d, begin to execute: %s", self._media_id, cmd) self.job = subprocess.Popen(cmd, shell=False, stdout=open(self.get_upload_log_path(),'a'), stderr=subprocess.STDOUT) # If running successfully, send notification time.sleep(15.0) if self.job.poll() is None: # A None value indicates that the process hasn’t terminated yet. self._notify() # Periodically check whether live end, not shown in rings while self.job.poll() is None: time.sleep(30.0) try: if self._host_id not in get_current_broadcasting_user(): self.job.terminate() break except Exception, e: logging.warning(e.message) if self._host_id in replay_running_host: replay_running_host.remove(self._host_id) else:
def run(self): ''' Running logic starts here ''' sema.acquire() start_time.val = time.time() # thread local start time try: # First, check whether current event has been broadcasted before # Second, check whether current host is broadcasting for another event if self._event_id not in already_run_event and self._host_id not in current_running_host: logging.info('Begin to do upload for event id = %s', self._event_id) # add host to current running set, before actually uploading current_running_host.add(self._host_id) # get live broadcasting url. if the scheduled event no live, this step raise exception temp_playlist=self._get_play_list_address() logging.info('Get play url for event id = %s: %s', self._event_id, temp_playlist) # update channel cover, title update_broadcast_info(self._channel_id, self._cover_url, self._title) logging.info('Update channel url and title for event id %s', self._event_id) # '-vcodec libx264 -b:v 1024k -preset fast -force_key_frames expr:gte(t,n_forced*1)' cmd = 'ffmpeg -re -i "{playlist}" ' \ ' -c:v copy ' \ '-c:a aac ' \ '-strict -2 -ac 1 -ar 22050 -b:a 48k -f flv {rmtp}'.format( playlist=temp_playlist, rmtp=self._get_rmtp_address() ) logging.info("For event id %s, begin to execute: %s",self._event_id, cmd) self.job = subprocess.Popen(cmd, shell=False, stdout=open(self.get_upload_log_path(),'a'), stderr=subprocess.STDOUT) already_run_event.add(self._event_id) logging.info('Currently running crawled hosts: %s', str(current_running_host)) # If running successfully, send notification time.sleep(15.0) if self.job.poll() is None: # A None value indicates that the process hasn’t terminated yet. self._notify() # Periodically check whether live end, not shown in rings, or timeout while self.job.poll() is None: time.sleep(30.0) try: if (self._host_id not in get_current_broadcasting_user() or start_time.val - time.time() > 3600*4): # if live more than 4 hour, stop self.job.terminate() logging.info('Event %s with host %d is terminated due to overtime or not shown in Rings', self._event_id, self._host_id) break except Exception, e: logging.warning(e.message) # After finish uploading, remove host from current running host set if self._host_id in current_running_host: current_running_host.remove(self._host_id) else: