25
Copyright © 2016 M/Gateway Developments Ltd EWD 3 Training Course Part 29 Running QEWD as a Service Rob Tweed Director, M/Gateway Developments Ltd Twitter: @rtweed

EWD 3 Training Course Part 29: Running QEWD as a Service

Embed Size (px)

Citation preview

Page 1: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

EWD 3 Training CoursePart 29

Running QEWD as a Service

Rob TweedDirector, M/Gateway Developments Ltd

Twitter: @rtweed

Page 2: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

The QEWD Node.js process

• So far we've run it in a Command Prompt or Terminal window– This OK for demonstration– But not for production

• It needs to run as a service– Automatically start at boot time– Restart on error– Pipe console.log to a log file

Page 3: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Running QEWD as a Service

• Different approaches needed for– Windows– Linux

• Many different options available, but the following are two that seem to work well

Page 4: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Running QEWD as a Service

• Windows:– NSSM

• Linux– PM2

Page 5: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Windows: NSSM

• http://nssm.cc/

• Free Service Manager for Windows• Works very well with Node.js and QEWD

• Click download and get the latest version– 2.24 (at time of writing these notes)

Page 6: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Install NSSM

• Drill down into the downloaded file– Downloads/nssm-2.24.zip

• Find the /win64 directory– Downloads/nssm-2.24.zip/nssm-2.24/win64/nssm.exe

• Copy nssm.exe to a directory that is already in your PATH for executables, eg:– C:\Program Files\nodejs

Page 7: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Log file for piped output

• Create a suitable directory, eg:– C:\qewd\log

• Create an empty file in it:– qewd.log

Page 8: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Dependency on Caché

• When NSSM starts up, it must wait to make sure Caché has already started– We need the dependency information

• Control Panel:– System and Security

• Administrative Tools– Services

Page 9: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Dependency on Caché

• In the Services Window that opens up, find the Service named:– Caché Controller for Cachexxxx

• Right click on it and select Properties• At the top you'll see the Service Name

– eg: Cache c- intersystems_cache2015-2• Copy this to your clipboard

Page 10: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Start NSSM Installer• In a Command Prompt window type:

nssm install• Application Path:

– C:\Program Files\nodejs\node.exe• Startup Directory:

– C:\ewd3 (or wherever you start ewd-xpress)• Arguments:

– ewd-xpress• Click on Dependencies Tab

– Paste the Caché Service string from clipboard

• Click on I/O tab– Input: leave blank– Output: C:\qewd\log\qewd.log– Error: C:\qewd\log\qewd.log

• Give your new service a name:– QEWD

• Click Install Service

Page 11: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Start the QEWD Service• Switch to the Services window• Click Refresh button• QEWD should appear in the list of services• Right-click on it and select Properties

– Service Status will be stopped– Click Start

• QEWD should now be running– Test by starting the qewd-monitor application

Page 12: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Log File

• Look in C:\qewd\log\qewd.log• You should see all the console.log activity

being piped there

Page 13: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Controlling the Service• Standard Windows Service• Use the Services panel and find QEWD

– Stop– Start– Restart

• If an error occurs that stops the master process, the QEWD service will automatically restart

• The QEWD service will start automatically when Windows is re-booted

Page 14: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

NSSM additional features

• Lots more functionality• Worth exploring documentation at

http://nssm.cc

• To edit your service definition:– Open Command Prompt window

nssm edit QEWD

Page 15: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Linux: PM2

• Advanced Node.js Process Manager– http://pm2.keymetrics.io/

• Install:

– -g option makes pm2 available as a command

cd ~sudo npm install –g pm2

Page 16: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Start QEWD as a Service

cd ~/qewdpm2 start qewd.js

Assuming that qewd.js is the name of yourQEWD startup file.

Page 17: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Start QEWD as a Service

cd ~/qewdpm2 start qewd.js

This will be now the PM2 Service name

Page 18: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Monitor PM2 Services

cd ~/qewdpm2 status

Displays the status of all your PM2 Services- online- stopped

Page 19: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Stop the QEWD PM2 Service

cd ~/qewdpm2 stop qewd

Page 20: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Restart the QEWD PM2 Service

cd ~/qewdpm2 start qewd

If you've already started QEWD as a service,and if it appears as stopped in pm2 status,

you don't need to specify the .js file extension

Page 21: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Restart the QEWD PM2 Service

cd ~/qewdpm2 restart qewd

To stop and start QEWD again in 1 command

Page 22: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Logging the output

• PM2 automatically pipes console.log and error output to log files:

– ~/qewd/.pm2/logs

– You should find:• ~/.pm2/logs/qewd-out-0.log

Page 23: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Logging the output

• PM2 automatically pipes console.log and error output to log files:

– ~/qewd/.pm2/logs

– You should find:• ~/.pm2/logs/qewd-out-0.log

This number may vary

Page 24: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

Logging the output

• Use the tail command to monitor QEWD activity in real-time

tail –f ~/.pm2/logs/qewd-out-0.log

Page 25: EWD 3 Training Course Part 29: Running QEWD as a Service

Copyright © 2016 M/Gateway Developments Ltd

PM2 will auto-restart QEWD

• If an error brings down the QEWD master process, PM2 will automatically restart it

• Try stopping the master process using qewd-monitor while tailing the log file– Watch it stop and immediately restart