larigira.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """
  2. This module is for the main application logic
  3. """
  4. from __future__ import print_function
  5. from gevent import monkey
  6. monkey.patch_all(subprocess=True)
  7. import sys
  8. import os
  9. import tempfile
  10. import signal
  11. from time import sleep
  12. import logging
  13. import logging.config
  14. import subprocess
  15. import gevent
  16. from gevent.pywsgi import WSGIServer
  17. from .mpc import Controller, get_mpd_client
  18. from .config import get_conf
  19. from .rpc import create_app
  20. def on_main_crash(*args, **kwargs):
  21. print('A crash occurred in "main" greenlet. Aborting...')
  22. sys.exit(1)
  23. class Larigira(object):
  24. def __init__(self):
  25. self.log = logging.getLogger("larigira")
  26. self.conf = get_conf()
  27. self.controller = Controller(self.conf)
  28. self.controller.link_exception(on_main_crash)
  29. self.http_server = WSGIServer(
  30. ("", int(self.conf["HTTP_PORT"])), create_app(self.controller.q, self)
  31. )
  32. def start(self):
  33. self.controller.start()
  34. self.http_server.start()
  35. def sd_notify(ready=False, status=None):
  36. args = ["systemd-notify"]
  37. if ready:
  38. args.append("--ready")
  39. if status is not None:
  40. args.append("--status")
  41. args.append(status)
  42. try:
  43. subprocess.check_call(args)
  44. except:
  45. pass
  46. def main():
  47. tempfile.tempdir = os.environ["TMPDIR"] = os.path.join(
  48. os.getenv("TMPDIR", "/tmp"), "larigira.%d" % os.getuid()
  49. )
  50. if not os.path.isdir(os.environ["TMPDIR"]):
  51. os.makedirs(os.environ["TMPDIR"])
  52. if get_conf()["LOG_CONFIG"]:
  53. logging.config.fileConfig(
  54. get_conf()["LOG_CONFIG"], disable_existing_loggers=True
  55. )
  56. else:
  57. log_format = "%(asctime)s|%(levelname)s[%(name)s:%(lineno)d] %(message)s"
  58. logging.basicConfig(
  59. level=logging.DEBUG if get_conf()["DEBUG"] else logging.INFO,
  60. format=log_format,
  61. datefmt="%H:%M:%S",
  62. )
  63. if get_conf()["MPD_WAIT_START"]:
  64. while True:
  65. try:
  66. get_mpd_client(get_conf())
  67. except Exception:
  68. logging.debug("Could not connect to MPD, waiting")
  69. sd_notify(status="Waiting MPD connection")
  70. sleep(int(get_conf()["MPD_WAIT_START_RETRYSECS"]))
  71. else:
  72. logging.info("MPD ready!")
  73. sd_notify(ready=True, status="Ready")
  74. break
  75. larigira = Larigira()
  76. larigira.start()
  77. def sig(*args):
  78. print("invoked sig", args)
  79. larigira.controller.q.put(dict(kind="signal", args=args))
  80. for signum in (signal.SIGHUP, signal.SIGALRM):
  81. gevent.signal(signum, sig, signum)
  82. gevent.wait()
  83. if __name__ == "__main__":
  84. main()