2016年2月12日金曜日

node.js サービス化 on ubuntu

ここから抜粋

/etc/init
以下にyourprogram.confとしてファイル作成

#yourprogram.conf
description "node.js server"
author      "kvz - http://kevin.vanzonneveld.net"

# used to be: start on startup
# until we found some mounts weren't ready yet while booting:
start on started mountall
stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

script
    # Not sure why $HOME is needed, but we found that it is:
    export HOME="/root"

    exec sudo -u www-data /usr/bin/node /where/yourprogram.js >> /var/log/node.log 2>&1
end script

post-start script
   # Optionally put a script here that will notifiy you node has (re)started
   # /root/bin/hoptoad.sh "node.js has started!"
end script
 
 
##/usr/local/bin/node → /usr/bin/nodeに修正
## ユーザwww-dataは作成しておく 
 
init-checkconf /etc/init/yourprogram.conf
を実行してみる
failed to ask Upstart to check conf fileと出るが
/tmpフォルダにinit-checkconf-upstart-outputが出力されているので
何もエラーがなければOkのはず
 
 
下記コマンドで実行 
start yourprogram   
停止
stop yourprogram 

いまさらながら Linq to SQL

必要だったのでLinq to SQL 触ってみましたが
いろいろ細かいところが面倒だったので
対応した内容をメモ


 Binary形式の比較ができない


下記のようなクラスを作成し

 public static class BinaryComparer
    {
        public static int Compare(this Binary b1, Binary b2)
        {
            throw new NotImplementedException();
        }
    }


where内で使う

hoge.Where(row =>BinaryComparer.Compare(binary, row.binary_column) == 0) ;



PropertyInfoを使ったカラム指定



public static Expression<Func<TItem, bool>> PropertyEquals<TItem, TValue>(
             PropertyInfo property, TValue value)
        {
            var param = Expression.Parameter(typeof(TItem));
            var body = Expression.Equal(Expression.Property(param, property),
                Expression.Constant(value));
            return Expression.Lambda<Func<TItem, boo>>(body, param);
        }


次のように利用する

hoge.Where(PropertyEquals<hogetype, byte>(propertyInfo, somebyte));


上記だとnullableに対応していないので
nullable対応版

public static Expression<Func<TItem, bool>> NullablePropertyEquals<TItem, TValue>(
             PropertyInfo property, TValue? value) where TValue : struct
        {
            var param = Expression.Parameter(typeof(TItem));
            var body = Expression.Equal(
                    Expression.Property(param, property),
                    Expression.Constant(value, typeof (TValue?)));
            return Expression.Lambda<Func<TItem, bool>>(body, param);
        }

nullableでしか動かないので注意
両方に対応したメソッドも作れるような気はするけど
取りあえずここまで