├── LICENSE ├── MMM-WatchDog.js ├── README.md └── node_helper.js /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Michael Teeuw 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MMM-WatchDog.js: -------------------------------------------------------------------------------- 1 | Module.register("MMM-WatchDog",{ 2 | 3 | // Default module config. 4 | defaults: { 5 | interval: 2, 6 | timeout: 10 7 | }, 8 | 9 | // Override dom generator. 10 | start: function() { 11 | this.sendSocketNotification('SET_CONFIG', this.config); 12 | this.startHeartbeat(); 13 | }, 14 | 15 | // Start the interval to send the PING message. 16 | startHeartbeat: function() { 17 | var self = this; 18 | setInterval(function() { 19 | self.sendSocketNotification('PING', this.config); 20 | }, this.config.interval * 1000); 21 | } 22 | 23 | }); 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MMM-WatchDog 2 | 3 | This MagicMirror² module keeps an eye on your UI and quits app in case the UI crashes. 4 | If you combine this with the [PM2 process manager](https://github.com/MichMich/MagicMirror/wiki/Auto-Starting-MagicMirror#using-pm2), MM2 will automaticly restart after a UI failure. 5 | 6 | ## Installation 7 | 8 | In your terminal, go to your MagicMirror's Module folder: 9 | ```` 10 | cd ~/MagicMirror/modules 11 | ```` 12 | 13 | Clone this repository: 14 | ```` 15 | git clone https://github.com/MichMich/MMM-WatchDog.git 16 | ```` 17 | 18 | Configure the module in your `config.js` file. 19 | 20 | ## Using the module 21 | 22 | To use this module, add it to the modules array in the `config/config.js` file: 23 | ````javascript 24 | modules: [ 25 | { 26 | module: 'MMM-WatchDog', 27 | config: { 28 | // See 'Configuration options' for more information. 29 | } 30 | } 31 | ] 32 | ```` 33 | 34 | ## Configuration options 35 | 36 | The following properties can be configured: 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 52 | 53 | 54 | 55 | 58 | 59 | 60 |
OptionDescription
intervalThe number of seconds between each Heartbeat. 50 |
Default value: 2 51 |
timeoutThe timeout in seconds before the MagicMirror² app quits. 56 |
Default value: 10 57 |
61 | -------------------------------------------------------------------------------- /node_helper.js: -------------------------------------------------------------------------------- 1 | var NodeHelper = require("node_helper"); 2 | module.exports = NodeHelper.create({ 3 | 4 | // Create the timer object. 5 | timer: null, 6 | 7 | // Set the default config. Since the timer should not start 8 | // before the config is set by the frontend, the default can 9 | // be 1 seconds. The real default is set in MMM-WatchDog.js 10 | 11 | config: { 12 | timeout: 1 13 | }, 14 | 15 | // Handle incomming messages. 16 | 17 | socketNotificationReceived: function(notification, payload) { 18 | 19 | // Incoming config. Store config and schedule restart. 20 | if (notification === 'SET_CONFIG') { 21 | this.config = payload; 22 | this.scheduleRestart(); 23 | console.log("WatchDog started. Maximum timeout: " + this.config.timeout + "s."); 24 | } 25 | 26 | // Incoming PING. Reschedule restart. 27 | if (notification === 'PING') { 28 | this.scheduleRestart(); 29 | } 30 | }, 31 | 32 | // Reschedule restart by clearing old timer, and setting a new timer. 33 | scheduleRestart: function() { 34 | clearTimeout(this.timer); 35 | this.timer = setTimeout(this.restart, this.config.timeout * 1000); 36 | }, 37 | 38 | // Quit Node process. 39 | restart: function() { 40 | var now = new Date(); 41 | console.error(now.toString() + ' - WatchDog: Heartbeat timeout. Frontend might have crashed. Exit now.'); 42 | process.exit(1); 43 | } 44 | }); 45 | --------------------------------------------------------------------------------