Install redis as server in ubuntu

Darktalker Post in Programming,Tags: ,
3

Step 1. download latest stable version of redis, and install it

cd /tmp
wget http://redis.googlecode.com/files/redis-2.2.12.tar.gz
tar -zxf redis-2.2.12.tar.gz
cd redis-2.2.12
make
sudo make install

It will install in global:

mkdir -p /usr/local/bin
cp -p redis-server /usr/local/bin
cp -p redis-benchmark /usr/local/bin
cp -p redis-cli /usr/local/bin
cp -p redis-check-dump /usr/local/bin
cp -p redis-check-aof /usr/local/bin

Continue reading “Install redis as server in ubuntu” »

VN:F [1.9.11_1134]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.11_1134]
Rating: +2 (from 2 votes)

Control Node.Js server using upstart/monit

Darktalker Post 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.11_1134]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.11_1134]
Rating: 0 (from 2 votes)

Php header code

Darktalker Post in Programming,Tags: ,
1

PHP header codes, for reference only:

// Use this header instruction to fix 404 headers
// produced by url rewriting...
header('HTTP/1.1 200 OK');

// Page was not found:
header('HTTP/1.1 404 Not Found');

// Access forbidden:
header('HTTP/1.1 403 Forbidden');

// The page moved permanently should be used for
// all redrictions, because search engines know
// what's going on and can easily update their urls.
header('HTTP/1.1 301 Moved Permanently');

// Server error
header('HTTP/1.1 500 Internal Server Error');

// Redirect to a new location:
header('Location: http://www.example.org/');

// Redriect with a delay:
header('Refresh: 10; url=http://www.example.org/');
print 'You will be redirected in 10 seconds';

// you can also use the HTML syntax:
// <meta http-equiv="refresh" content="10;http://www.example.org/ />

// override X-Powered-By value
header('X-Powered-By: PHP/4.4.0');
header('X-Powered-By: Brain/0.6b');

// content language (en = English)
header('Content-language: en');

// last modified (good for caching)
$time = time() - 60; // or filemtime($fn), etc
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');

// header for telling the browser that the content
// did not get changed
header('HTTP/1.1 304 Not Modified');

// set content length (good for caching):
header('Content-Length: 1234');

// Headers for an download:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
header('Content-Transfer-Encoding: binary');
// load the file to send:
readfile('example.zip');

// Disable caching of the current document:
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Pragma: no-cache');

// set content type:
header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/plain'); // plain text file
header('Content-Type: image/jpeg'); // JPG picture
header('Content-Type: application/zip'); // ZIP file
header('Content-Type: application/pdf'); // PDF file
header('Content-Type: audio/mpeg'); // Audio MPEG (MP3,...) file
header('Content-Type: application/x-shockwave-flash'); // Flash animation

// show sign in box
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Top Secret"');
print 'Text that will be displayed if the user hits cancel or ';
print 'enters wrong login data';
VN:F [1.9.11_1134]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.11_1134]
Rating: +1 (from 1 vote)

dojo based Theme for YUIDOC

Darktalker Post in Programming,Tags: , ,
0

I just created a dojo based YUIDOC theme, it’s similar to Dojotoolkit Api.

demo can be found here: YUI Api Doc

Feature:

  • Ajax based explorer, easy to navigate
  • Multi-languages support.
  • use SyntaxHighlighter to highlight syntax

It has some performance issue in Firefox < 4 for huge project (like YUI), and IE <9 is not supported (well, not tested :) )

your feedback is much appreciated :)

source code can be found here: theme-darktalker

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

get millisecond & unique timestampe in php

Darktalker Post in Programming,Tags: ,
0

To get millisecond:

(float)microtime(true);

To get unique timestampe:

function uniqueTimeStamp()
    {
        $Asec = explode(" ", microtime());
        $Amicro = explode(".", $Asec[0]);
        return ($Asec[1] . substr($Amicro[1], 0, 4));
    }
VN:F [1.9.11_1134]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.11_1134]
Rating: +1 (from 1 vote)

preg_replace_callback in class

Darktalker Post in Programming,Tags:
0

variable scope in PHP is painful. not like java or javascript, you can’t get preg_replace_callback works like this:

class test{
private $a = 'test';
public function foo($data){
   $a = $this->a;
   function nested($matches){
        return $matches[1].$a;
   }
   preg_replace_callback('/href=(.*?)/i','nested',$data);
}
}

what you have to do is like this:

class test{
private $a = 'test';
public function foo($data){
   $a = $this->a;
   $nested = function nested($matches){
        return $matches[1].$a;
   }
   preg_replace_callback('/href=(.*?)/i',array(get_class($this), 'bar'),$data);
}
private function bar($matches){
   return $matches[1].$this->a;
}
}
VN:F [1.9.11_1134]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.11_1134]
Rating: +1 (from 1 vote)

Manually trigger a DOM event

Darktalker Post 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.11_1134]
Rating: 5.0/5 (2 votes cast)
VN:F [1.9.11_1134]
Rating: +1 (from 1 vote)

How to reindex “Catalog Search Index” via script

Darktalker Post in Programming,Tags: ,
0

Sometimes we have problem of reindexing via magento’s admin such as latency, we prefer doing it directly via script.

Continue reading “How to reindex “Catalog Search Index” via script” »

VN:F [1.9.11_1134]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.11_1134]
Rating: -1 (from 3 votes)

Add password protection for your site

Darktalker Post in Programming,Tags: ,
0

This post will describe a simple technique to secure your virtual host with basic HTTP password protection provided by Apache. This can be useful if you want to keep users away from the webstore during development

Continue reading “Add password protection for your site” »

VN:F [1.9.11_1134]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.11_1134]
Rating: +1 (from 1 vote)

How to load an image and do callback

Darktalker Post 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.11_1134]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.11_1134]
Rating: +1 (from 1 vote)