Install LetsEncrypt for Multiple Sites in Nginx

Turn on one site at a time and install new certificate. 

Once all certificates are installed you can turn on all the sites. 

The installation client may be confused when more than one site are hosted on the same server during domain validation process.

Posted in Ubuntu | Tagged , | Comments Off on Install LetsEncrypt for Multiple Sites in Nginx

Playing with TensorFlow

It’s a great tool!

Posted in General | Comments Off on Playing with TensorFlow

Phottix Odin for Sony

The TCU and receiver work great on HVL-F56AM, HVL-F58AM and HVL-F60M with TTL, Manual and HSS. EV adjustment in Group A, B and C work too.

Tested in both a700 and a99.

Posted in General | Tagged , , | Comments Off on Phottix Odin for Sony

“Synchronous” MongoDB Calls with Fibers

Here I’m showing an example of converting MongoDB node driver into “synchronous” APIs from callback based asynchronous APIs.

We define two new classes to wrap up the native Db and Cursor. Two methods are converted: Db.Open and Cursor.toArray. The “sync” call wraps a callback with Fiber run/yield that will block in the fiber until callback is returned.

  1 Fiber = require 'fibers'
  2 mongodb = require 'mongodb'
  3 server = new mongodb.Server 'dev', 27017
  4
  5 Function::sync = (that, args) ->
  6   args = if args then Array::slice.call(args) else []
  7   fiber = Fiber.current
  8   args.push((err, res) ->
  9     if err then console.log err
 10     fiber.run(err || res)
 11   )
 12  
 13   this.apply(that, args)
 14  
 15   result = Fiber.yield()
 16   if result instanceof Error then throw result
 17   return result
 18  
 19 Cursor = (@cursor) ->
 20
 21 Cursor::toArray = ->
 22   return this.cursor.toArray.sync(this.cursor)
 23  
 24 Db = (@db) ->
 25
 26 Db::open = ->
 27   return this.db.open.sync(this.db)
 28  
 29 Fiber((name) ->
 30   db = new Db(new mongodb.Db('test', server, {w:1})).open()
 31   sequences = new mongodb.Collection db, 'sequences'
 32   s = new Cursor(sequences.find({_id: name})).toArray()
 33   console.log s
 34 ).run('userid')
 35
Posted in JavaScript, Node.js, NoSQL | Tagged , , | Comments Off on “Synchronous” MongoDB Calls with Fibers

Node-Fibers Example

With node-fibers, we can write codes in traditionally synchronous fashion and avoid deep nested callback calls without blocking the Node event loop.

Below is an example to generate Fibonacci series. Each run resumes the calculation loop and returns a number to the calling code.

  1 Fiber = require 'fibers'
  2
  3 Fibonacci = ->
  4   return Fiber(->
  5     Fiber.yield 0
  6     prev = 0
  7     curr = 1
  8     while true
  9       Fiber.yield curr
 10       tmp = prev + curr
 11       prev = curr
 12       curr = tmp
 13   )
 14
 15 seq  = Fibonacci()
 16 i = 0
 17 while i < 100
 18   i = seq.run()
 19   console.log i
 20 #coffee fibonacci.coffee
 21 #0
 22 #1
 23 #1
 24 #2
 25 #3
 26 #5
 27 #8
 28 #13
 29 #21
 30 #34
 31 #55
 32 #89
 33 #144
Posted in JavaScript, Node.js | Tagged , | Comments Off on Node-Fibers Example

Mindful of CoffeeScript Function Return Value

Here’s a gotcha if you’re not careful when writing CoffeeScript. Notice the generated js code where constructor now returns this._cursor!!

#c.coffee
Cursor = (cursor) ->
  this._cursor = cursor
0 // c.js
1 // Generated by CoffeeScript 1.6.2
2 (function() {
3   var Cursor;
4
5   Cursor = function(cursor) {
6     return this._cursor = cursor;
7   };
8
9 }).call(this);

Here’s the fix.

#c.coffee
Cursor = (cursor) ->
  this._cursor = cursor
  this
0 // c.js
1 // Generated by CoffeeScript 1.6.2
2 (function() {
3   var Cursor;
4
5   Cursor = function(cursor) {
6     this._cursor = cursor;
7     return this;
8   };
9
10 }).call(this);
Posted in General, JavaScript | Tagged | Comments Off on Mindful of CoffeeScript Function Return Value

MongoDB Test2

Using a two-core server generating enough traffics and achived over 8000 ticks/s. mongod runs 65% CPU time.

Posted in Node.js, NoSQL, Ubuntu | Tagged | Comments Off on MongoDB Test2

MongoDB Test 1

Simple test in simulated ticks: single node process had throughput of 2394 ticks per second. mongod runs at 44% CPU usage.

Posted in Node.js, NoSQL, Ubuntu | Tagged | Comments Off on MongoDB Test 1

MongoDB Build

Spent a few hours building a simple MongoDB cluster with Digital Ocean VPS. Three nodes: Primary, Secondary and Arbiter. Primary node is located in New York and Secondary node is in San Francisco. Arbiter is also in New York leveraging an existing web server.

Total monthly cost: $15 with 512MB RAM and 20GB SSD on each node, which is way cheaper than buying a MongoDB service from the cloud.

Ping time between NY and SF is about 75ms. Ping time in the same region is less than 1ms.

Everything works so far. Will start testing the cluster…

Posted in Node.js, NoSQL, Ubuntu | Tagged | Comments Off on MongoDB Build

Encapsulation in Coffee Script

m.coffee
---------
publicMethod1 = ->
  @publicMember1 = "public member 1"
  @publicMember2 = "public member 2"
  console.log 'publicMethod1***'
  console.log '*' + @publicMember1

publicMethod2 = ->
  console.log 'publicMethod2***'
  @publicMethod1()
  console.log '*' + @publicMember2

class Foo
  constructor: ->
    @fooPublicMember1 = "Foo public member 1"
    @fooPublicMember2 = "Foo public member 2"
    privateMember1 = "private member 1"
    privateMember2 = "private member 2"

    privateMethod = ->
      console.log 'FooPrivateMethod***'
      console.log '*' + privateMember1

    @privilegedMethod = ->
      console.log 'FooPrivilegedMethod***'
      privateMethod()
      console.log '*' + module.exports.publicMember1
      console.log '*' + @fooPublicMember1
      console.log '*' + privateMember2

  publicMethod: ->
    console.log 'FooPublicMethod***'
    console.log '*' + module.exports.publicMember2
    console.log '*' + @fooPublicMember2
    @privilegedMethod()

module.exports =
  publicMethod1: publicMethod1
  publicMethod2: publicMethod2
  Foo: Foo

c.coffee
--------
m = require './m'

m.publicMethod1()
m.publicMethod2()
console.log m.publicMember1

foo = new m.Foo
foo.privilegedMethod()
foo.publicMethod()
console.log foo.fooPublicMember1

results
--------
publicMethod1***
*public member 1
publicMethod2***
publicMethod1***
*public member 1
*public member 2
public member 1
FooPrivilegedMethod***
FooPrivateMethod***
*private member 1
*public member 1
*Foo public member 1
*private member 2
FooPublicMethod***
*public member 2
*Foo public member 2
FooPrivilegedMethod***
FooPrivateMethod***
*private member 1
*public member 1
*Foo public member 1
*private member 2
Foo public member 1
Posted in Node.js | Tagged | Comments Off on Encapsulation in Coffee Script