Posts Tagged ‘javascript’

Control Node.Js server using upstart/monit

Darktalker Posted in Programming,Tags: , ,
0

Environment: debian/ubuntu

This script can monitor cpu/memory usage of your node application, and start/stop it via a web interface or command line

Step 1. install startup and monit (they are normally preinstalled in ubuntu)

sudo apt-get install upstart monit

then edit /etc/defaults/monit, set startup=1

Step 2. in /etc/init/, create a config of your application, ex: your_app.conf with code

#!upstart

description "your_app"
author "Hongbo LU"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel 0

respawn # restart when job dies
respawn limit 5 60 # give up restart after 5 respawns in 60 seconds

script  

exec start-stop-daemon --start --make-pidfile --pidfile /var/run/your_app.pid --chdir /path/to/your/app --chuid user:usergroup --exec NODE_ENV=production /usr/local/bin/node index.js >> /var/log/nodejs/node.log 2>&1

end script

NOTICE:

  • user:usergroup must be the one who owns Node
  • modify the log folder as you wish
  • in order to use relative paths in Node, we must change directory to project’s folder

Step 3. edit file /etc/monit/monitrc, enable web interface

include /etc/monit/conf.d/*
set daemon 120 # Poll at 2-minute intervals
set logfile syslog facility log_daemon
set alert youremail@email.com
set httpd port 2812 and use address localhost
allow localhost   # Allow localhost to connect
allow admin:Monit # Allow Basic Auth

Step 4. create file /etc/monit/conf.d/your_app.monitrc

check process plusnode
    with pidfile "/var/run/your_app.pid"
    start program = "/sbin/start your_app"
    stop program = "/sbin/stop your_app"
    if 2 restarts within 3 cycles then timeout
    if totalmem > 100 Mb then alert
    if children > 255 for 5 cycles then stop
    if cpu usage > 95% for 3 cycles then restart
    if failed port 80 protocol http
	request /valid_url/
	with timeout 5 seconds
    and if failed port 443 type tcpssl protocol http
        request /valid_url/
        with timeout 5 seconds
	then restart

NOTICE:

  • modify your_app as you wish
  • modify server port as you wish
  • monit will try to reach localhost:8080/valid_url to check the server status, so make sure your valid_url is valid :)

Step 5. now you can control your app. via web interface: localhost:2812 or via command:

#upstart command
sudo start your_app
sudo status your_app
sudo stop your_app
#the commands above are shortcuts of initctl:
initctl start your_app
initctl status your_app
#to check if the upstart conf is valid
initctl check-config
#to reload upstart configs
initctl reload-configuration
#monit command
sudo monit
sudo monit start your_app

NOTICE:

  • if your app. stopped, monit will restart it automatically.
VN:F [1.9.16_1159]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.16_1159]
Rating: 0 (from 2 votes)

Manually trigger a DOM event

Darktalker Posted in Programming,Tags: , ,
0

How to manually trigger a DOM event:

/**
 * trigger a DOM event via script
 * @param {Object,String} element a DOM node/node id
 * @param {String} event a given event to be fired - click,dblclick,mousedown,etc.
 */
var fireEvent = function(element, event) {
    var evt;
    var isString = function(it) {
        return typeof it == "string" || it instanceof String;
    }
    element = (isString(element)) ? document.getElementById(element) : element;
    if (document.createEventObject) {
        // dispatch for IE
        evt = document.createEventObject();
        return element.fireEvent('on' + event, evt)
    }
    else {
        // dispatch for firefox + others
        evt = document.createEvent("HTMLEvents");
        evt.initEvent(event, true, true); // event type,bubbling,cancelable
        return !element.dispatchEvent(evt);
    }
}

usage:

fireEvent("node_id","click");
VN:F [1.9.16_1159]
Rating: 5.0/5 (3 votes cast)
VN:F [1.9.16_1159]
Rating: +1 (from 1 vote)

How to load an image and do callback

Darktalker Posted in Programming,Tags: ,
0

How to load an image and do callback

function loadImage(src,callback){
var img = new Image();
    img.onload = function() {
      if (callback) {
         callback(img);
       }
   }
 img.src = src;
}

NOTICE: onload callback should be set before setting src

VN:F [1.9.16_1159]
Rating: 5.0/5 (2 votes cast)
VN:F [1.9.16_1159]
Rating: +2 (from 2 votes)

Improved array functions

Darktalker Posted in Programming,Tags:
0

Regular array functions can not parse associative arrays, here are some improved array functions might meet your need:
Continue reading “Improved array functions” »

VN:F [1.9.16_1159]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.16_1159]
Rating: 0 (from 0 votes)