0
Ruby on Redis	

Pascal Weemaels	

Koen Handekyn	

Oct 2013
Target	

Create a Zip file of PDF’s
based on a CSV data file	

‣  Linear version	

‣  Making it scale with Redis	


parse cs...
Step 1: linear 	

‣  Parse CSV	

•  std lib : require ‘csv’	

•  docs = CSV.read("#{DATA}.csv")
Simple Templating with String Interpolation	

invoice.html	

<<Q	

<div class="title">	

INVOICE #{invoice_nr}	


‣  Merge...
Step 1: linear 	

‣  Create PDF	

•  prince xml using princely gem	

•  http://www.princexml.com	

•  p = Princely.new
p.a...
Step 1: linear	

‣  Create ZIP	

•  Zip::ZipOutputstream.
open(zipfile_name)do |zos|
files.each do |file, content|
zos.new...
Full Code
	

require 'csv'!
require 'princely'!
require 'zip/zip’!
!
DATA_FILE = ARGV[0]!
DATA_FILE_BASE_NAME = File.basen...
DEMO
Step 2: from linear ...	

parse csv
	


create pdf
	

create pdf
	


...	


create pdf
	


zip
Step 2: ...to parallel	

parse csv
	


create pdf
	


create pdf
	


zip
	


Threads
	

?
	


create pdf
Multi Threaded	

‣  Advantage	

•  Lightweight (minimal overhead)	

‣  Challenges (or why is it hard)	

•  Hard to code: m...
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Ruby on Redis
Upcoming SlideShare
Loading in...5
×

Ruby on Redis

693

Published on

Making an application horizontally scalable in 30 minutes. This presentation describes how a linear processing application (mail merge) can be converted into a horizontally scalable using Redis and provides some context why a multi-process approach is preferable to a multi-threaded approach.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
693
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Ruby on Redis"

  1. 1. Ruby on Redis Pascal Weemaels Koen Handekyn Oct 2013
  2. 2. Target Create a Zip file of PDF’s based on a CSV data file ‣  Linear version ‣  Making it scale with Redis parse csv create pdf create pdf ... create pdf zip
  3. 3. Step 1: linear ‣  Parse CSV •  std lib : require ‘csv’ •  docs = CSV.read("#{DATA}.csv")
  4. 4. Simple Templating with String Interpolation invoice.html <<Q <div class="title"> INVOICE #{invoice_nr} ‣  Merge data into HTML •  template = File.new('invoice.html'). read •  html = eval("<<QQQn#{template} nQQQ”) </div> <div class="address"> #{name}</br> #{street}</br> #{zip} #{city}</br> </div> Q
  5. 5. Step 1: linear ‣  Create PDF •  prince xml using princely gem •  http://www.princexml.com •  p = Princely.new p.add_style_sheets('invoice.css') p.pdf_from_string(html)
  6. 6. Step 1: linear ‣  Create ZIP •  Zip::ZipOutputstream. open(zipfile_name)do |zos| files.each do |file, content| zos.new_entry(file) zos.puts content end end
  7. 7. Full Code require 'csv'! require 'princely'! require 'zip/zip’! ! DATA_FILE = ARGV[0]! DATA_FILE_BASE_NAME = File.basename(DATA_FILE, ".csv”)! ! # create a pdf document from a csv line! def create_pdf(invoice_nr, name, street, zip, city)! template = File.new('../resources/invoice.html').read! html = eval("<<WTFMFn#{template}nWTFMF")! p = Princely.new! p.add_style_sheets('../resources/invoice.css')! p.pdf_from_string(html)! end! ! # zip files from hash ! def create_zip(files_h)! zipfile_name = "../out/#{DATA_FILE_BASE_NAME}.#{Time.now.to_s}.zip"! Zip::ZipOutputStream.open(zipfile_name) do |zos|! files_h.each do |name, content|! zos.put_next_entry "#{name}.pdf"! zos.puts content! end! end! zipfile_name! end! ! # load data from csv! docs = CSV.read(DATA_FILE) # array of arrays! ! # create a pdf for each line in the csv ! # and put it in a hash! files_h = docs.inject({}) do |files_h, doc|! files_h[doc[0]] = create_pdf(*doc)! files_h! end! ! # zip all pfd's from the hash ! create_zip files_h! !
  8. 8. DEMO
  9. 9. Step 2: from linear ... parse csv create pdf create pdf ... create pdf zip
  10. 10. Step 2: ...to parallel parse csv create pdf create pdf zip Threads ? create pdf
  11. 11. Multi Threaded ‣  Advantage •  Lightweight (minimal overhead) ‣  Challenges (or why is it hard) •  Hard to code: most data structures are not thread safe by default, they need synchronized access •  Hard to test: different execution paths , timings •  Hard to maintain ‣  Limitation •  single machine - not a solution for horizontal scalability
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×