CC-4114: use liquidsoap shutdown() command to restart liquidsoap

-more fixes
This commit is contained in:
Martin Konecny 2012-08-20 12:11:03 -04:00
parent 9f903833d7
commit 79ea05bc1b
2 changed files with 48 additions and 18 deletions

View File

@ -28,7 +28,7 @@ stop () {
/usr/lib/airtime/airtime_virtualenv/bin/python /usr/lib/airtime/pypo/bin/liquidsoap_scripts/liquidsoap_prepare_terminate.py /usr/lib/airtime/airtime_virtualenv/bin/python /usr/lib/airtime/pypo/bin/liquidsoap_scripts/liquidsoap_prepare_terminate.py
# Send TERM after 5 seconds, wait at most 30 seconds. # Send TERM after 5 seconds, wait at most 30 seconds.
start-stop-daemon --stop --oknodo --retry TERM/5/0/30 --quiet --pidfile $PIDFILE start-stop-daemon --stop --oknodo --retry 5 --quiet --pidfile $PIDFILE
rm -f $PIDFILE rm -f $PIDFILE
} }

View File

@ -8,6 +8,7 @@ import json
import telnetlib import telnetlib
import copy import copy
from threading import Thread from threading import Thread
import subprocess
from Queue import Empty from Queue import Empty
@ -142,14 +143,14 @@ class PypoFetch(Thread):
def switch_source(logger, lock, sourcename, status): def switch_source(logger, lock, sourcename, status):
logger.debug('Switching source: %s to "%s" status', sourcename, status) logger.debug('Switching source: %s to "%s" status', sourcename, status)
command = "streams." command = "streams."
if(sourcename == "master_dj"): if sourcename == "master_dj":
command += "master_dj_" command += "master_dj_"
elif(sourcename == "live_dj"): elif sourcename == "live_dj":
command += "live_dj_" command += "live_dj_"
elif(sourcename == "scheduled_play"): elif sourcename == "scheduled_play":
command += "scheduled_play_" command += "scheduled_play_"
if(status == "on"): if status == "on":
command += "start\n" command += "start\n"
else: else:
command += "stop\n" command += "stop\n"
@ -206,10 +207,37 @@ class PypoFetch(Thread):
fh.close() fh.close()
def restart_liquidsoap(self): def restart_liquidsoap(self):
self.telnet_lock.acquire()
try: try:
self.logger.info("Restarting Liquidsoap")
subprocess.call('/etc/init.d/airtime-liquidsoap restart', shell=True) subprocess.call('/etc/init.d/airtime-liquidsoap restart', shell=True)
#Wait here until Liquidsoap has started up
self.logger.info("Waiting for Liquidsoap to start")
while True:
try:
tn = telnetlib.Telnet(LS_HOST, LS_PORT)
tn.write("exit\n")
tn.read_all()
self.logger.info("Liquidsoap is up and running")
break
except Exception, e:
#sleep 0.5 seconds and try again
time.sleep(0.5)
except Exception, e: except Exception, e:
self.logger.error(e) self.logger.error(e)
finally:
self.telnet_lock.release()
try:
self.set_bootstrap_variables()
#get the most up to date schedule, which will #initiate the process
#of making sure Liquidsoap is playing the schedule
self.manual_schedule_fetch()
except Exception, e:
self.logger.error(str(e))
def regenerate_liquidsoap_conf(self, setting): def regenerate_liquidsoap_conf(self, setting):
existing = {} existing = {}
@ -220,8 +248,8 @@ class PypoFetch(Thread):
fh = open('/etc/airtime/liquidsoap.cfg', 'r') fh = open('/etc/airtime/liquidsoap.cfg', 'r')
except IOError, e: except IOError, e:
#file does not exist #file does not exist
self.write_liquidsoap_config(setting)
self.restart_liquidsoap() self.restart_liquidsoap()
return
self.logger.info("Reading existing config...") self.logger.info("Reading existing config...")
# read existing conf file and build dict # read existing conf file and build dict
@ -249,10 +277,10 @@ class PypoFetch(Thread):
existing[key] = value existing[key] = value
fh.close() fh.close()
# dict flag for any change in cofig # dict flag for any change in config
change = {} change = {}
# this flag is to detect disable -> disable change # this flag is to detect disable -> disable change
# in that case, we don't want to restart even if there are chnges. # in that case, we don't want to restart even if there are changes.
state_change_restart = {} state_change_restart = {}
#restart flag #restart flag
restart = False restart = False
@ -287,7 +315,7 @@ class PypoFetch(Thread):
if stream not in change: if stream not in change:
change[stream] = False change[stream] = False
if not (s[u'value'] == existing[s[u'keyname']]): if not (s[u'value'] == existing[s[u'keyname']]):
self.logger.info("Keyname: %s, Curent value: %s, New Value: %s", s[u'keyname'], existing[s[u'keyname']], s[u'value']) self.logger.info("Keyname: %s, Current value: %s, New Value: %s", s[u'keyname'], existing[s[u'keyname']], s[u'value'])
change[stream] = True change[stream] = True
# set flag change for sound_device alway True # set flag change for sound_device alway True
@ -301,7 +329,6 @@ class PypoFetch(Thread):
restart = True restart = True
# rewrite # rewrite
if restart: if restart:
self.write_liquidsoap_config(setting)
self.restart_liquidsoap() self.restart_liquidsoap()
else: else:
self.logger.info("No change detected in setting...") self.logger.info("No change detected in setting...")
@ -309,14 +336,14 @@ class PypoFetch(Thread):
def update_liquidsoap_connection_status(self): def update_liquidsoap_connection_status(self):
""" """
updates the status of liquidsoap connection to the streaming server updates the status of Liquidsoap connection to the streaming server
This fucntion updates the bootup time variable in liquidsoap script This function updates the bootup time variable in Liquidsoap script
""" """
self.telnet_lock.acquire() self.telnet_lock.acquire()
try: try:
tn = telnetlib.Telnet(LS_HOST, LS_PORT) tn = telnetlib.Telnet(LS_HOST, LS_PORT)
# update the boot up time of liquidsoap. Since liquidsoap is not restarting, # update the boot up time of Liquidsoap. Since Liquidsoap is not restarting,
# we are manually adjusting the bootup time variable so the status msg will get # we are manually adjusting the bootup time variable so the status msg will get
# updated. # updated.
current_time = time.time() current_time = time.time()
@ -478,13 +505,18 @@ class PypoFetch(Thread):
except Exception, e: except Exception, e:
self.logger.error(e) self.logger.error(e)
def manual_schedule_fetch(self):
success, self.schedule_data = self.api_client.get_schedule()
if success:
self.process_schedule(self.schedule_data)
return success
def main(self): def main(self):
# Bootstrap: since we are just starting up, we need to grab the # Bootstrap: since we are just starting up, we need to grab the
# most recent schedule. After that we can just wait for updates. # most recent schedule. After that we can just wait for updates.
success, self.schedule_data = self.api_client.get_schedule() success = self.manual_schedule_fetch()
if success: if success:
self.logger.info("Bootstrap schedule received: %s", self.schedule_data) self.logger.info("Bootstrap schedule received: %s", self.schedule_data)
self.process_schedule(self.schedule_data)
self.set_bootstrap_variables() self.set_bootstrap_variables()
loops = 1 loops = 1
@ -510,9 +542,7 @@ class PypoFetch(Thread):
self.handle_message(message) self.handle_message(message)
except Empty, e: except Empty, e:
self.logger.info("Queue timeout. Fetching schedule manually") self.logger.info("Queue timeout. Fetching schedule manually")
success, self.schedule_data = self.api_client.get_schedule() self.manual_schedule_fetch()
if success:
self.process_schedule(self.schedule_data)
except Exception, e: except Exception, e:
import traceback import traceback
top = traceback.format_exc() top = traceback.format_exc()