CC-2080: Turn pypo-fetch and pypo-push into threads of the same process
First implementation
This commit is contained in:
parent
6ba4881ff2
commit
e64e30d6da
|
@ -1,3 +0,0 @@
|
|||
#!/bin/sh
|
||||
|
||||
su -l pypo -c "tail -F /etc/service/pypo-fetch/log/main/current"
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/sh
|
||||
|
||||
su -l pypo -c "less /etc/service/pypo/log/main/current"
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/sh
|
||||
|
||||
su -l pypo -c "tail -F /etc/service/pypo/log/main/current"
|
|
@ -39,7 +39,6 @@ AirtimeInstall::InstallPostgresScriptingLanguage();
|
|||
|
||||
echo "* Creating Database Tables".PHP_EOL;
|
||||
AirtimeInstall::CreateDatabaseTables();
|
||||
AirtimeInstall::MigrateTables(__DIR__);
|
||||
|
||||
echo "* Storage Directory Setup".PHP_EOL;
|
||||
AirtimeInstall::SetupStorageDirectory($CC_CONFIG);
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
#!/bin/sh
|
||||
pypo_user="pypo"
|
||||
export HOME="/home/pypo/"
|
||||
# Location of pypo_cli.py Python script
|
||||
pypo_path="/opt/pypo/bin/"
|
||||
pypo_script="pypo-cli.py"
|
||||
echo "*** Daemontools: starting daemon"
|
||||
cd ${pypo_path}
|
||||
exec 2>&1
|
||||
# Note the -u when calling python! we need it to get unbuffered binary stdout and stderr
|
||||
exec setuidgid ${pypo_user} \
|
||||
python -u ${pypo_path}${pypo_script} \
|
||||
-p
|
||||
# EOF
|
|
@ -36,7 +36,7 @@ def create_user(username):
|
|||
os.system("adduser --system --quiet --group --shell /bin/bash "+username)
|
||||
|
||||
#set pypo password
|
||||
p = os.popen('/usr/bin/passwd pypo', 'w')
|
||||
p = os.popen('/usr/bin/passwd pypo 2>&1 1>/dev/null', 'w')
|
||||
p.write('pypo\n')
|
||||
p.write('pypo\n')
|
||||
p.close()
|
||||
|
@ -103,23 +103,15 @@ try:
|
|||
os.system("chmod -R 755 "+BASE_PATH)
|
||||
os.system("chown -R pypo:pypo "+BASE_PATH)
|
||||
|
||||
print "Installing daemontool script pypo-fetch"
|
||||
create_path("/etc/service/pypo-fetch")
|
||||
create_path("/etc/service/pypo-fetch/log")
|
||||
shutil.copy("%s/pypo-daemontools-fetch.sh"%current_script_dir, "/etc/service/pypo-fetch/run")
|
||||
shutil.copy("%s/pypo-daemontools-logger.sh"%current_script_dir, "/etc/service/pypo-fetch/log/run")
|
||||
os.system("chmod -R 755 /etc/service/pypo-fetch")
|
||||
os.system("chown -R pypo:pypo /etc/service/pypo-fetch")
|
||||
print "Installing pypo daemon"
|
||||
create_path("/etc/service/pypo")
|
||||
create_path("/etc/service/pypo/log")
|
||||
shutil.copy("%s/pypo-daemontools.sh"%current_script_dir, "/etc/service/pypo/run")
|
||||
shutil.copy("%s/pypo-daemontools-logger.sh"%current_script_dir, "/etc/service/pypo/log/run")
|
||||
os.system("chmod -R 755 /etc/service/pypo")
|
||||
os.system("chown -R pypo:pypo /etc/service/pypo")
|
||||
|
||||
print "Installing daemontool script pypo-push"
|
||||
create_path("/etc/service/pypo-push")
|
||||
create_path("/etc/service/pypo-push/log")
|
||||
shutil.copy("%s/pypo-daemontools-push.sh"%current_script_dir, "/etc/service/pypo-push/run")
|
||||
shutil.copy("%s/pypo-daemontools-logger.sh"%current_script_dir, "/etc/service/pypo-push/log/run")
|
||||
os.system("chmod -R 755 /etc/service/pypo-push")
|
||||
os.system("chown -R pypo:pypo /etc/service/pypo-push")
|
||||
|
||||
print "Installing daemontool script pypo-liquidsoap"
|
||||
print "Installing liquidsoap daemon"
|
||||
create_path("/etc/service/pypo-liquidsoap")
|
||||
create_path("/etc/service/pypo-liquidsoap/log")
|
||||
shutil.copy("%s/pypo-daemontools-liquidsoap.sh"%current_script_dir, "/etc/service/pypo-liquidsoap/run")
|
||||
|
@ -134,16 +126,12 @@ try:
|
|||
|
||||
found = True
|
||||
|
||||
p = Popen('svstat /etc/service/pypo-fetch', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
|
||||
p = Popen('svstat /etc/service/pypo', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
|
||||
output = p.stdout.read()
|
||||
if (output.find("unable to open supervise/ok: file does not exist") >= 0):
|
||||
found = False
|
||||
print output
|
||||
|
||||
p = Popen('svstat /etc/service/pypo-push', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
|
||||
output = p.stdout.read()
|
||||
print output
|
||||
|
||||
|
||||
p = Popen('svstat /etc/service/pypo-liquidsoap', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
|
||||
output = p.stdout.read()
|
||||
print output
|
||||
|
@ -152,6 +140,7 @@ try:
|
|||
print "Pypo install has completed, but daemontools is not running, please make sure you have it installed and then reboot."
|
||||
except Exception, e:
|
||||
print "exception:" + str(e)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -9,12 +9,9 @@ if os.geteuid() != 0:
|
|||
sys.exit(1)
|
||||
|
||||
try:
|
||||
print "Starting daemontool script pypo-fetch"
|
||||
os.system("svc -u /etc/service/pypo-fetch")
|
||||
|
||||
print "Starting daemontool script pypo-push"
|
||||
os.system("svc -u /etc/service/pypo-push")
|
||||
|
||||
print "Starting daemontool script pypo"
|
||||
os.system("svc -u /etc/service/pypo")
|
||||
|
||||
print "Starting daemontool script pypo-liquidsoap"
|
||||
os.system("svc -u /etc/service/pypo-liquidsoap")
|
||||
|
||||
|
|
|
@ -9,12 +9,9 @@ if os.geteuid() != 0:
|
|||
sys.exit(1)
|
||||
|
||||
try:
|
||||
print "Stopping daemontool script pypo-fetch"
|
||||
os.system("svc -dx /etc/service/pypo-fetch 2>/dev/null")
|
||||
print "Stopping daemontool script pypo"
|
||||
os.system("svc -dx /etc/service/pypo 2>/dev/null")
|
||||
|
||||
print "Stopping daemontool script pypo-push"
|
||||
os.system("svc -dx /etc/service/pypo-push 2>/dev/null")
|
||||
|
||||
print "Stopping daemontool script pypo-liquidsoap"
|
||||
os.system("svc -dx /etc/service/pypo-liquidsoap 2>/dev/null")
|
||||
os.system("killall liquidsoap")
|
||||
|
|
|
@ -37,12 +37,9 @@ try:
|
|||
print "Removing pypo files"
|
||||
remove_path(BASE_PATH)
|
||||
|
||||
print "Removing daemontool script pypo-fetch"
|
||||
remove_path("rm -rf /etc/service/pypo-fetch")
|
||||
|
||||
print "Removing daemontool script pypo-push"
|
||||
remove_path("rm -rf /etc/service/pypo-push")
|
||||
|
||||
print "Removing daemontool script pypo"
|
||||
remove_path("rm -rf /etc/service/pypo")
|
||||
|
||||
print "Removing daemontool script pypo-liquidsoap"
|
||||
remove_path("rm -rf /etc/service/pypo-liquidsoap")
|
||||
|
||||
|
|
|
@ -1,22 +1,27 @@
|
|||
[loggers]
|
||||
keys=root
|
||||
keys=root,fetch,push
|
||||
|
||||
[handlers]
|
||||
keys=consoleHandler,fileHandlerERROR,fileHandlerDEBUG,nullHandler
|
||||
keys=consoleHandler
|
||||
|
||||
[formatters]
|
||||
keys=simpleFormatter
|
||||
|
||||
[logger_root]
|
||||
level=DEBUG
|
||||
handlers=consoleHandler,fileHandlerERROR,fileHandlerDEBUG
|
||||
handlers=consoleHandler
|
||||
|
||||
[logger_libs]
|
||||
handlers=nullHandler
|
||||
[logger_fetch]
|
||||
level=DEBUG
|
||||
qualname="process"
|
||||
handlers=consoleHandler
|
||||
qualname=fetch
|
||||
propagate=0
|
||||
|
||||
[logger_push]
|
||||
level=DEBUG
|
||||
handlers=consoleHandler
|
||||
qualname=push
|
||||
propagate=0
|
||||
|
||||
[handler_consoleHandler]
|
||||
class=StreamHandler
|
||||
|
@ -24,37 +29,6 @@ level=DEBUG
|
|||
formatter=simpleFormatter
|
||||
args=(sys.stdout,)
|
||||
|
||||
[handler_fileHandlerERROR]
|
||||
class=FileHandler
|
||||
level=WARNING
|
||||
formatter=simpleFormatter
|
||||
args=("./error.log",)
|
||||
|
||||
[handler_fileHandlerDEBUG]
|
||||
class=FileHandler
|
||||
level=DEBUG
|
||||
formatter=simpleFormatter
|
||||
args=("./debug.log",)
|
||||
|
||||
[handler_nullHandler]
|
||||
class=FileHandler
|
||||
level=DEBUG
|
||||
formatter=simpleFormatter
|
||||
args=("/dev/null",)
|
||||
|
||||
|
||||
[formatter_simpleFormatter]
|
||||
format=%(asctime)s %(levelname)s - [%(filename)s : %(funcName)s() : line %(lineno)d] - %(message)s
|
||||
datefmt=
|
||||
|
||||
|
||||
## multitail color sheme
|
||||
## pyml / python
|
||||
# colorscheme:pyml:www.obp.net
|
||||
# cs_re:blue:\[[^ ]*\]
|
||||
# cs_re:red:CRITICAL:*
|
||||
# cs_re:red,black,blink:ERROR:*
|
||||
# cs_re:blue:NOTICE:*
|
||||
# cs_re:cyan:INFO:*
|
||||
# cs_re:green:DEBUG:*
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ import logging.config
|
|||
#import string
|
||||
#import operator
|
||||
#import inspect
|
||||
from Queue import Queue
|
||||
|
||||
from pypopush import PypoPush
|
||||
from pypofetch import PypoFetch
|
||||
|
@ -66,10 +67,6 @@ logging.config.fileConfig("logging.cfg")
|
|||
# loading config file
|
||||
try:
|
||||
config = ConfigObj('config.cfg')
|
||||
POLL_INTERVAL = float(config['poll_interval'])
|
||||
PUSH_INTERVAL = float(config['push_interval'])
|
||||
LS_HOST = config['ls_host']
|
||||
LS_PORT = config['ls_port']
|
||||
except Exception, e:
|
||||
print 'Error loading config file: ', e
|
||||
sys.exit()
|
||||
|
@ -135,41 +132,23 @@ if __name__ == '__main__':
|
|||
g.selfcheck()
|
||||
|
||||
logger = logging.getLogger()
|
||||
loops = 0
|
||||
|
||||
if options.test:
|
||||
g.test_api()
|
||||
sys.exit()
|
||||
|
||||
|
||||
if options.fetch_scheduler:
|
||||
pf = PypoFetch()
|
||||
while True:
|
||||
try: pf.fetch('scheduler')
|
||||
except Exception, e:
|
||||
print e
|
||||
sys.exit()
|
||||
q = Queue()
|
||||
|
||||
if (loops%2 == 0):
|
||||
logger.info("heartbeat")
|
||||
loops += 1
|
||||
time.sleep(POLL_INTERVAL)
|
||||
pp = PypoPush(q)
|
||||
pp.start()
|
||||
|
||||
if options.push_scheduler:
|
||||
pp = PypoPush()
|
||||
while True:
|
||||
try: pp.push('scheduler')
|
||||
except Exception, e:
|
||||
print 'PUSH ERROR!! WILL EXIT NOW:('
|
||||
print e
|
||||
sys.exit()
|
||||
pf = PypoFetch(q)
|
||||
pf.start()
|
||||
|
||||
if (loops%60 == 0):
|
||||
logger.info("heartbeat")
|
||||
|
||||
loops += 1
|
||||
time.sleep(PUSH_INTERVAL)
|
||||
pp.join()
|
||||
pf.join()
|
||||
|
||||
"""
|
||||
if options.check:
|
||||
try: g.check_schedule()
|
||||
except Exception, e:
|
||||
|
@ -179,4 +158,4 @@ if __name__ == '__main__':
|
|||
try: pf.cleanup('scheduler')
|
||||
except Exception, e:
|
||||
print e
|
||||
sys.exit()
|
||||
"""
|
||||
|
|
|
@ -9,35 +9,39 @@ import random
|
|||
import string
|
||||
import json
|
||||
import telnetlib
|
||||
import math
|
||||
from threading import Thread
|
||||
|
||||
from api_clients import api_client
|
||||
from util import CueFile
|
||||
|
||||
from configobj import ConfigObj
|
||||
|
||||
# configure logging
|
||||
logging.config.fileConfig("logging.cfg")
|
||||
|
||||
# loading config file
|
||||
try:
|
||||
config = ConfigObj('config.cfg')
|
||||
POLL_INTERVAL = float(config['poll_interval'])
|
||||
PUSH_INTERVAL = 0.5
|
||||
#PUSH_INTERVAL = float(config['push_interval'])
|
||||
LS_HOST = config['ls_host']
|
||||
LS_PORT = config['ls_port']
|
||||
POLL_INTERVAL = 5
|
||||
|
||||
except Exception, e:
|
||||
print 'Error loading config file: ', e
|
||||
sys.exit()
|
||||
|
||||
class PypoFetch:
|
||||
def __init__(self):
|
||||
class PypoFetch(Thread):
|
||||
def __init__(self, q):
|
||||
Thread.__init__(self)
|
||||
self.api_client = api_client.api_client_factory(config)
|
||||
self.cue_file = CueFile()
|
||||
self.set_export_source('scheduler')
|
||||
self.queue = q
|
||||
|
||||
def set_export_source(self, export_source):
|
||||
self.export_source = export_source
|
||||
self.cache_dir = config["cache_dir"] + self.export_source + '/'
|
||||
self.schedule_file = self.cache_dir + 'schedule.pickle'
|
||||
self.schedule_tracker_file = self.cache_dir + "schedule_tracker.pickle"
|
||||
|
||||
"""
|
||||
Fetching part of pypo
|
||||
|
@ -48,10 +52,8 @@ class PypoFetch:
|
|||
- runs the cleanup routine, to get rid of unused cashed files
|
||||
"""
|
||||
def fetch(self, export_source):
|
||||
"""
|
||||
wrapper script for fetching the whole schedule (in json)
|
||||
"""
|
||||
logger = logging.getLogger()
|
||||
#wrapper script for fetching the whole schedule (in json)
|
||||
logger = logging.getLogger('fetch')
|
||||
|
||||
try: os.mkdir(self.cache_dir)
|
||||
except Exception, e: pass
|
||||
|
@ -65,30 +67,29 @@ class PypoFetch:
|
|||
except Exception, e: logger.error("%s", e)
|
||||
|
||||
# prepare the playlists
|
||||
if config["cue_style"] == 'pre':
|
||||
try: self.prepare_playlists_cue()
|
||||
except Exception, e: logger.error("%s", e)
|
||||
elif config["cue_style"] == 'otf':
|
||||
try: self.prepare_playlists(self.export_source)
|
||||
except Exception, e: logger.error("%s", e)
|
||||
try:
|
||||
playlists = self.prepare_playlists()
|
||||
except Exception, e: logger.error("%s", e)
|
||||
|
||||
|
||||
scheduled_data = dict()
|
||||
scheduled_data['playlists'] = playlists
|
||||
scheduled_data['schedule'] = self.schedule
|
||||
self.queue.put(scheduled_data)
|
||||
|
||||
# cleanup
|
||||
try: self.cleanup(self.export_source)
|
||||
except Exception, e: logger.error("%s", e)
|
||||
|
||||
def get_schedule(self):
|
||||
logger = logging.getLogger()
|
||||
logger = logging.getLogger('fetch')
|
||||
status, response = self.api_client.get_schedule()
|
||||
|
||||
if status == 1:
|
||||
logger.info("dump serialized schedule to %s", self.schedule_file)
|
||||
schedule = response['playlists']
|
||||
stream_metadata = response['stream_metadata']
|
||||
try:
|
||||
schedule_file = open(self.schedule_file, "w")
|
||||
pickle.dump(schedule, schedule_file)
|
||||
schedule_file.close()
|
||||
|
||||
self.schedule = schedule
|
||||
tn = telnetlib.Telnet(LS_HOST, LS_PORT)
|
||||
|
||||
#encode in latin-1 due to telnet protocol not supporting utf-8
|
||||
|
@ -96,7 +97,7 @@ class PypoFetch:
|
|||
tn.write(('vars.station_name %s\n' % stream_metadata['station_name']).encode('latin-1'))
|
||||
|
||||
tn.write('exit\n')
|
||||
logger.debug(tn.read_all())
|
||||
tn.read_all()
|
||||
|
||||
except Exception, e:
|
||||
logger.error("Exception %s", e)
|
||||
|
@ -104,45 +105,22 @@ class PypoFetch:
|
|||
|
||||
return status
|
||||
|
||||
#TODO this is a duplicate function!!!
|
||||
def load_schedule(self):
|
||||
logger = logging.getLogger()
|
||||
schedule = None
|
||||
|
||||
# create the file if it doesnt exist
|
||||
if (not os.path.exists(self.schedule_file)):
|
||||
logger.debug('creating file ' + self.schedule_file)
|
||||
open(self.schedule_file, 'w').close()
|
||||
else:
|
||||
# load the schedule from cache
|
||||
#logger.debug('loading schedule file '+self.schedule_file)
|
||||
try:
|
||||
schedule_file = open(self.schedule_file, "r")
|
||||
schedule = pickle.load(schedule_file)
|
||||
schedule_file.close()
|
||||
|
||||
except Exception, e:
|
||||
logger.error('%s', e)
|
||||
|
||||
return schedule
|
||||
|
||||
|
||||
"""
|
||||
Alternative version of playout preparation. Every playlist entry is
|
||||
pre-cued if neccessary (cue_in/cue_out != 0) and stored in the
|
||||
playlist folder.
|
||||
file is eg 2010-06-23-15-00-00/17_cue_10.132-123.321.mp3
|
||||
"""
|
||||
def prepare_playlists_cue(self):
|
||||
logger = logging.getLogger()
|
||||
def prepare_playlists(self):
|
||||
logger = logging.getLogger('fetch')
|
||||
|
||||
# Load schedule from disk
|
||||
schedule = self.load_schedule()
|
||||
schedule = self.schedule
|
||||
playlists = dict()
|
||||
|
||||
# Dont do anything if schedule is empty
|
||||
if (not schedule):
|
||||
if not schedule:
|
||||
logger.debug("Schedule is empty.")
|
||||
return
|
||||
return playlists
|
||||
|
||||
scheduleKeys = sorted(schedule.iterkeys())
|
||||
|
||||
|
@ -173,14 +151,10 @@ class PypoFetch:
|
|||
elif int(playlist['subtype']) > 0 and int(playlist['subtype']) < 5:
|
||||
ls_playlist = self.handle_media_file(playlist, pkey)
|
||||
|
||||
# write playlist file
|
||||
plfile = open(self.cache_dir + str(pkey) + '/list.lsp', "w")
|
||||
plfile.write(json.dumps(ls_playlist))
|
||||
plfile.close()
|
||||
logger.info('ls playlist file written to %s', self.cache_dir + str(pkey) + '/list.lsp')
|
||||
|
||||
playlists[pkey] = ls_playlist
|
||||
except Exception, e:
|
||||
logger.info("%s", e)
|
||||
return playlists
|
||||
|
||||
def handle_media_file(self, playlist, pkey):
|
||||
"""
|
||||
|
@ -189,7 +163,7 @@ class PypoFetch:
|
|||
"""
|
||||
ls_playlist = []
|
||||
|
||||
logger = logging.getLogger()
|
||||
logger = logging.getLogger('fetch')
|
||||
for media in playlist['medias']:
|
||||
logger.debug("Processing track %s", media['uri'])
|
||||
|
||||
|
@ -252,7 +226,7 @@ class PypoFetch:
|
|||
|
||||
|
||||
def handle_remote_file(self, media, dst, do_cue):
|
||||
logger = logging.getLogger()
|
||||
logger = logging.getLogger('fetch')
|
||||
if do_cue == False:
|
||||
if os.path.isfile(dst):
|
||||
logger.debug("file already in cache: %s", dst)
|
||||
|
@ -301,7 +275,7 @@ class PypoFetch:
|
|||
Cleans up folders in cache_dir. Look for modification date older than "now - CACHE_FOR"
|
||||
and deletes them.
|
||||
"""
|
||||
logger = logging.getLogger()
|
||||
logger = logging.getLogger('fetch')
|
||||
|
||||
offset = 3600 * int(config["cache_for"])
|
||||
now = time.time()
|
||||
|
@ -323,3 +297,18 @@ class PypoFetch:
|
|||
print e
|
||||
logger.error("%s", e)
|
||||
|
||||
def run(self):
|
||||
loops = 0
|
||||
heartbeat_period = math.floor(30/POLL_INTERVAL)
|
||||
logger = logging.getLogger('fetch')
|
||||
|
||||
while True:
|
||||
if loops % heartbeat_period == 0:
|
||||
logger.info("heartbeat")
|
||||
loops = 0
|
||||
try: self.fetch('scheduler')
|
||||
except Exception, e:
|
||||
logger.error('Pypo Fetch Error, exiting: %s', e)
|
||||
sys.exit()
|
||||
time.sleep(POLL_INTERVAL)
|
||||
loops += 1
|
||||
|
|
152
pypo/pypopush.py
152
pypo/pypopush.py
|
@ -7,29 +7,37 @@ import pickle
|
|||
import telnetlib
|
||||
import calendar
|
||||
import json
|
||||
import math
|
||||
from threading import Thread
|
||||
|
||||
from api_clients import api_client
|
||||
from util import CueFile
|
||||
|
||||
from configobj import ConfigObj
|
||||
|
||||
# configure logging
|
||||
logging.config.fileConfig("logging.cfg")
|
||||
|
||||
# loading config file
|
||||
try:
|
||||
config = ConfigObj('config.cfg')
|
||||
POLL_INTERVAL = float(config['poll_interval'])
|
||||
PUSH_INTERVAL = 0.5
|
||||
#PUSH_INTERVAL = float(config['push_interval'])
|
||||
LS_HOST = config['ls_host']
|
||||
LS_PORT = config['ls_port']
|
||||
PUSH_INTERVAL = 2
|
||||
except Exception, e:
|
||||
print 'Error loading config file: ', e
|
||||
logger.error('Error loading config file %s', e)
|
||||
sys.exit()
|
||||
|
||||
class PypoPush:
|
||||
def __init__(self):
|
||||
class PypoPush(Thread):
|
||||
def __init__(self, q):
|
||||
Thread.__init__(self)
|
||||
self.api_client = api_client.api_client_factory(config)
|
||||
self.cue_file = CueFile()
|
||||
self.set_export_source('scheduler')
|
||||
self.queue = q
|
||||
|
||||
self.schedule = dict()
|
||||
self.playlists = dict()
|
||||
|
||||
"""
|
||||
push_ahead2 MUST be < push_ahead. The difference in these two values
|
||||
|
@ -42,7 +50,6 @@ class PypoPush:
|
|||
def set_export_source(self, export_source):
|
||||
self.export_source = export_source
|
||||
self.cache_dir = config["cache_dir"] + self.export_source + '/'
|
||||
self.schedule_file = self.cache_dir + 'schedule.pickle'
|
||||
self.schedule_tracker_file = self.cache_dir + "schedule_tracker.pickle"
|
||||
|
||||
"""
|
||||
|
@ -52,41 +59,46 @@ class PypoPush:
|
|||
then liquidsoap is asked (via telnet) to reload and immediately play it.
|
||||
"""
|
||||
def push(self, export_source):
|
||||
logger = logging.getLogger()
|
||||
logger = logging.getLogger('push')
|
||||
|
||||
self.schedule = self.load_schedule()
|
||||
playedItems = self.load_schedule_tracker()
|
||||
if not self.queue.empty():
|
||||
scheduled_data = self.queue.get()
|
||||
self.schedule = scheduled_data['schedule']
|
||||
self.playlists = scheduled_data['playlists']
|
||||
|
||||
tcoming = time.localtime(time.time() + self.push_ahead)
|
||||
tcoming2 = time.localtime(time.time() + self.push_ahead2)
|
||||
schedule = self.schedule
|
||||
playlists = self.playlists
|
||||
|
||||
|
||||
str_tcoming_s = "%04d-%02d-%02d-%02d-%02d-%02d" % (tcoming[0], tcoming[1], tcoming[2], tcoming[3], tcoming[4], tcoming[5])
|
||||
str_tcoming2_s = "%04d-%02d-%02d-%02d-%02d-%02d" % (tcoming2[0], tcoming2[1], tcoming2[2], tcoming2[3], tcoming2[4], tcoming2[5])
|
||||
|
||||
currently_on_air = False
|
||||
if self.schedule == None:
|
||||
logger.warn('Unable to loop schedule - maybe write in progress?')
|
||||
logger.warn('Will try again in next loop.')
|
||||
if schedule:
|
||||
playedItems = self.load_schedule_tracker()
|
||||
|
||||
else:
|
||||
for pkey in self.schedule:
|
||||
timenow = time.time()
|
||||
tcoming = time.localtime(timenow + self.push_ahead)
|
||||
str_tcoming_s = "%04d-%02d-%02d-%02d-%02d-%02d" % (tcoming[0], tcoming[1], tcoming[2], tcoming[3], tcoming[4], tcoming[5])
|
||||
|
||||
tcoming2 = time.localtime(timenow + self.push_ahead2)
|
||||
str_tcoming2_s = "%04d-%02d-%02d-%02d-%02d-%02d" % (tcoming2[0], tcoming2[1], tcoming2[2], tcoming2[3], tcoming2[4], tcoming2[5])
|
||||
|
||||
tnow = time.localtime(timenow)
|
||||
str_tnow_s = "%04d-%02d-%02d-%02d-%02d-%02d" % (tnow[0], tnow[1], tnow[2], tnow[3], tnow[4], tnow[5])
|
||||
|
||||
for pkey in schedule:
|
||||
plstart = pkey[0:19]
|
||||
start = self.schedule[pkey]['start']
|
||||
end = self.schedule[pkey]['end']
|
||||
start = schedule[pkey]['start']
|
||||
end = schedule[pkey]['end']
|
||||
|
||||
playedFlag = (pkey in playedItems) and playedItems[pkey].get("played", 0)
|
||||
|
||||
if plstart == str_tcoming_s or (plstart < str_tcoming_s and plstart > str_tcoming2_s and not playedFlag):
|
||||
logger.debug('Preparing to push playlist scheduled at: %s', pkey)
|
||||
playlist = self.schedule[pkey]
|
||||
playlist = schedule[pkey]
|
||||
|
||||
ptype = playlist['subtype']
|
||||
currently_on_air = True
|
||||
|
||||
# We have a match, replace the current playlist and
|
||||
# force liquidsoap to refresh.
|
||||
if (self.push_liquidsoap(pkey, self.schedule, ptype) == 1):
|
||||
if (self.push_liquidsoap(pkey, schedule, playlists) == 1):
|
||||
logger.debug("Pushed to liquidsoap, updating 'played' status.")
|
||||
# Marked the current playlist as 'played' in the schedule tracker
|
||||
# so it is not called again in the next push loop.
|
||||
|
@ -100,39 +112,27 @@ class PypoPush:
|
|||
|
||||
# Call API to update schedule states
|
||||
logger.debug("Doing callback to server to update 'played' status.")
|
||||
self.api_client.notify_scheduled_item_start_playing(pkey, self.schedule)
|
||||
|
||||
if self.schedule != None:
|
||||
tnow = time.localtime(time.time())
|
||||
str_tnow_s = "%04d-%02d-%02d-%02d-%02d-%02d" % (tnow[0], tnow[1], tnow[2], tnow[3], tnow[4], tnow[5])
|
||||
for pkey in self.schedule:
|
||||
start = self.schedule[pkey]['start']
|
||||
end = self.schedule[pkey]['end']
|
||||
self.api_client.notify_scheduled_item_start_playing(pkey, schedule)
|
||||
|
||||
start = schedule[pkey]['start']
|
||||
end = schedule[pkey]['end']
|
||||
|
||||
if start <= str_tnow_s and str_tnow_s < end:
|
||||
currently_on_air = True
|
||||
|
||||
else:
|
||||
logger.debug('Empty schedule')
|
||||
|
||||
if not currently_on_air:
|
||||
tn = telnetlib.Telnet(LS_HOST, LS_PORT)
|
||||
tn.write('source.skip\n'.encode('latin-1'))
|
||||
tn.write('source.skip\n')
|
||||
tn.write('exit\n')
|
||||
tn.read_all()
|
||||
#logger.info('source.skip')
|
||||
#logger.debug(tn.read_all())
|
||||
|
||||
def push_liquidsoap(self, pkey, schedule, ptype):
|
||||
logger = logging.getLogger()
|
||||
src = self.cache_dir + str(pkey) + '/list.lsp'
|
||||
def push_liquidsoap(self, pkey, schedule, playlists):
|
||||
logger = logging.getLogger('push')
|
||||
|
||||
try:
|
||||
if True == os.access(src, os.R_OK):
|
||||
logger.debug('OK - Can read playlist file')
|
||||
|
||||
pl_file = open(src, "r")
|
||||
file_content = pl_file.read()
|
||||
pl_file.close()
|
||||
logger.debug('file content: %s' % (file_content))
|
||||
playlist = json.loads(file_content)
|
||||
playlist = playlists[pkey]
|
||||
|
||||
#strptime returns struct_time in local time
|
||||
#mktime takes a time_struct and returns a floating point
|
||||
|
@ -180,43 +180,24 @@ class PypoPush:
|
|||
except Exception, e:
|
||||
logger.error('%s', e)
|
||||
status = 0
|
||||
|
||||
return status
|
||||
|
||||
|
||||
def load_schedule(self):
|
||||
logger = logging.getLogger()
|
||||
schedule = None
|
||||
|
||||
# create the file if it doesnt exist
|
||||
if (not os.path.exists(self.schedule_file)):
|
||||
logger.debug('creating file ' + self.schedule_file)
|
||||
open(self.schedule_file, 'w').close()
|
||||
else:
|
||||
# load the schedule from cache
|
||||
#logger.debug('loading schedule file '+self.schedule_file)
|
||||
try:
|
||||
schedule_file = open(self.schedule_file, "r")
|
||||
schedule = pickle.load(schedule_file)
|
||||
schedule_file.close()
|
||||
|
||||
except Exception, e:
|
||||
logger.error('%s', e)
|
||||
|
||||
return schedule
|
||||
|
||||
|
||||
def load_schedule_tracker(self):
|
||||
logger = logging.getLogger()
|
||||
logger = logging.getLogger('push')
|
||||
logger.debug('load_schedule_tracker')
|
||||
playedItems = dict()
|
||||
|
||||
# create the file if it doesnt exist
|
||||
if (not os.path.exists(self.schedule_tracker_file)):
|
||||
logger.debug('creating file ' + self.schedule_tracker_file)
|
||||
schedule_tracker = open(self.schedule_tracker_file, 'w')
|
||||
pickle.dump(playedItems, schedule_tracker)
|
||||
schedule_tracker.close()
|
||||
try:
|
||||
logger.debug('creating file ' + self.schedule_tracker_file)
|
||||
schedule_tracker = open(self.schedule_tracker_file, 'w')
|
||||
pickle.dump(playedItems, schedule_tracker)
|
||||
schedule_tracker.close()
|
||||
except Exception, e:
|
||||
logger.error('Error creating schedule tracker file: %s', e)
|
||||
else:
|
||||
logger.debug('schedule tracker file exists, opening: ' + self.schedule_tracker_file)
|
||||
try:
|
||||
schedule_tracker = open(self.schedule_tracker_file, "r")
|
||||
playedItems = pickle.load(schedule_tracker)
|
||||
|
@ -226,3 +207,18 @@ class PypoPush:
|
|||
|
||||
return playedItems
|
||||
|
||||
def run(self):
|
||||
loops = 0
|
||||
heartbeat_period = math.floor(30/PUSH_INTERVAL)
|
||||
logger = logging.getLogger('push')
|
||||
|
||||
while True:
|
||||
if loops % heartbeat_period == 0:
|
||||
logger.info("heartbeat")
|
||||
loops = 0
|
||||
try: self.push('scheduler')
|
||||
except Exception, e:
|
||||
logger.error('Pypo Push Error, exiting: %s', e)
|
||||
sys.exit()
|
||||
time.sleep(PUSH_INTERVAL)
|
||||
loops += 1
|
||||
|
|
Loading…
Reference in New Issue