Skip to content
Browse files

added models and database connection

  • Loading branch information...
1 parent baf5f10 commit 25e918c5c889c29840af34b9bc1018227dbb991d @rkononov rkononov committed
View
11 README.rdoc
@@ -4,8 +4,15 @@ This example show how to work with iron_worker under Rails environment
== Getting Started
-1. Set proper configuration in iron.json and config_development.yml
+1. Set proper configuration in [iron.json](https://github.com/iron-io/iron_worker_rails_example/blob/master/iron.json) and [config_development.yml](https://github.com/iron-io/iron_worker_rails_example/blob/master/config_development.yml)
2. run 'rails s'
-3. Go to http://localhost:3000/ and test
+3. Go to http://localhost:3000 and test
+
+== FAQ Rail+IronWorker
+1. Q:Where to place workers? <br/>
+ A:Better to place workers in rails_root/workers dir like in example [worker dir](https://github.com/iron-io/iron_worker_rails_example/tree/master/workers)
+2. Q:How to use ActionMailer in worker? <br/>
+ A: *
+
View
3 app/controllers/custom_examples_controller.rb
@@ -5,7 +5,8 @@ def index
def send_email
client.tasks.create('SimpleMailerWorker', {:email => params[:email],
:mailer => ActionMailer::Base.smtp_settings,
- :message => params[:message]})
+ :message => params[:message],
+ :database=> ActiveRecord::Base.connection.instance_variable_get(:@config)})
redirect_to tasks_path
end
end
View
5 app/models/user.rb
@@ -0,0 +1,5 @@
+class User < ActiveRecord::Base
+ def self.get_default_message
+ 'Message from model'
+ end
+end
View
17 workers/simple_mailer_worker.rb
@@ -1,19 +1,30 @@
require "action_mailer"
+require "active_record"
+#require 'mysql2' # you need to require your database adapter
require 'simple_mailer.rb'
+require 'models/user.rb' #require all needed models
-#configuring up mailer
def init_mailer
#Need to convert to proper hashes
mailer_config = params['mailer'].inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo }
+ # set default views dir to current dir
ActionMailer::Base.prepend_view_path('.')
- ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = mailer_config
ActionMailer::Base.delivery_method = :smtp
end
+def setup_database
+ puts "Database connection details:#{params['database'].inspect}"
+ return unless params['database']
+ # estabilsh database connection
+ #ActiveRecord::Base.establish_connection(params['database'])
+end
+
#-------------------Main worker code---------------------------
init_mailer
+setup_database
puts "I got '#{params.inspect}' parameters"
#send email!
-SimpleMailer.welcome_email(params['email'], params['message']).deliver!
+# you could use here any active_record queries with models
+SimpleMailer.welcome_email(params['email'], params['message'] + User.get_default_message).deliver!
View
6 workers/simple_mailer_worker.worker
@@ -1,8 +1,12 @@
# define the runtime language, this can be ruby, java, node, php, go, etc.
runtime "ruby"
name "SimpleMailerWorker"
+#merge your database adapter
+#gem 'mysql2'
+#merge erb file to a specific folder
file "../app/views/simple_mailer/welcome_email.erb" , "simple_mailer"
+ # merge mailer
file "../app/mailers/simple_mailer.rb"
-
+dir "../app/models" # merge all models
# exec is the file that will be executed:
exec "simple_mailer_worker.rb"

0 comments on commit 25e918c

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