11
2- //var events = require('events');
3- //import events from 'events';
4-
5- //var server = require("./modules/server");
6- //var router = require("./modules/router");
7- //var requestHandlers = require("./modules/requestHandlers");
8- //var Queue = require("./modules/queue.js");
9- //var gpioworker = require("./modules/gpioworker");
10-
112import start from "./modules/server" ;
12- //import router from "./modules/router";
133import route from "./modules/router" ;
144import { index as rhindex } from "./modules/requestHandlers" ;
155import { favicon as rhfavicon } from "./modules/requestHandlers" ;
@@ -18,6 +8,19 @@ import { ws as rhws } from "./modules/requestHandlers";
188import Queue from "./modules/queue" ;
199import { gpioWorker as gpioworker } from "./modules/gpioworker" ;
2010
11+ var fs = require ( "fs" ) ;
12+ var path = require ( "path" ) ;
13+
14+ var DEFAULT_PORT = 8888 ;
15+ var DEFAULT_JOBCHECK_INTERVAL = 60 ;
16+ var PIDDIR = "/var/run/brewable" ;
17+
18+
19+ console . log ( "pid=" + process . pid ) ;
20+ if ( fs . existsSync ( PIDDIR ) ) {
21+ process . umask ( 0 ) ;
22+ fs . writeFile ( path . join ( PIDDIR , "pid" ) , process . pid . toString ( ) , { mode :parseInt ( '0644' , 8 ) } ) ;
23+ }
2124
2225// Command line options
2326var options = { } ;
@@ -28,7 +31,7 @@ process.argv.forEach((val) => {
2831 var opt = val . split ( '=' ) ;
2932 options [ opt [ 0 ] ] = opt [ 1 ] ;
3033 }
31- if ( val == "help" ) {
34+ if ( val == "help" || val == "--help" ) {
3235 showUsage ( ) ;
3336 process . exit ( ) ;
3437 }
@@ -37,31 +40,30 @@ process.argv.forEach((val) => {
3740if ( "port" in options ) {
3841 options . port = parseInt ( options . port ) ;
3942} else {
40- options . port = 8888 ;
43+ options . port = DEFAULT_PORT ;
4144}
42- //console.log("options: " + JSON.stringify(options));
4345
4446if ( "interval" in options ) {
4547 options . jobCheckInterval = parseInt ( options . interval ) ;
4648} else {
47- options . jobCheckInterval = 30 ; // seconds
49+ options . jobCheckInterval = DEFAULT_JOBCHECK_INTERVAL ; // seconds
4850}
49- //console.log("options: " + JSON.stringify(options));
5051
5152function showUsage ( ) {
5253 console . log ( "\nUsage:" ) ;
53- console . log ( " brewable [options]" ) ;
54+ console . log ( " \x1b[4mbrewable [options]\x1b[0m " ) ;
5455 console . log ( "\nOptions:" ) ;
55- console . log ( " port =<n>" ) ;
56+ console . log ( " \x1b[4mport =<n>\x1b[0m " ) ;
5657 console . log ( " where <n> is a valid port number (default is 8888)" ) ;
57- console . log ( "\n interval =<n>" ) ;
58+ console . log ( "\n \x1b[4minterval =<n>\x1b[0m " ) ;
5859 console . log ( " where <n> is the interval, in seconds, between processings of current jobs" ) ;
59- console . log ( " (default is 60)" ) ;
60+ console . log ( " (default is " + DEFAULT_JOBCHECK_INTERVAL + ")" ) ;
61+ console . log ( "\n \x1b[4mhelp\x1b[0m" ) ;
62+ console . log ( " show this usage message." ) ;
6063 console . log ( "\nExample:" ) ;
6164 console . log ( " brewable port=8686 interval=30\n" ) ;
6265}
6366
64- //global.eventEmitter = new events.EventEmitter();
6567import { eventEmitter } from "./modules/gpioworker" ;
6668var clients = [ ] ;
6769
@@ -86,8 +88,6 @@ var messageWaiting = function () {
8688} ;
8789
8890var input_queue = new Queue ( { 'name' :'input_queue' , 'interval' :200 , 'action' :messageWaiting } ) ;
89- //var output_queue = new Queue({'name':'output_queue'});
90- //var output_queue = new Queue({'action':function(){console.log("SOME action!");}});
9191var output_queue = new Queue ( { 'name' :'output_queue' , 'action' :updateClients } ) ;
9292
9393
@@ -109,19 +109,18 @@ input_queue.start();
109109output_queue . start ( ) ;
110110start ( route , handle , clients , input_queue , options ) ;
111111
112+ /* millseconds i.e.
113+ 10000 = 10s
114+ 30000 = 30s
115+ */
112116setInterval ( function ( ) {
113117 //console.log("\nDoing device updates");
114118 worker . updateDevices ( ) ;
115119} , 2000 ) ;
116120
117- /* millseconds i.e.
118- 10000 = 10s
119- 30000 = 30s
120- */
121121setInterval ( function ( ) {
122- console . log ( "jobCheckInterval = " + new Date ( ) ) ;
122+ // console.log("jobCheckInterval = " + new Date());
123123 worker . processRunningJobs ( ) ;
124- //}, (30000 + Math.floor((Math.random() * 1000) + 1)));
125124} , ( ( 1000 * options . jobCheckInterval ) + Math . floor ( ( Math . random ( ) * 1000 ) + 1 ) ) ) ;
126125
127126process . on ( 'SIGHUP' , ( ) => {
@@ -141,5 +140,4 @@ process.on('SIGTERM', () => {
141140} ) ;
142141
143142
144-
145143/* ex:set ai shiftwidth=2 inputtab=spaces smarttab noautotab: */
0 commit comments