Skip to content
This repository
Browse code

[#45]Refactor Email Example (master/slave, IronMQ, _ng, php)

  • Loading branch information...
commit c3cd2e360b256c67171585e1658e177f904c6ed7 1 parent e832e77
Roman authored June 05, 2012 romand committed June 05, 2012
3  ruby/_config.yml
@@ -135,7 +135,8 @@ email:
135 135
     username: user_name
136 136
     password: password
137 137
     from: somebody@acme.com
138  
-    to: somebodyelse@acme.com
  138
+    to: somebodyelse@gmail.com
  139
+    provider: gmail
139 140
 
140 141
 loggly:
141 142
     key: my_loggly_key
12  ruby_ng/email_worker/README.md
Source Rendered
... ...
@@ -0,0 +1,12 @@
  1
+# email_worker
  2
+
  3
+This is simple example how to send email using IronWorker
  4
+
  5
+## Getting started
  6
+
  7
+- Edit _config.yml (iw and email sections)
  8
+- Upload the worker by running 'iron_worker upload email_worker' in this directory
  9
+- run 'single_email_sender.rb'
  10
+
  11
+That's it, now you should receive email.
  12
+
54  ruby_ng/email_worker/email_worker.rb
... ...
@@ -1,26 +1,32 @@
1 1
 # Configures smtp settings to send email.
2 2
 def init_mail(username, password, domain, provider)
  3
+  puts "initializing provider"
3 4
   case provider
4 5
     when 'gmail'
  6
+      puts "Selected gmail as a provider"
5 7
       port = 587
6 8
       address = "smtp.gmail.com"
7  
-    else #sendgrid
  9
+    else #sendgrid - default
  10
+      puts "Selected sendgrid as a provider"
8 11
       port = 25
9 12
       address = "smtp.sendgrid.net"
10 13
   end
  14
+  puts "Preparing mail configuration"
11 15
   mail_conf = {:address => address,
12 16
                :port => port,
13  
-               :domain => domain,
  17
+               :domain => domain, #custom domain
14 18
                :user_name => username,
15 19
                :password => password,
16 20
                :authentication => 'plain',
17  
-               :enable_starttls_auto => true}
  21
+               :enable_starttls_auto => true} #gmail require this option
18 22
   Mail.defaults do
19 23
     delivery_method :smtp, mail_conf
20 24
   end
  25
+  puts "Mail service configured"
21 26
 end
22 27
 
23 28
 def send_mail(to, from, subject, content)
  29
+  puts "Preparing email from:#{from},to:#{to},subject#{subject}"
24 30
   msg = Mail.new do
25 31
     to to
26 32
     from from
@@ -30,14 +36,52 @@ def send_mail(to, from, subject, content)
30 36
       body content
31 37
     end
32 38
   end
33  
-  msg.deliver
  39
+  puts "Mail ready, delivering"
  40
+  details = msg.deliver
  41
+  puts "Mail delivered!"
  42
+  details
34 43
 end
35 44
 
  45
+def update_message_status(email, message_details)
  46
+  puts "Updating user status via API endpoint"
  47
+  puts "Email:#{email},details:#{message_details.inspect}"
  48
+
  49
+  #here is the example how you could send email details from this worker to your api
  50
+  require 'rest'
  51
+  puts "initializing client"
  52
+  web_client = Rest::Client.new # res gem already merged
  53
+  puts "sending api request"
  54
+  #just change google.com on your api endpoint
  55
+  web_client.get('http://google.com', {:email => email, :message_details => message_details})
  56
+
  57
+  # or put email details in iron_cache
  58
+  if params['iw_token'] && params['iw_project_id']
  59
+    puts "Initializing cache"
  60
+    require 'iron_cache'
  61
+    cache = IronCache::Client.new(:token => params['iw_token'], :project_id => params['iw_project_id'])
  62
+    puts "Sending message details to cache"
  63
+    cache.items.put(email, {:timestamp => Time.now, :message_details => message_details.inspect}.to_json)
  64
+  end
  65
+
  66
+end
36 67
 
37 68
 # Sample worker that sends an email.
  69
+puts "Worker started"
38 70
 
39 71
 require 'mail'
40 72
 
41 73
 init_mail(params['username'], params['password'], params['domain'], params['provider'])
42 74
 
43  
-send_mail(params['to'], params['from'], params['subject'], params['content'])
  75
+
  76
+if params['to'].is_a? Array # check what is passed one email or array of emails
  77
+  puts "Array of #{params['to'].count} emails passed"
  78
+  params['to'].each do |email|
  79
+    message_details = send_mail(email, params['from'], params['subject'], params['content'])
  80
+    update_message_status(email, message_details)
  81
+  end
  82
+else
  83
+  message_details = send_mail(params['to'], params['from'], params['subject'], params['content'])
  84
+  update_message_status(email, message_details)
  85
+end
  86
+
  87
+puts "Worker finished"
4  ruby_ng/email_worker/email_worker.worker
... ...
@@ -1,3 +1,5 @@
1 1
 runtime "ruby"
2 2
 exec "email_worker.rb"
3  
-gem "mail"
  3
+gem "mail"
  4
+gem "rest"
  5
+gem "iron_cache"
44  ruby_ng/email_worker/master_email_worker.rb
... ...
@@ -1,44 +0,0 @@
1  
-require 'iron_mq'
2  
-require 'iron_worker_ng'
3  
-# Create an IronWorker client
4  
-def queue_worker(config_data, to, subject, content)
5  
-  client = IronWorkerNG::Client.new(:token => config_data['iw']['token'], :project_id => config_data['iw']['project_id'])
6  
-  email = config_data['email']
7  
-  params = {:username => email['username'],
8  
-            :password => email['password'],
9  
-            :domain => email['domain'],
10  
-            :from => email['from'],
11  
-            :to => to,
12  
-            :subject => subject,
13  
-            :content => content,
14  
-            :provider => email['provider']}
15  
-  client.tasks.create("email_worker", params)
16  
-end
17  
-
18  
-def get_emails(config_data)
19  
-  ironmq = IronMQ::Client.new(:token => config_data['iw']['token'], :project_id => config_data['iw']['project_id'])
20  
-  emails = []
21  
-  n = 0
22  
-  while n < 100 # 100 emails per single run
23  
-    msg = ironmq.messages.get()
24  
-    break unless msg
25  
-    emails << msg.body
26  
-    msg.delete
27  
-    n+=1
28  
-  end
29  
-  emails
30  
-end
31  
-
32  
-def content
33  
-  <<-EOF
34  
-  <h1>Thank you for using Iron Worker</h1>
35  
-It's a simple email that has some html tags<br>
36  
-  <a href='http://iron.io'>Iron.io</a>
37  
-  EOF
38  
-end
39  
-
40  
-emails = get_emails(params)
41  
-
42  
-emails.each do |email|
43  
-  queue_worker(params, email, "Welcome email", content)
44  
-end
6  ruby_ng/email_worker/single_email_sender.rb
... ...
@@ -1,9 +1,10 @@
1 1
 require 'yaml'
2 2
 require 'iron_worker_ng'
3  
-# Create an IronWorker client
4 3
 
  4
+# loading config from config file
5 5
 config_data = YAML.load_file '../../ruby/_config.yml'
6 6
 
  7
+# Create an IronWorker client
7 8
 client = IronWorkerNG::Client.new(:token => config_data['iw']['token'], :project_id => config_data['iw']['project_id'])
8 9
 
9 10
 email = config_data['email']
@@ -11,9 +12,10 @@
11 12
           :password => email['password'],
12 13
           :domain => 'simpleworker.com',
13 14
           :from => email['from'],
14  
-          :to => email['to'],
  15
+          :to => [email['to'],email['to']],
15 16
           :subject => 'sample',
16 17
           :content => 'HEY ITs a body',
17 18
           :provider => 'gmail'
18 19
 }
  20
+#launching worker
19 21
 client.tasks.create("email_worker", params)
13  ruby_ng/master_email_worker/README.md
Source Rendered
... ...
@@ -0,0 +1,13 @@
  1
+# Master/slave email sender
  2
+
  3
+This is more complex example that show what is the best way to send a tons of emails using IronWorker.
  4
+
  5
+## Getting started
  6
+
  7
+- Edit _config.yml (iw and email sections)
  8
+- Upload the worker by running 'iron_worker upload master_email_worker' in this directory
  9
+- Upload the worker by running 'iron_worker upload email_worker' in email_worker directory
  10
+- run 'master_email_sender.rb'
  11
+
  12
+That's it, now you should receive 10 emails that was send by 2 separate tasks fired by single master worker.
  13
+
12  ruby_ng/email_worker/master_email_sender.rb → ruby_ng/master_email_worker/master_email_sender.rb
... ...
@@ -1,9 +1,19 @@
1 1
 require 'yaml'
2 2
 require 'iron_worker_ng'
3  
-# Create an IronWorker client
4 3
 
  4
+#loading config
5 5
 config_data = YAML.load_file '../../ruby/_config.yml'
6 6
 
  7
+#initializing IronMQ
  8
+ironmq = IronMQ::Client.new(:token => config_data['iw']['token'], :project_id => config_data['iw']['project_id'])
  9
+
  10
+#adding emails to IronMQ queue
  11
+20.times do |i|
  12
+  ironmq.messages.post(config_data['email']['to'])
  13
+end
  14
+
  15
+
  16
+# Create an IronWorker client
7 17
 client = IronWorkerNG::Client.new(:token => config_data['iw']['token'], :project_id => config_data['iw']['project_id'])
8 18
 
9 19
 client.tasks.create("master_email_worker", config_data)
75  ruby_ng/master_email_worker/master_email_worker.rb
... ...
@@ -0,0 +1,75 @@
  1
+require 'iron_mq'
  2
+require 'iron_worker_ng'
  3
+# Create an IronWorker client
  4
+def queue_worker(config_data, to, subject, content)
  5
+  puts "Preparing worker params"
  6
+  client = IronWorkerNG::Client.new(:token => config_data['iw']['token'], :project_id => config_data['iw']['project_id'])
  7
+  email = config_data['email']
  8
+  params = {:username => email['username'],
  9
+            :password => email['password'],
  10
+            :domain => email['domain'],
  11
+            :from => email['from'],
  12
+            :to => to,
  13
+            :subject => subject,
  14
+            :content => content,
  15
+            :provider => email['provider']}
  16
+
  17
+  #adding iw token and project_id for IronCache if you don't use it you could remove following lines
  18
+  params.merge!({:iw_token => config_data['iw']['token'],
  19
+  :iw_project_id => config_data['iw']['project_id']})
  20
+
  21
+  puts "Params ready, launching worker with #{to.count} emails"
  22
+  client.tasks.create("email_worker", params)
  23
+end
  24
+
  25
+
  26
+#simple get emails details from IronMQ queue
  27
+def get_emails(config_data)
  28
+  puts "initialize iron_mq client"
  29
+  ironmq = IronMQ::Client.new(:token => config_data['iw']['token'], :project_id => config_data['iw']['project_id'])
  30
+
  31
+  emails = []
  32
+  n = 0
  33
+
  34
+  #number of max emails that should be send with this worker per run
  35
+  max_emails_to_send = 100
  36
+
  37
+  while n < max_emails_to_send
  38
+    puts "Gettig message from IronMQ"
  39
+    msg = ironmq.messages.get()
  40
+    puts "Got message from queue - #{msg.inspect}"
  41
+    break unless msg
  42
+    puts "Adding #{msg.body} to list of emails"
  43
+    emails << msg.body
  44
+    puts "Deleting message from queue"
  45
+    msg.delete
  46
+    n+=1
  47
+  end
  48
+  emails
  49
+end
  50
+
  51
+def content
  52
+  <<-EOF
  53
+  <h1>Thank you for using Iron Worker</h1>
  54
+It's a simple email that has some html tags<br>
  55
+  <a href='http://iron.io'>Iron.io</a>
  56
+  EOF
  57
+end
  58
+
  59
+puts "Started"
  60
+puts "Getting emails"
  61
+emails = get_emails(params)
  62
+puts "Received #{emails.count} emails"
  63
+#max number of emails per single worker
  64
+number_of_emails_per_worker = 10
  65
+
  66
+#slice array of emails into small arrays with max 10 elems in each
  67
+sliced_emails = emails.each_slice(number_of_emails_per_worker).to_a
  68
+
  69
+puts "Should be launched #{sliced_emails.count} workers"
  70
+
  71
+sliced_emails.each do |array_of_emails|
  72
+  puts "Queueing worker"
  73
+  queue_worker(params, array_of_emails, "Welcome email", content)
  74
+end
  75
+puts "Worker done"
0  ruby_ng/email_worker/master_email_worker.worker → ...ng/master_email_worker/master_email_worker.worker
File renamed without changes
11  ruby_ng/twilio_worker/README.md
Source Rendered
... ...
@@ -0,0 +1,11 @@
  1
+# Twilio SMS worker
  2
+
  3
+This is simple example how to send SMS using twilio
  4
+
  5
+## Getting started
  6
+
  7
+- Edit _config.yml (iw and twilio sections)
  8
+- Upload the worker by running 'iron_worker upload twilio_worker' in this directory
  9
+- run 'single_twilio_sender.rb'
  10
+
  11
+That's it, now you should receive SMS, also you could check task details in hud.iron.io .
19  ruby_ng/twilio_worker/single_twilio_sender.rb
... ...
@@ -0,0 +1,19 @@
  1
+require 'yaml'
  2
+require 'iron_worker_ng'
  3
+
  4
+# loading config from config file
  5
+config_data = YAML.load_file '../../ruby/_config.yml'
  6
+
  7
+# Create an IronWorker client
  8
+client = IronWorkerNG::Client.new(:token => config_data['iw']['token'], :project_id => config_data['iw']['project_id'])
  9
+
  10
+twilio = config_data['twilio']
  11
+
  12
+params = {:sid => twilio['sid'],
  13
+          :token => twilio['token'],
  14
+          :from => twilio['from'],
  15
+          :to => twilio['to'],
  16
+          :message => 'sample'
  17
+}
  18
+#launching worker
  19
+client.tasks.create("twilio_worker", params)
17  ruby_ng/twilio_worker/twilio_worker.rb
... ...
@@ -0,0 +1,17 @@
  1
+require 'twilio-ruby'
  2
+puts "Running TwilioSMSWorker..."
  3
+
  4
+puts "Connecting to Twilio..."
  5
+client = Twilio::REST::Client.new(params['sid'], params['token'])
  6
+puts "Connected."
  7
+
  8
+puts "Sending a text message..."
  9
+client.account.sms.messages.create(
  10
+    :from => params['from'],
  11
+    :to => params['to'],
  12
+    :body => params['message']
  13
+)
  14
+
  15
+puts "Sent."
  16
+
  17
+puts "Finished processing TwilioSMSWorker."
4  ruby_ng/twilio_worker/twilio_worker.worker
... ...
@@ -0,0 +1,4 @@
  1
+runtime "ruby"
  2
+exec "twilio_worker.rb"
  3
+
  4
+gem 'twilio-ruby'

0 notes on commit c3cd2e3

Please sign in to comment.
Something went wrong with that request. Please try again.