Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added deserialize worker/example

  • Loading branch information...
commit 47509228ab0aa511db311d1370af449d0db84a84 1 parent 46a535f
@rkononov rkononov authored
View
2  Gemfile
@@ -8,7 +8,7 @@ gem 'rails', '3.2.8'
gem 'sqlite3'
gem 'iron_worker_ng'
-
+gem 'iron_cache'
# Gems used only for assets and not required
# in production environments by default.
View
3  Gemfile.lock
@@ -43,6 +43,8 @@ GEM
multi_json (~> 1.0)
hike (1.2.1)
i18n (0.6.1)
+ iron_cache (1.2.1)
+ iron_core (>= 0.4.2)
iron_core (0.4.2)
rest (>= 2.0.2)
iron_worker_ng (0.10.2)
@@ -131,6 +133,7 @@ PLATFORMS
DEPENDENCIES
coffee-rails (~> 3.2.1)
+ iron_cache
iron_worker_ng
jquery-rails
rails (= 3.2.8)
View
16 app/controllers/custom_examples_controller.rb
@@ -1,5 +1,6 @@
class CustomExamplesController < ApplicationController
def index
+ @deserialize_tasks = client.tasks.list.select { |t| t.code_name == 'DeserializeWorker' }
end
def send_email
@@ -22,4 +23,19 @@ def attribute_worker
client.tasks.create('AttributeWorker', worker_attributes)
redirect_to tasks_path
end
+
+ def deserialize_worker
+ m = ComplexModel.new
+ m.a = params['a'].split(',').map(&:to_i) rescue 1
+ m.b = params['b'].split(',').map(&:to_i) rescue [1, 2, 3]
+ client.tasks.create('DeserializeWorker', :complex_model => m.to_json, :iron_config => Rails.application.config.iron_config.to_json)
+ redirect_to custom_examples_path
+ end
+
+ def deserialize_worker_results
+ client = IronCache::Client.new(Rails.application.config.iron_config)
+ cache = client.cache("IronWorker101Example")
+ item = cache.get(params['id'])
+ @complex_model = JSON.load item.value
+ end
end
View
44 app/models/complex_model.rb
@@ -0,0 +1,44 @@
+require 'active_support/core_ext'
+class ComplexModel
+ attr_accessor :a, :b, :sum, :av, :hash_value
+
+ def calculate
+ count = 0
+ if a.is_a? Array
+ sum_a = a.sum
+ count+=a.count
+ else
+ count+=1
+ end
+
+ if b.is_a? Array
+ sum_b = b.sum
+ count+=b.count
+ else
+ count+=1
+ end
+ self.sum = (sum_a||a) + (sum_b||b)
+ self.av = ((sum_a||a) + (sum_b||b)).to_f/count
+ self.hash_value={'a' => self.a, 'b' => self.b, 'sum' => self.sum, 'av' => self.av.round(2)}
+ end
+
+ def to_json(*a)
+ hash_data = {}
+ self.instance_variables.each do |var|
+ hash_data[var] = self.instance_variable_get var
+ end
+
+ hash = {}
+ hash['json_class'] = self.class.name
+ hash['data'] = hash_data
+ hash.to_json(*a)
+ end
+
+ def self.json_create(a)
+ model = new
+ a["data"].each do |var, val|
+ model.instance_variable_set var, val
+ end
+ model
+ end
+end
View
3  app/views/custom_examples/deserialize_worker_results.html.erb
@@ -0,0 +1,3 @@
+<h3>Deserialized results from IronCache</h3>
+<%= @complex_model.inspect %>
+<%= link_to :back %>
View
35 app/views/custom_examples/index.html.erb
@@ -43,3 +43,38 @@ This worker show how to pass and parse different data types to worker
<%= submit_tag("Send to worker!", :class => 'controls btn btn-primary') %>
</div>
<% end %>
+<h3>Deserialize Worker</h3>
+This is simple worker that create new custom object, send it to worker and recieve it back with processed data
+<div>
+ <%= form_tag(deserialize_worker_custom_examples_path, :class => 'form-inline') do %>
+
+ <%= text_field_tag(:a, '1,2,3,4') %>
+
+ <%= text_field_tag(:b, '5,6,7,8') %>
+ <%= submit_tag("Process values!", :class => 'btn btn-primary') %>
+ <% end %>
+ <% if !@deserialize_tasks.empty? %>
+ <table class="table table-bordered table-hover table-condensed">
+ <thead>
+ <tr>
+ <th>Code Name</th>
+ <th>Created at</th>
+ <th>Status</th>
+ <th>Duration</th>
+ <th>Get results!</th>
+ </tr>
+ </thead>
+
+ <% @deserialize_tasks.each do |t| %>
+ <tr>
+ <td><%= t.code_name %></td>
+ <td><%= t.created_at %></td>
+ <td><%= t.status %></td>
+ <td><%= t.duration %></td>
+ <td><%= link_to("Results", deserialize_worker_results_custom_examples_path(:id=>t.id)) if t.status=='complete' %></td>
+ <% end %>
+ </table>
+ <% else %>
+ <h5>There are now any tasks, feel free to queue new one</h5>
+ <% end %>
+</div>
View
1  config/application.rb
@@ -60,6 +60,7 @@ class Application < Rails::Application
config.assets.version = '1.0'
config2 = YAML.load_file(File.join(Rails.root, "config_#{Rails.env}.yml"))
puts "Initializing IronWorker for #{Rails.env} environment"
+ config.iron_config = config2['iron']
config.iw_client = IronWorkerNG::Client.new(:token => config2['iron']['token'], :project_id => config2['iron']['project_id'])
config.action_mailer.smtp_settings =
{
View
2  config/routes.rb
@@ -25,6 +25,8 @@
collection do
post 'send_email'
post 'attribute_worker'
+ post 'deserialize_worker'
+ get 'deserialize_worker_results'
end
end
View
2  lib/tasks/iron_worker.rake
@@ -3,7 +3,7 @@ namespace :iron_worker do
task :upload => :environment do
Dir.glob('workers/*.worker').each do |file|
code_from_workerfile = IronWorkerNG::Code::Base.new(:workerfile => file)
- IW_CLIENT.codes.create(code_from_workerfile)
+ Rails.application.config.iw_client.codes.create(code_from_workerfile)
end
end
end
View
16 workers/deserialize_worker.rb
@@ -0,0 +1,16 @@
+require 'json'
+require 'iron_cache'
+require 'models/complex_model'
+puts "Got params:#{params.inspect}"
+puts "Deserializing"
+complex_model = JSON.load params["complex_model"]
+puts "Performing calculations"
+complex_model.calculate
+puts "pushing model to iron_cache"
+puts complex_model.to_json
+iron_config = JSON.parse(params['iron_config'])
+puts "Initializing cache :#{iron_config.inspect}"
+client = IronCache::Client.new(iron_config)
+cache = client.cache("IronWorker101Example")
+puts "Putting model to cache"
+cache.put(@iron_task_id, complex_model.to_json)
View
7 workers/deserialize_worker.worker
@@ -0,0 +1,7 @@
+# define the runtime language, this can be ruby, java, node, php, go, etc.
+runtime "ruby"
+gem 'iron_cache'
+name "DeserializeWorker"
+dir "../app/models" # merge all models
+# exec is the file that will be executed:
+exec "deserialize_worker.rb"
Please sign in to comment.
Something went wrong with that request. Please try again.