ActiveResourceをUNIX Domain Socketで使ってみる


APIHTTPlocalhostAPIPHP-FPMunix domain socket
 http://inside.pixiv.net/blog/2012/11/08/pixiv-dot-comfalsequan-ti-xiang/  



ActiveResource  UNIX Domain Socket 




 Rails 
% rails new crazyresource -J -O
% cd crazyresource

Gemfile 
source 'https://rubygems.org'
gem 'rails', '3.2.11'
gem 'activeresource'

bundle install 
% bundle install --path vendor/bundle

 scaffold bookmark 
% bundle exec rails generate scaffold bookmark title:string url:string comment:text

 http://localhost:8080 
% vi app/models/bookmark.rb
class Bookmark < ActiveResource::Base
  self.site = 'http://localhost:8080/'
end


 8080  nginx 
% mkdir nginx
% mkdir nginx/logs
% mkdir nginx/html
% echo '[{"comment":"Google Search","id":1,"title":"Google","url":"http://www.google.com/"}]' > nginx/html/bookmarks.json
% vi nginx/nginx.conf

nginx.conf 
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    server {
        listen       8080;
        server_name  localhost;

        location / {
            root   html;
        }
    }
}

nginx 
% nginx -p $PWD/nginx -c nginx.conf
% curl "http://localhost:8080/bookmarks.json"
[{"comment":"Google Search","id":1,"title":"Google","url":"http://www.google.com/"}]


 Rails 
% bundle exec rails server


% curl "http://localhost:3000/bookmarks.json"
[{"bookmark":{"comment":"Google Search","id":1,"title":"Google","url":"http://www.google.com/"}}]


nginx  UNIX Domain Socket 
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    server {
        listen       unix:/tmp/nginx.sock;
        server_name  localhost;

        location / {
            root   html;
        }
    }
}

nginx 
% nginx -s stop
% nginx -p $PWD/nginx -c nginx.conf

curl "http://localhost:3000/bookmarks.json" 




unix_socket_hack 使


UNIXSocket - walf443's blog


Gemfile bundle install 
gem 'unix_socket_hack', :git => 'git://github.com/walf443/unix_socket_hack.git'

 Net::HTTP  unix_sock_hack  https://github.com/walf443/unix_socket_hack/issues/2  vendor/bundle/ruby/1.9.1/bundler/gems/unix_socket_hack-93e97dea2425/lib/unix_socket_hack.rb 

localhost:8080  TCPSocket  /tmp/nginx.sock  UNIXSocket  config/application.rb 
require 'unix_socket_hack'
UNIXSocketHack.apply({ 'localhost:8080' => '/tmp/nginx.sock' })

Rails 
% curl "http://localhost:3000/bookmarks.json"
[{"bookmark":{"comment":"Google Search","id":1,"title":"Google","url":"http://www.google.com/"}}]

nginx  unix:  UNIX Domain Socket 
% tail -f nginx/logs/access.log
unix: - - [20/Jan/2013:18:10:23 +0900] "GET /bookmarks.json HTTP/1.1" 200 85 "-" "Ruby"

Macに最初から入ってるmemcachedを常時起動するようにした


Mac  memcached Lion 

 Lion  memcached 


https://gist.github.com/1462820




~/Library/LaunchAgents/com.danga.memcached.plist


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>KeepAlive</key>
  <true/>
  <key>Label</key>
  <string>memcached</string>
  <key>Program</key>
  <string>/usr/bin/memcached</string>
  <key>RunAtLoad</key>
  <true/>
  <key>UserName</key>
  <string>ユーザー名</string>
</dict>
</plist>


launchctl load -w ~/Library/LaunchAgents/com.danga.memcached.plist

RunAtLoad=true 

MacBook AirのCPU温度を取得する


Mac OS X 
ioreg -n AppleSmartBattery

CPU

IOKit使C smc.h  smc.c OK


https://bitbucket.org/elventear/fan-control/src


Mac OS X 10.5 (Leopard) IOConnectMethodStructureIStructureOIOConnectCallStructMethod




http://d.hatena.ne.jp/bootblack/20090610

http://d.hatena.ne.jp/bootblack/20120603/p1

http://www.geocities.jp/freeggggroup/nf/  "ClamNF.app v5.5.9 for Mac (OS X 10.8 Mountain Lion)"  "ClamNF.app v5.5.5 for Mac OS X 10.5.8" 


.app  Gist  GPL 


https://gist.github.com/4049362



clang -framework IOKit -o smc smc.c

使
$ ./smc -h
Apple System Management Control (SMC) tool 0.01_2
Usage:
./smc [options]
    -f         : fan info decoded
    -h         : help
    -k <key>   : key to manipulate
    -l         : list all keys and values
    -r         : read the value of a key
    -w <value> : write the specified value to a key
    -v         : version
$ ./smc -f
Total fans in system: 1

Fan #0:
    Actual speed : 1997
    Minimum speed: 2000
    Maximum speed: 6500
    Safe speed   : 0
    Target speed : 2000
    Mode         : auto
    Temp         = 55.375
    Temp TB0T        = 32.6875
    Temp TC0D        = 55.375
    Temp TC0P        = 50.5
    Temp TM0P        = 46.875
    Temp TN0P        = 0
    Temp Th0H        = 0
    Temp Ts0P        = 30.625
    Temp TN1P        = 0
    Temp Th1H        = 35.375

CPU55.375


おまけ

GrowthForecastに送ってみた。

GrowthForecast.plで自分ロギングしてみた




GrowthForecastPerl


http://kazeburo.github.com/GrowthForecast/


使MacRRDtoolVirtualBox使

使HTTPPOST





GrowthForecast

MacObjective-CXCodeVim

CObjective-C


https://gist.github.com/3762129



clang sample.m -o sample -framework Cocoa -lcurl


GrowthForecastlibcurl使
#include <curl/curl.h>

...

size_t curl_write_callback_func(char* ptr, size_t size, size_t nmemb, void* nothing) {
    return 0;
}

int post(char *name, int num) {
    char url[MAXLINE];
    sprintf(url, "http://localhost:5126/api/me/log/%s", name);

    char postdata[MAXLINE];
    sprintf(postdata, "number=%d", num);

    printf("%s: %d\n", name, num);

    CURL *curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_callback_func);
    CURLcode res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);

    return res == CURLE_OK;
}


MacCGEventTapAPI使
#import <Cocoa/Cocoa.h>
#include <time.h>

...

CGEventRef catchEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* nothing) {

    // http://stackoverflow.com/questions/4727149/application-randomly-stops-receiving-key-presses-cgeventtaps
    if (type == kCGEventTapDisabledByTimeout) {
        CGEventTapEnable(eventTap, true);
    }

    if (type == kCGEventKeyDown) { /* to listen for key events, run this program as root */
        int key = CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);

        printf("%d\n", key);
        count_key++;
    }
    else if (type == kCGEventMouseMoved || type == kCGEventScrollWheel) {
        count_mouse++;
    }

    return event;
}

int main() {
    CFRunLoopSourceRef runLoopSource;

    eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, 0, kCGEventMaskForAllEvents, catchEventCallback, NULL);

    runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);

    CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);

    CGEventTapEnable(eventTap, true);
 
    ...

    CFRunLoopRun();

    return 0;
}

root

 CGEventTap StackOverflowSnow Leopard kCGEventTapDisabledByTimeout  CGEventTap 


GrowthForecastPOSTCFRunLoopTimerRef使
#include <time.h>
#include <dispatch/dispatch.h>

...

void postEventsAsync(int num_key, int num_mouse) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
        post("key", num_key);
        post("mouse", num_mouse);
    });
}

void recordCountsCallback(CFRunLoopTimerRef timer, void* nothing) {
    time_t this_time;
    struct tm * timeinfo;

    time(&this_time);
    timeinfo = localtime(&this_time);
    timeinfo->tm_sec = 0;
    this_time = mktime(timeinfo);

    if (this_time == prev_time) {
        // within less than 60 sec (do nothing)
        return;
    } else if (prev_time == 0) {
        // first time (set prev_time to this_time)
    } else if (difftime(this_time, prev_time) >= 120.0) {
        // come back from sleep (discard)
        count_key = 0;
        count_mouse = 0;

        postEventsAsync(count_key, count_mouse);
    } else {
        // more than 60 sec (record)
        postEventsAsync(count_key, count_mouse);

        count_key = 0;
        count_mouse = 0;
    }
    prev_time = this_time;
}

int main() {

    ...

    CFRunLoopTimerRef timer = CFRunLoopTimerCreate(kCFAllocatorDefault, 0, 1.0, 0, 0, recordCountsCallback, NULL);
    CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopDefaultMode);

    CFRunLoopRun();

    return 0;
}

Grand Central Dispatch 使HTTP


Happy Life Logging!

ニョーン


 pixiv.js yksk
7686:     // マウスオーバーでおすすめタグをニョーンと出す


7629:         // TODO: pixiv.storage.localStorage の使い方が分かりにくいのでとりあえず window.localStorage を使う

pixiv.storage.localStorage 使 window.localStorage 使




UI - sandbox


pixivIE7


宣伝

↑僕もけっこう書きました。

pixivに入社しました





331GEEK DAY TOKYOWebGL15

ワクワクエンジニアリング




GEEK DAY TOKYO





OK

331GEEK DAY TOKYO
http://geekday.jp/

↓100人までだそうですが、すぐ埋まりそうな感じですね。


それからこのブログ並びに僕のオンラインでの活動は全て個人的な表現活動であり、雇用者の意見と必ずしも一致するものではありません。

学校辞めた




11