Formerly, the initial record pull was based on the current time of the server minus five minutes. This caused timing issues, specifically in the case where the database server and dbmonitor server had different timezones set. The initial record is now grabbed based on the timestamp of the last record insertted into the table. If there are no records in the database 0000-01-01 00:00:00 is used as the initial timestamp.
55 lines
2.1 KiB
CoffeeScript
55 lines
2.1 KiB
CoffeeScript
database = require './config/database.coffee'
|
|
application = require './config/application.coffee'
|
|
|
|
moment = require 'moment'
|
|
fs = require 'fs'
|
|
|
|
sockets = []
|
|
|
|
db = require('mysql-native').createTCPClient(database.host, database.port)
|
|
db.auth database.database, database.username, database.password
|
|
db.auto_prepare = true
|
|
|
|
checkDate = moment('0000-01-01 00:00:00', 'YYYY-MM-DD HH:mm:ss')
|
|
query = "SELECT " + application.timestamp_column + " FROM `print_jobs` ORDER BY " + application.timestamp_column + " DESC LIMIT 1"
|
|
db.query(query).addListener 'row', (start) ->
|
|
checkDate = moment(start[application.timestamp_column], 'YYYY-MM-DD HH:mm:ss')
|
|
|
|
getDatabaseUpdates = () ->
|
|
query = "SELECT " + application.timestamp_column + "," + application.columns.join(',') + " FROM " + application.table + " WHERE " + application.where + " AND " + application.timestamp_column + " > '" + checkDate.format('YYYY-MM-DD HH:mm:ss') + "'"
|
|
db.query(query).addListener 'row', (job) ->
|
|
jobDate = moment(job[application.timestamp_column], 'YYYY-MM-DD HH:mm:ss')
|
|
checkDate = jobDate if jobDate > checkDate
|
|
updateClient job
|
|
|
|
updateClient = (job) ->
|
|
for client in sockets
|
|
client.emit 'update', job
|
|
|
|
http = require('http').createServer((req, res) ->
|
|
if req.url == '/'
|
|
res.writeHead 200, {'Content-Type': 'text/html'}
|
|
res.end fs.readFileSync('public/index.html')
|
|
else if req.url == '/css/style.css'
|
|
res.writeHead 200, {'Content-Type': 'text/css'}
|
|
res.end fs.readFileSync('public/css/style.css')
|
|
else if req.url == '/js/jquery.mustache.js'
|
|
res.writeHead 200, {'Content-Type': 'text/javascript'}
|
|
res.end fs.readFileSync('public/js/jquery.mustache.js')
|
|
else if req.url == '/templates/views.html'
|
|
res.writeHead 200, {'Content-Type': 'text/html'}
|
|
res.end fs.readFileSync('public/templates/views.html')
|
|
else
|
|
res.writeHead 404, {"Content-Type": "text/plain"}
|
|
res.end "404 Not Found\n"
|
|
).listen application.port
|
|
|
|
io = require('socket.io').listen(http)
|
|
io.set('log level', 1);
|
|
io.sockets.on 'connection', (socket) ->
|
|
sockets.push socket
|
|
|
|
setInterval () ->
|
|
getDatabaseUpdates()
|
|
, 1000 * application.updateTime
|