From caff375e584fa24590aa4d0da4c0a604cded6cbc Mon Sep 17 00:00:00 2001
From: Michael Fairchild <mfairchild365@gmail.com>
Date: Tue, 26 Aug 2014 11:57:19 -0500
Subject: [PATCH] Implement a bus daemon

---
 scripts/bus-daemon-upstart.conf | 16 ++++++++++++++++
 scripts/bus-daemon.php          | 14 ++++++++++++++
 www/busproxy.php                |  2 +-
 3 files changed, 31 insertions(+), 1 deletion(-)
 create mode 100644 scripts/bus-daemon-upstart.conf
 create mode 100644 scripts/bus-daemon.php

diff --git a/scripts/bus-daemon-upstart.conf b/scripts/bus-daemon-upstart.conf
new file mode 100644
index 00000000..386a7100
--- /dev/null
+++ b/scripts/bus-daemon-upstart.conf
@@ -0,0 +1,16 @@
+description "maps.unl.edu bus location proxy"
+author "mfairchild365@gmail.com"
+# used to be: start on startup
+# until we found some mounts weren't ready yet while booting:
+start on runlevel [2345]
+stop on runlevel [06]
+# Automatically Respawn:
+respawn
+respawn limit 15 5
+script
+#configure the path
+exec php /var/www/html/scripts/bus-daemon.php >> /var/log/bus_daemon.log 2>&1
+end script
+post-start script
+# Optionally put a script here that will notify you node has (re)started
+end script
\ No newline at end of file
diff --git a/scripts/bus-daemon.php b/scripts/bus-daemon.php
new file mode 100644
index 00000000..729d4d11
--- /dev/null
+++ b/scripts/bus-daemon.php
@@ -0,0 +1,14 @@
+<?php
+/**
+ * This daemon runs the bus proxy script every second to generate the json file.
+ * This is helpful for when the server is under heavy load to ensure that the script is only ever ran once every second.
+ * 
+ * If you use this daemon, be sure that busproxy.php is aliased in apache to serve bus.json.
+ */
+
+$project_root = dirname(__DIR__);
+
+while(true) {
+    $result = shell_exec("php $project_root/www/busproxy.php");
+    sleep(1);
+}
diff --git a/www/busproxy.php b/www/busproxy.php
index f85a525e..7d739096 100644
--- a/www/busproxy.php
+++ b/www/busproxy.php
@@ -1,7 +1,7 @@
 <?php
 
 $url = 'http://getonboard.lincoln.ne.gov/packet/json/vehicle?routes=138,139&lastVehicleHttpRequestTime=0';
-$file = 'bus.json';
+$file = __DIR__ . '/bus.json';
 $seconds_to_cache = 1;
 
 if (!file_exists($file) || time() - $seconds_to_cache >= filemtime($file)) {
-- 
GitLab