Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a3293cb
Showing
13 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
*.gem | ||
*.rbc | ||
.bundle | ||
.config | ||
.yardoc | ||
Gemfile.lock | ||
InstalledFiles | ||
_yardoc | ||
coverage | ||
doc/ | ||
lib/bundler/man | ||
pkg | ||
rdoc | ||
spec/reports | ||
test/tmp | ||
test/version_tmp | ||
tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in mmailer.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2013 Daniel Szmulewicz | ||
|
||
MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Mmailer | ||
|
||
TODO: Write a gem description | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
gem 'mmailer' | ||
|
||
And then execute: | ||
|
||
$ bundle | ||
|
||
Or install it yourself as: | ||
|
||
$ gem install mmailer | ||
|
||
## Usage | ||
|
||
TODO: Write usage instructions here | ||
|
||
## Contributing | ||
|
||
1. Fork it | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Add some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create new Pull Request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require "bundler/gem_tasks" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require "mmailer/version" | ||
|
||
module Mmailer | ||
require 'mail_helper' | ||
require 'master_helper' | ||
require 'worker' | ||
require 'master' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'drb/drb' | ||
|
||
uri = ARGV.shift | ||
obj = DRbObject.new_with_uri(uri) | ||
obj.up? | ||
rc = obj.up | ||
puts rc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'mail' | ||
require 'erb' | ||
|
||
class MailHelper | ||
attr_reader :template, :title | ||
|
||
def initialize(args) | ||
set_provider args.fetch(:provider, :mailchimp) | ||
@template=args[:template] | ||
@title=args[:title] | ||
end | ||
|
||
def set_provider(provider) | ||
|
||
mandrill_config = Proc.new do | ||
delivery_method :smtp, { | ||
:port => 587, | ||
:address => ENV['MANDRILL_SMTP'], | ||
:user_name => ENV['MANDRILL_USERNAME'], | ||
:password => ENV['MANDRILL_API_KEY'] | ||
} | ||
end | ||
|
||
gmail_config = Proc.new do | ||
delivery_method :smtp, { | ||
:port => 587, | ||
:address => ENV['GMAIL_SMTP'], | ||
:user_name => ENV['GMAIL_USERNAME'], | ||
:password => ENV['GMAIL_PASSWORD'], | ||
:authentication => :plain, | ||
:enable_starttls_auto => true | ||
} | ||
end | ||
|
||
providers = {google: gmail_config, mailchimp: mandrill_config} | ||
|
||
Mail.defaults(&providers[provider]) | ||
|
||
end | ||
|
||
def send_email(user) | ||
|
||
mail = Mail.new do | ||
to user.email | ||
from 'Etsy Fu <info@shopi-fu.com>' | ||
end | ||
mail.subject = title | ||
|
||
text_part = Mail::Part.new | ||
text_part.body=ERB.new(File.read("#{Bundler.root}/lib/mailer/templates/#{template}.txt.erb")).result(binding) | ||
|
||
html_part = Mail::Part.new | ||
html_part.content_type='text/html; charset=UTF-8' | ||
html_part.body=ERB.new(File.read("#{Bundler.root}/lib/mailer/templates/#{template}.html.erb")).result(binding) | ||
|
||
mail.text_part = text_part | ||
mail.html_part = html_part | ||
#when Non US-ASCII detected and no charset defined. Defaulting to UTF-8, set your own if this is incorrect. | ||
mail.charset = 'UTF-8' | ||
#puts mail.to_s | ||
mail.deliver! | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
uri = ARGV.shift | ||
DRb.start_service(uri, MasterHelper.new) | ||
puts DRb.uri | ||
begin | ||
DRb.thread.join | ||
rescue Interrupt | ||
abort "Shutting down..." | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
class MasterHelper | ||
attr_accessor :stream, :worker, :machine | ||
|
||
def initialize | ||
@stream = $stdout | ||
self.machine = MicroMachine.new(:stopped) | ||
machine.when(:start, :stopped => :started) | ||
machine.when(:resume, :paused => :started) | ||
machine.when(:stop, :started => :stopped, :paused => :stopped) | ||
machine.when(:pause, :started => :paused) | ||
end | ||
|
||
def display_state | ||
stream.puts state | ||
end | ||
|
||
def state | ||
machine.state | ||
end | ||
|
||
def puts(str) | ||
stream.puts(str) | ||
end | ||
|
||
def up? | ||
true | ||
end | ||
|
||
def resume | ||
if machine.trigger(:resume) | ||
display_state | ||
machine.state | ||
end | ||
end | ||
|
||
def stop | ||
if machine.trigger(:stop) | ||
display_state | ||
machine.state | ||
end | ||
end | ||
|
||
def pause | ||
if machine.trigger(:pause) | ||
display_state | ||
machine.state | ||
end | ||
end | ||
|
||
def start(from=0) | ||
if machine.trigger(:start) | ||
puts "starting from #{from}" | ||
@worker = Thread.new(from) do | from | | ||
worker = Worker.new(from) | ||
end | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module Mmailer | ||
VERSION = "0.0.1" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class Worker | ||
attr_reader :obj, :mailHelper, :collection, :from | ||
|
||
def initialize(from) | ||
@from = from | ||
@obj = DRbObject.new_with_uri('druby://localhost:8080') | ||
@mailHelper = MailHelper.new | ||
load_collection | ||
exec | ||
end | ||
|
||
def exec | ||
while not collection.empty? do | ||
case obj.state | ||
when :paused | ||
sleep 1 | ||
when :started | ||
index ||= from; index += 1 | ||
user = collection.shift | ||
obj.puts "#{index}: #{user.email}" | ||
mailHelper.send_email(user) if not user.email.nil? | ||
sleep rand(6) | ||
if index % 48 == 0 | ||
obj.puts "48th element, going to sleep for an hour" | ||
sleep 3600 | ||
end | ||
when :stopped | ||
break | ||
end | ||
end | ||
obj.puts "Exiting worker" | ||
Thread.exit | ||
end | ||
|
||
def load_collection | ||
@collection = User.active | ||
collection.shift(from) | ||
obj.puts "Loaded #{collection.count} entries" | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# coding: utf-8 | ||
lib = File.expand_path('../lib', __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require 'mmailer/version' | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "mmailer" | ||
spec.version = Mmailer::VERSION | ||
spec.authors = ["Daniel Szmulewicz"] | ||
spec.email = ["danielsz@freeshell.org"] | ||
spec.description = %q{TODO: Write a gem description} | ||
spec.summary = %q{Mass mailing the Ruby way} | ||
spec.homepage = "" | ||
spec.license = "MIT" | ||
|
||
spec.files = `git ls-files`.split($/) | ||
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } | ||
spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) | ||
spec.require_paths = ["lib"] | ||
|
||
spec.add_development_dependency "bundler", "~> 1.3" | ||
spec.add_development_dependency "rake" | ||
spec.add_development_dependency "rspec" | ||
end |