Sunday, January 29, 2017

postgresql Cluster

List All Cluster:

    root@debian:~# pg_lsclusters
    Ver Cluster Port Status Owner    Data directory               Log file
    9.6 main    5432 online postgres /var/lib/postgresql/9.6/main     /var/log/postgresql/postgresql-9.6-main.log

Find Status of given instance:


    root@debian:~# pg_ctlcluster 9.6 main  status
    pg_ctl: server is running (PID: 6062)
    /usr/lib/postgresql/9.6/bin/postgres "-D" "/var/lib/postgresql/9.6/main" "-c" "config_file=/etc/postgresql/9.6/main/postgresql.conf"

Kill Process:

    sudo kill -SIGHUP  6062

Change Status of instance – start/stop:

    pg_ctlcluster 9.6 main  stop
    pg_ctlcluster 9.6 main  start
    pg_ctlcluster 9.6 main  restart

View Cluster Configuration:

    root@debian:~# pg_ctlcluster 9.6 main  status
    pg_ctl: server is running (PID: 6062)
    /usr/lib/postgresql/9.6/bin/postgres "-D" "/var/lib/postgresql/9.6/main"     "-c" "config_file=/etc/postgresql/9.6/main/postgresql.conf"
    root@debian:~# ^C
    root@debian:~# pg_conftool 9.6 main show all
    cluster_name = '9.6/main'
    data_directory = '/var/lib/postgresql/9.6/main'
    datestyle = 'iso, mdy'
    default_text_search_config = pg_catalog.english
    dynamic_shared_memory_type = posix
    external_pid_file = '/var/run/postgresql/9.6-main.pid'
    hba_file = '/etc/postgresql/9.6/main/pg_hba.conf'
    ident_file = '/etc/postgresql/9.6/main/pg_ident.conf'
    lc_messages = en_IN
    lc_monetary = en_IN
    lc_numeric = en_IN
    lc_time = en_IN
    log_line_prefix = '%t [%p-%l] %q%u@%d '
    log_timezone = localtime
    max_connections = 100
    port = 5432
    shared_buffers = 128MB
    ssl = true
    ssl_cert_file = '/etc/ssl/certs/ssl-cert-snakeoil.pem'
    ssl_key_file = '/etc/ssl/private/ssl-cert-snakeoil.key'
    stats_temp_directory = '/var/run/postgresql/9.6-main.pg_stat_tmp'
    timezone = localtime
    unix_socket_directories = '/var/run/postgresql'

Create New Cluster:
     pg_createcluster 9.6 new_cluster

DROP Cluster:
     pg_dropcluster --stop 9.6 main

Update Cluster:
     pg_upgradecluster 9.5 main

    it upgrades to next version

Reload Configuration:
      pg_ctlcluster 9.6 main reload

Restart Postgres Service:
    root@debian:~# systemctl reload postgresql
CHECK if Postgres is Listensing :
    root@debian:~# pg_isready
    /var/run/postgresql:5432 - accepting connections

Sunday, January 15, 2017

Listing all files recursively using bluebird promise

var Promise = require("bluebird");
var fs = Promise.promisifyAll(require('fs'));
var Path = require('path');
var dir = '/home/test_user/pictures/sample';


var ext = ['.txt', '.jpg'];
let files = [];

function walk(dir, ext) {
    return fs.readdirAsync(dir).map(function (fileName) {
        fileName = Path.join(dir, fileName);
        var extname = Path.extname(fileName);

        if ((ext.indexOf(extname) > -1) || (extname == '')) {
            return fs.statAsync(fileName)
                .then(function (stats) {
                    return stats.isDirectory() ? walk(fileName, ext) : fileName;
                })
        } else {
            return false
        }
    }).reduce(function (a, b) {
        if (b == false) {
            return a;
        } else {
            return a.concat(b);
        }
    }, []);
}

var res = walk(dir, ext).then(function (v) {
    console.log(JSON.stringify(v));
});

Saturday, January 14, 2017

Listing files & directories in given folder using node.js synchroniously

Here is my code for getting list of directories & sub-directories & files within each of it for given path.

var fs = require("fs");
var path = require('path');
var op = new Object();

var basePath = '/home/test_user/pictures/samples/';

function myCb(opt){
    if(op[opt.path] == null){
        op[opt.path] ={
            files:null,
            directories:null
        }
        op[opt.path].files = opt.files
        op[opt.path].directories = opt.dirs
    }

    for(var j=0;j< opt.dirs.length;j++){
        walkSync(opt.dirs[j],myCb);
    }
}

function walkSync(searchPath,cb) {
    var pathList = fs.readdirSync(searchPath);

    var fileList=[];
    var dirList=[];

    for (var i = 0; i < pathList.length; i++) {
        var walkPath = path.join(searchPath, pathList[i]);
        var stat = fs.statSync(walkPath);
        if (stat.isFile()) {
            fileList.push(walkPath);
        }
        else if (stat.isDirectory()) {
            dirList.push(walkPath);
        }
    }

    var output ={
        "path":searchPath,
        "files":fileList,
        "dirs":dirList
    }

    cb(output);
}

walkSync(basePath,myCb);
console.log(JSON.stringify(op));

output is an array of object of type

"source_folder_path" : 


{
    "/home/test_user/pictures/samples/": {
        "files": [],
        "directories": [
            "/home/test_user/pictures/samples/1",
            "/home/test_user/pictures/samples/2"
        ]
    },
    "/home/test_user/pictures/samples/1": {
        "files": [
            "/home/test_user/pictures/samples/1/IMG_1942067a4_235124.jpg",
            "/home/test_user/pictures/samples/1/IMG_1942067a4_235138.jpg",
            "/home/test_user/pictures/samples/1/IMG_1942067a7_213012.jpg",
            "/home/test_user/pictures/samples/1/IMG_1942067a7_213020.jpg",
            "/home/test_user/pictures/samples/1/IMG_1942067a7_213022.jpg",
            "/home/test_user/pictures/samples/1/IMG_1942067a7_213034.jpg"
                ],
        "directories": []
    },
    "/home/test_user/pictures/samples/2": {
        "files": [
                      "/home/test_user/pictures/samples/2/IMG_20151218_232553.jpg"
        ],
        "directories": [
            "/home/test_user/pictures/samples/2/4"
        ]
    },
    "/home/test_user/pictures/samples/2/4": {
        "files": [
            "/home/test_user/pictures/samples/2/4/IMG_1942067a8_000623.jpg",
            "/home/test_user/pictures/samples/2/4/IMG_1942067a8_000626.jpg",
            "/home/test_user/pictures/samples/2/4/IMG_19420123_200014.jpg",
            "/home/test_user/pictures/samples/2/4/IMG_19420123_200018.jpg",
            "/home/test_user/pictures/samples/2/4/IMG_19420206_267a022.jpg",
            "/home/test_user/pictures/samples/2/4/IMG_19420214_215517.jpg",
            "/home/test_user/pictures/samples/2/4/IMG_19420214_215650.jpg"
        ],
        "directories": []
    }
}