SlideShare une entreprise Scribd logo
1  sur  28
Télécharger pour lire hors ligne
実践で使える拙作npmモジュール
twitter.com/maaash
github.com/mash
無職

なのに実践って??
1. imagemagick-native
https://github.com/mash/node-imagemagick-native
Imagemagick's Magick++ binding for Node.
% brew install imagemagick
% npm install imagemagick-native
1. imagemagick-native
サムネイルの動的生成
いろいろなサイズの画像へのリクエストがある、
UI改善するために異なるサイズを試したい
× 画像アップロード直後にサムネイルを生成
○ サムネイルへのリクエストが来てから画像を生成
1. imagemagick-native
サムネイルの動的生成
1. HTTPリクエストを受ける
2. 画像ストレージ(S3など)から元画像を取得
3. サムネイルを生成
4. 保存(memcachedなど)
5. レスポンスを返す
1. imagemagick-native
サムネイルの動的生成
1. HTTPリクエストを受ける
2. 画像ストレージ(S3など)から元画像を取得 (IO)
3. サムネイルを生成 (CPU)
4. 保存(memcachedなど) (IO)
5. レスポンスを返す
IOで詰まったら同期IOなworkerは詰まっちゃうんじゃ?
→ node.jsでやったらどうか
1. imagemagick-native
% npm install imagemagick
実体は
require(“child_process”).spawn(“convert”,
args);
% npm install imagemagick-native
ImageMagickのC++ Bindingを使う
imagemagick: 8.07ms per iteration
imagemagick-native: 0.87ms per iteration
app.get( "/*", function (req, res) {
async.waterfall([
function (options, callback) {
var requestOptions = parse( req.url );
// download from s3
request.get( requestOptions, function(err,res,body) {
callback( null, body, resizeOptions );
});
},
function (body, resizeOptions, callback) {
var buf;
try {
buf = imagemagick.convert( resizeOptions );
} catch (e) {
// handle error
};
callback( null, buf );
},
function (buf) {
// store
cache.set(key, buf);
},
function (buf) {
// respond
res.end(buf);
callback( null );
}
], errorHandler );
});
1. imagemagick-native
API群
* convert
* identify
* quantizeColors
* composite

サイズ、形式の変換
サイズや形式を識別
量子化
合成
1. imagemagick-native
API群
* convert
* identify
* quantizeColors
* composite

自分がつくったのはこれだけ
pullreq
pullreq
pullreq
1. imagemagick-native

うれしい
2. angel
https://github.com/mash/node-angel
gracefully restart multi process net.Servers
% npm install angel
2. angel

昨日 npm publish しました
2. angel
じわじわ系メモリリーク
数10MB/月
2. angel

おれのせいじゃないよ〜><
って言いたい
2. angel

定期的にcronで殺したりして(しないか)
2. angel

max_requests_per_child
一定のリクエスト数をこなした
workerはいったん殺してforkしなおす
これができるのが見当たらなかったので
作ってみたのが angel
2. angel
net.Serverのインスタンスとportを渡すと
CPU数分、プロセス起動してlistenしてくれる
* workerが突然死したら生き返らせてくれる
* HUPシグナルをmasterに送るとworkerを生き返らせ
てくれる
* その時モジュールを読み込みなおせる
=無停止デプロイ
CC BY-NC pedra_uding
http://www.flickr.com/photos/petra_uding/6166961931/
2. angel

名前がかっこいいですね
2. angel
var angel = require("angel");
var server = require("http").createServer( function(req, res)
{
res.writeHead(200);
res.end("Hello, World!");
});
angel( app, {
port:
refresh_modules_regexp:
max_requests_per_child:
workers:
});

3000,
"(config|lib)/",
10000,
4
2. angel
% angel --port 3000 
--workers 4 
--max-requests-per-child 10000 
--refresh-modules-regexp “lib/”
app.js
% cat app.js
var server = require("http").createServer( function(req, res)
{
res.writeHead(200);
res.end("Hello, World!");
});
module.exports = server;
2. angel
% node eg/server.js
master[70402] created pid_file: angel.pid
master[70402] master will fork 4 workers
master[70402] forked worker[70403]
master[70402] forked worker[70404]
master[70402] forked worker[70405]
master[70402] forked worker[70406]
worker[70404] launched
worker[70404] listening on 3000
worker[70406] launched
worker[70406] listening on 3000
worker[70403] launched
worker[70405] launched
worker[70403] listening on 3000
worker[70405] listening on 3000
2. angel
% pstree -p 70402
| |
-+= 55828 mash /bin/zsh
| |
-+= 70402 mash node eg/server.js
| |
|--- 70403 mash snip/proj/node-angel/eg/server.js
| |
|--- 70404 mash snip/proj/node-angel/eg/server.js
| |
|--- 70405 mash snip/proj/node-angel/eg/server.js
| |
--- 70406 mash snip/proj/node-angel/eg/server.js
2. angel
master[70402]
master[70402]
master[70402]
master[70402]
master[70402]
master[70402]
worker[70560]
worker[70560]
worker[70561]
worker[70561]
worker[70559]
worker[70559]
worker[70562]
worker[70562]
worker[70403]
master[70402]
worker[70404]
worker[70405]
master[70402]
master[70402]
worker[70406]
master[70402]

SIGHUP
reloaded snip/proj/node-angel/eg/app.js
forked worker[70559]
forked worker[70560]
forked worker[70561]
forked worker[70562]
launched
listening on 3000
launched
listening on 3000
launched
listening on 3000
launched
listening on 3000
closes
worker[70403] died
closes
closes
worker[70405] died
worker[70404] died
closes
worker[70406] died
2. angel

名前がかっこいいですね
2. angel

pullreq お待ちしております
Thank you
twitter.com/maaash
github.com/mash

Contenu connexe

En vedette

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

En vedette (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

Nodefest2013 my npm modules