iron_worker_ng 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -27,10 +27,11 @@ gem install typhoeus
27
27
 
28
28
  # Creating A Worker
29
29
 
30
- Each IronWorkerNG Ruby worker is just Ruby code. It can be as simple or as complex as you want. For example, the following is an acceptable worker:
30
+ Each IronWorkerNG Ruby worker is just Ruby code. It can be as simple or as complex as you want. For example,
31
+ the following is an acceptable worker:
31
32
 
32
33
  ```ruby
33
- puts "I'm worker"
34
+ puts "Hello Worker!"
34
35
  puts "My task_id is #{@iron_worker_task_id}"
35
36
  puts "I got '#{params}' parameters"
36
37
  ```
@@ -39,15 +40,48 @@ All output to `STDOUT` will be logged and available for your review when your wo
39
40
 
40
41
  # Creating The Code Package
41
42
 
42
- Because your worker will be executed in the cloud, you'll need to bundle all the necessary gems, supplementary data, and other dependencies with it. `IronWorkerNG::Code::Ruby` makes this easy.
43
+ Before you can run use IronWorker, be sure you've [created a free account with Iron.io](http://www.iron.io)
44
+ and [setup your Iron.io credentials on your system](http://dev.iron.io/articles/configuration/) (either in a json
45
+ file or using ENV variables). You only need to do that once for your machine. If you've done that, then you can continue.
46
+
47
+ Since our worker will be executed in the cloud, you'll need to bundle all the necessary gems,
48
+ supplementary data, and other dependencies with it. `.worker` files make it easy to define your worker.
43
49
 
44
50
  ```ruby
45
- code = IronWorkerNG::Code::Ruby.new
51
+ # define the runtime language, this can be ruby, java, node, php, go, etc.
52
+ runtime "ruby"
53
+ # exec is the file that will be executed:
54
+ exec "hello_worker.rb"
55
+ ```
46
56
 
47
- code.merge_exec 'my_worker.rb'
48
- code.merge_file '../lib/utils.rb'
49
- code.merge_dir '../config'
50
- code.merge_gem 'activerecord'
57
+ You can read more about `.worker` files here: http://dev.iron.io/worker/reference/worker-files/
58
+
59
+ ## Uploading the Code Package
60
+
61
+ If your .worker file is called `hello.worker`, then run:
62
+
63
+ iron_worker upload hello
64
+
65
+ This will upload your worker with the name "hello" so you can reference it like that when queuing up tasks for it.
66
+
67
+ ## Queue Up a Task for your Worker
68
+
69
+ You can quicky queue up a task for your worker from the command line using:
70
+
71
+ iron_worker queue hello
72
+
73
+ Use the `-p` parameter to pass in a payload:
74
+
75
+ iron_worker queue hello -p "{\"hi\": \"world\"}"
76
+
77
+ Most commonly you'll be queuing up tasks from code though, so you can do this:
78
+
79
+ ```ruby
80
+ require "iron_worker_ng"
81
+ client = IronWorkerNG::Client.new
82
+ 100.times do
83
+ client.tasks.create("hello", "foo"=>"bar")
84
+ end
51
85
  ```
52
86
 
53
87
  ## IronWorkerNG::Code::Ruby API
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.1
1
+ 0.6.0
@@ -9,6 +9,7 @@ module IronWorkerNG
9
9
  module Code
10
10
  class Base
11
11
  attr_reader :features
12
+ attr_accessor :base_dir
12
13
 
13
14
  @@registered_types = []
14
15
 
@@ -41,6 +42,7 @@ module IronWorkerNG
41
42
 
42
43
  def initialize(*args, &block)
43
44
  @features = []
45
+ @base_dir = './'
44
46
 
45
47
  initialize_code(*args, &block)
46
48
  end
@@ -107,11 +109,13 @@ module IronWorkerNG
107
109
  # iron_worker_ng-#{IronWorkerNG.full_version}
108
110
 
109
111
  root() {
110
- while [ $# -gt 0 ]; do
112
+ while [ $# -gt 1 ]; do
111
113
  if [ "$1" = "-d" ]; then
112
- printf "%s\n" "$2"
114
+ printf "%s" "$2"
113
115
  break
114
116
  fi
117
+
118
+ shift
115
119
  done
116
120
  }
117
121
 
@@ -15,22 +15,26 @@ module IronWorkerNG
15
15
  merge_exec(exec) unless exec.nil?
16
16
  end
17
17
 
18
- if args.length == 1 and (opts = args[0]).is_a? Hash and wfile = opts[:workerfile] || opts['workerfile']
19
- IronCore::Logger.info 'IronWorkerNG', "Processing workerfile #{wfile}"
20
- eval(File.read(File.expand_path wfile))
21
- end
18
+ wfiles = []
22
19
 
23
- wfiles = ['Workerfile']
20
+ if args.length == 1 && args[0].class == Hash && (args[0][:workerfile] || args[0]['workerfile'])
21
+ wfiles << args[0][:workerfile] || args[0]['workerfile']
22
+ end
24
23
 
25
24
  unless name.nil?
26
25
  wfiles << name + '.worker'
27
26
  wfiles << name + '.workerfile'
28
27
  end
29
28
 
29
+ wfiles << 'Workerfile'
30
+
30
31
  wfiles.each do |wfile|
31
32
  if File.exists?(wfile)
32
- IronCore::Logger.info 'IronWorkerNG', "Processing workerfile #{wfile}"
33
33
  eval(File.read(wfile))
34
+
35
+ @base_dir = File.dirname(wfile) + '/'
36
+
37
+ break
34
38
  end
35
39
  end
36
40
 
@@ -12,23 +12,13 @@ module IronWorkerNG
12
12
  def bundle(zip)
13
13
  super(zip)
14
14
 
15
- gempath_code_array = []
16
-
17
- @features.each do |f|
18
- if f.respond_to?(:code_for_gempath)
19
- gempath_code_array << f.send(:code_for_gempath)
20
- end
21
- end
22
-
23
- gempath_code = gempath_code_array.join("\n")
24
-
25
15
  zip.get_output_stream('__runner__.rb') do |runner|
26
16
  runner.write <<RUBY_RUNNER
27
17
  # iron_worker_ng-#{IronWorkerNG.full_version}
28
18
 
29
19
  module IronWorkerNG
30
- #{File.read(File.dirname(__FILE__) + '/../../3rdparty/hashie/merge_initializer.rb')}
31
- #{File.read(File.dirname(__FILE__) + '/../../3rdparty/hashie/indifferent_access.rb')}
20
+ #{File.read(File.dirname(__FILE__) + '/../../3rdparty/hashie/merge_initializer.rb')}
21
+ #{File.read(File.dirname(__FILE__) + '/../../3rdparty/hashie/indifferent_access.rb')}
32
22
  end
33
23
 
34
24
  class IronWorkerNGHash < Hash
@@ -46,9 +36,8 @@ task_id = nil
46
36
  task_id = $*[i + 1] if $*[i] == '-id'
47
37
  end
48
38
 
49
- Dir.chdir(root)
39
+ ENV['GEM_PATH'] = ([root + '__gems__'] + (ENV['GEM_PATH'] || '').split(':')).join(':')
50
40
 
51
- #{gempath_code}
52
41
  $:.unshift("\#{root}")
53
42
 
54
43
  require 'json'
@@ -1,6 +1,23 @@
1
1
  module IronWorkerNG
2
2
  module Feature
3
3
  class Base
4
+ def initialize(code)
5
+ @code = code
6
+ end
7
+
8
+ def zip_add(zip, dest, src, rebase = true)
9
+ if rebase && (not src.start_with?('/'))
10
+ src = @code.base_dir + src
11
+ end
12
+
13
+ unless File.exists?(src)
14
+ IronCore::Logger.error 'IronWorkerNG', "Can't find src with path='#{src}'"
15
+ raise IronCore::IronError.new("Can't find src with path='#{src}'")
16
+ end
17
+
18
+ zip.add(dest, src)
19
+ end
20
+
4
21
  def hash_string
5
22
  ''
6
23
  end
@@ -5,13 +5,10 @@ module IronWorkerNG
5
5
  class Feature < IronWorkerNG::Feature::Base
6
6
  attr_reader :path
7
7
 
8
- def initialize(path)
9
- unless File.exist?(path)
10
- IronCore::Logger.error 'IronWorkerNG', "Can't find binary exec with path='#{path}'"
11
- raise IronCore::IronError.new("Can't find binary exec with path='#{path}'")
12
- end
8
+ def initialize(code, path)
9
+ super(code)
13
10
 
14
- @path = File.expand_path(path)
11
+ @path = path
15
12
  end
16
13
 
17
14
  def hash_string
@@ -21,7 +18,7 @@ module IronWorkerNG
21
18
  def bundle(zip)
22
19
  IronCore::Logger.debug 'IronWorkerNG', "Bundling binary exec with path='#{@path}'"
23
20
 
24
- zip.add(File.basename(@path), @path)
21
+ zip_add(zip, File.basename(@path), @path)
25
22
  end
26
23
  end
27
24
 
@@ -34,7 +31,7 @@ module IronWorkerNG
34
31
  return
35
32
  end
36
33
 
37
- @exec = IronWorkerNG::Feature::Binary::MergeExec::Feature.new(path)
34
+ @exec = IronWorkerNG::Feature::Binary::MergeExec::Feature.new(self, path)
38
35
 
39
36
  IronCore::Logger.info 'IronWorkerNG', "Merging binary exec with path='#{path}'"
40
37
 
@@ -8,13 +8,10 @@ module IronWorkerNG
8
8
  attr_reader :path
9
9
  attr_reader :dest
10
10
 
11
- def initialize(path, dest)
12
- unless Dir.exist?(path)
13
- IronCore::Logger.error 'IronWorkerNG', "Can't find directory with path='#{path}'"
14
- raise IronCore::IronError.new("Can't find directory with path='#{path}'")
15
- end
11
+ def initialize(code, path, dest)
12
+ super(code)
16
13
 
17
- @path = File.expand_path(path)
14
+ @path = path
18
15
  @dest = dest
19
16
  @dest = Pathname.new(dest).cleanpath.to_s + '/' unless @dest.empty?
20
17
  end
@@ -33,7 +30,7 @@ module IronWorkerNG
33
30
  IronCore::Logger.debug 'IronWorkerNG', "Bundling dir with path='#{@path}' and dest='#{@dest}'"
34
31
 
35
32
  Dir.glob(@path + '/**/**') do |path|
36
- zip.add(@dest + File.basename(@path) + path[@path.length .. -1], path)
33
+ zip_add(zip, @dest + File.basename(@path) + path[@path.length .. -1], path)
37
34
  end
38
35
  end
39
36
  end
@@ -42,7 +39,7 @@ module IronWorkerNG
42
39
  def merge_dir(path, dest = '')
43
40
  IronCore::Logger.info 'IronWorkerNG', "Merging dir with path='#{path}' and dest='#{dest}'"
44
41
 
45
- @features << IronWorkerNG::Feature::Common::MergeDir::Feature.new(path, dest)
42
+ @features << IronWorkerNG::Feature::Common::MergeDir::Feature.new(self, path, dest)
46
43
  end
47
44
 
48
45
  alias :dir :merge_dir
@@ -8,13 +8,10 @@ module IronWorkerNG
8
8
  attr_reader :path
9
9
  attr_reader :dest
10
10
 
11
- def initialize(path, dest)
12
- unless File.exist?(path)
13
- IronCore::Logger.error 'IronWorkerNG', "Can't find file with path='#{path}'"
14
- raise IronCore::IronError.new("Can't find file with path='#{path}'")
15
- end
11
+ def initialize(code, path, dest)
12
+ super(code)
16
13
 
17
- @path = File.expand_path(path)
14
+ @path = path
18
15
  @dest = dest
19
16
  @dest = Pathname.new(dest).cleanpath.to_s + '/' unless @dest.empty?
20
17
  end
@@ -26,7 +23,7 @@ module IronWorkerNG
26
23
  def bundle(zip)
27
24
  IronCore::Logger.debug 'IronWorkerNG', "Bundling file with path='#{@path}' and dest='#{@dest}'"
28
25
 
29
- zip.add(@dest + File.basename(@path), @path)
26
+ zip_add(zip, @dest + File.basename(@path), @path)
30
27
  end
31
28
  end
32
29
 
@@ -34,7 +31,7 @@ module IronWorkerNG
34
31
  def merge_file(path, dest = '')
35
32
  IronCore::Logger.info 'IronWorkerNG', "Merging file with path='#{path}' and dest='#{dest}'"
36
33
 
37
- @features << IronWorkerNG::Feature::Common::MergeFile::Feature.new(path, dest)
34
+ @features << IronWorkerNG::Feature::Common::MergeFile::Feature.new(self, path, dest)
38
35
  end
39
36
 
40
37
  alias :file :merge_file
@@ -6,13 +6,10 @@ module IronWorkerNG
6
6
  attr_reader :path
7
7
  attr_reader :klass
8
8
 
9
- def initialize(path, klass)
10
- unless File.exist?(path)
11
- IronCore::Logger.error 'IronWorkerNG', "Can't find java exec with path='#{path}'"
12
- raise IronCore::IronError.new("Can't find java exec with path='#{path}'")
13
- end
9
+ def initialize(code, path, klass)
10
+ super(code)
14
11
 
15
- @path = File.expand_path(path)
12
+ @path = path
16
13
  @klass = klass
17
14
  end
18
15
 
@@ -23,7 +20,7 @@ module IronWorkerNG
23
20
  def bundle(zip)
24
21
  IronCore::Logger.debug 'IronWorkerNG', "Bundling java exec with path='#{@path}' and class='#{@klass}'"
25
22
 
26
- zip.add(File.basename(@path), @path)
23
+ zip_add(zip, File.basename(@path), @path)
27
24
  end
28
25
 
29
26
  def code_for_classpath
@@ -40,7 +37,7 @@ module IronWorkerNG
40
37
  return
41
38
  end
42
39
 
43
- @exec = IronWorkerNG::Feature::Java::MergeExec::Feature.new(path, klass)
40
+ @exec = IronWorkerNG::Feature::Java::MergeExec::Feature.new(self, path, klass)
44
41
 
45
42
  IronCore::Logger.info 'IronWorkerNG', "Merging java exec with path='#{path}' and class='#{klass}'"
46
43
 
@@ -5,13 +5,10 @@ module IronWorkerNG
5
5
  class Feature < IronWorkerNG::Feature::Base
6
6
  attr_reader :path
7
7
 
8
- def initialize(path)
9
- unless File.exist?(path)
10
- IronCore::Logger.error 'IronWorkerNG', "Can't find java jar with path='#{path}'"
11
- raise IronCore::IronError.new("Can't find java jar with path='#{path}'")
12
- end
8
+ def initialize(code, path)
9
+ super(code)
13
10
 
14
- @path = File.expand_path(path)
11
+ @path = path
15
12
  end
16
13
 
17
14
  def hash_string
@@ -21,7 +18,7 @@ module IronWorkerNG
21
18
  def bundle(zip)
22
19
  IronCore::Logger.debug 'IronWorkerNG', "Bundling java jar with path='#{@path}'"
23
20
 
24
- zip.add(File.basename(@path), @path)
21
+ zip_add(zip, File.basename(@path), @path)
25
22
  end
26
23
 
27
24
  def code_for_classpath
@@ -33,7 +30,7 @@ module IronWorkerNG
33
30
  def merge_jar(path)
34
31
  IronCore::Logger.info 'IronWorkerNG', "Merging java jar with path='#{path}'"
35
32
 
36
- @features << IronWorkerNG::Feature::Java::MergeJar::Feature.new(path)
33
+ @features << IronWorkerNG::Feature::Java::MergeJar::Feature.new(self, path)
37
34
  end
38
35
 
39
36
  alias :jar :merge_jar
@@ -5,13 +5,10 @@ module IronWorkerNG
5
5
  class Feature < IronWorkerNG::Feature::Base
6
6
  attr_reader :path
7
7
 
8
- def initialize(path)
9
- unless File.exist?(path)
10
- IronCore::Logger.error 'IronWorkerNG', "Can't find node exec with path='#{path}'"
11
- raise IronCore::IronError.new("Can't find node exec with path='#{path}'")
12
- end
8
+ def initialize(code, path)
9
+ super(code)
13
10
 
14
- @path = File.expand_path(path)
11
+ @path = path
15
12
  end
16
13
 
17
14
  def hash_string
@@ -21,7 +18,7 @@ module IronWorkerNG
21
18
  def bundle(zip)
22
19
  IronCore::Logger.debug 'IronWorkerNG', "Bundling node exec with path='#{@path}'"
23
20
 
24
- zip.add(File.basename(@path), @path)
21
+ zip_add(zip, File.basename(@path), @path)
25
22
  end
26
23
  end
27
24
 
@@ -34,7 +31,7 @@ module IronWorkerNG
34
31
  return
35
32
  end
36
33
 
37
- @exec = IronWorkerNG::Feature::Node::MergeExec::Feature.new(path)
34
+ @exec = IronWorkerNG::Feature::Node::MergeExec::Feature.new(self, path)
38
35
 
39
36
  IronCore::Logger.info 'IronWorkerNG', "Merging node exec with path='#{path}'"
40
37
 
@@ -6,13 +6,10 @@ module IronWorkerNG
6
6
  attr_reader :path
7
7
  attr_reader :klass
8
8
 
9
- def initialize(path, klass)
10
- unless File.exist?(path)
11
- IronCore::Logger.error 'IronWorkerNG', "Can't find ruby exec with path='#{path}'"
12
- raise IronCore::IronError.new("Can't find ruby exec with path='#{path}'")
13
- end
9
+ def initialize(code, path, klass)
10
+ super(code)
14
11
 
15
- @path = File.expand_path(path)
12
+ @path = path
16
13
  @klass = klass
17
14
  end
18
15
 
@@ -23,7 +20,7 @@ module IronWorkerNG
23
20
  def bundle(zip)
24
21
  IronCore::Logger.debug 'IronWorkerNG', "Bundling ruby exec with path='#{path}' and class='#{klass}'"
25
22
 
26
- zip.add(File.basename(@path), @path)
23
+ zip_add(zip, File.basename(@path), @path)
27
24
  end
28
25
  end
29
26
 
@@ -36,7 +33,7 @@ module IronWorkerNG
36
33
  return
37
34
  end
38
35
 
39
- @exec = IronWorkerNG::Feature::Ruby::MergeExec::Feature.new(path, klass)
36
+ @exec = IronWorkerNG::Feature::Ruby::MergeExec::Feature.new(self, path, klass)
40
37
 
41
38
  IronCore::Logger.info 'IronWorkerNG', "Merging ruby exec with path='#{path}' and class='#{klass}'"
42
39
 
@@ -15,7 +15,9 @@ module IronWorkerNG
15
15
  class Feature < IronWorkerNG::Feature::Base
16
16
  attr_reader :spec
17
17
 
18
- def initialize(spec)
18
+ def initialize(code, spec)
19
+ super(code)
20
+
19
21
  @spec = spec
20
22
  end
21
23
 
@@ -42,22 +44,16 @@ module IronWorkerNG
42
44
  if @spec.extensions.length == 0 || IronWorkerNG::Feature::Ruby::MergeGem.merge_binary?
43
45
  IronCore::Logger.debug 'IronWorkerNG', "Bundling ruby gem with name='#{@spec.name}' and version='#{@spec.version}'"
44
46
 
45
- zip.add('__gems__/' + @spec.full_name, gem_path)
47
+ zip_add(zip, '__gems__/gems/' + @spec.full_name, gem_path, false)
48
+ zip_add(zip, "__gems__/specifications/#{@spec.full_name}.gemspec", File.expand_path(gem_path + '/../../specifications/' + @spec.full_name + '.gemspec'), false)
49
+
46
50
  Dir.glob(gem_path + '/**/**') do |path|
47
- zip.add('__gems__/' + @spec.full_name + path[gem_path.length .. -1], path)
51
+ zip_add(zip, '__gems__/gems/' + @spec.full_name + path[gem_path.length .. -1], path, false)
48
52
  end
49
53
  else
50
54
  IronCore::Logger.warn 'IronWorkerNG', "Skipping ruby gem with name='#{@spec.name}' and version='#{@spec.version}' as it contains native extensions"
51
55
  end
52
56
  end
53
-
54
- def code_for_gempath
55
- if @spec.extensions.length == 0 || IronWorkerNG::Feature::Ruby::MergeGem.merge_binary?
56
- '$:.unshift("#{root}/__gems__/' + @spec.full_name + '/lib")'
57
- else
58
- '# native gem ' + @spec.full_name
59
- end
60
- end
61
57
  end
62
58
 
63
59
  module InstanceMethods
@@ -101,7 +97,7 @@ module IronWorkerNG
101
97
 
102
98
  IronCore::Logger.info 'IronWorkerNG', "Merging ruby gem with name='#{spec.name}' and version='#{spec.version}'"
103
99
 
104
- @features << IronWorkerNG::Feature::Ruby::MergeGem::Feature.new(spec)
100
+ @features << IronWorkerNG::Feature::Ruby::MergeGem::Feature.new(self, spec)
105
101
  end
106
102
  end
107
103
  end
@@ -8,8 +8,10 @@ module IronWorkerNG
8
8
  attr_reader :path
9
9
  attr_reader :groups
10
10
 
11
- def initialize(path, groups)
12
- @path = File.expand_path(path)
11
+ def initialize(code, path, groups)
12
+ super(code)
13
+
14
+ @path = path
13
15
  @groups = groups
14
16
  end
15
17
 
@@ -31,7 +33,7 @@ module IronWorkerNG
31
33
  merge_gem(spec.name, spec.version.to_s)
32
34
  end
33
35
 
34
- @features << IronWorkerNG::Feature::Ruby::MergeGemfile::Feature.new(path, groups)
36
+ @features << IronWorkerNG::Feature::Ruby::MergeGemfile::Feature.new(self, path, groups)
35
37
  end
36
38
 
37
39
  alias :gemfile :merge_gemfile
data/ng_tests_worker.rb CHANGED
@@ -1,3 +1,2 @@
1
- Dir.chdir Dir.glob('iwng/*').first
2
- ENV['NG_GEM_PATH'] = $:.join ':'
3
- exec 'rake -f test/Rakefile test TESTP=basic'
1
+ Dir.chdir 'iwng'
2
+ exec 'rake -f test/Rakefile test'
data/remote_test.rb CHANGED
@@ -20,7 +20,13 @@ code = IronWorkerNG::Code::Ruby.new do
20
20
  exec 'ng_tests_worker.rb'
21
21
  gemfile 'Gemfile'
22
22
  gemfile 'test/Gemfile'
23
- dir '.', 'iwng'
23
+
24
+ Dir.glob('*').each do |p|
25
+ dir p, 'iwng' if Dir.exist? p
26
+ file p, 'iwng' if File.exist? p
27
+ end
28
+
29
+ iron_io_config 'iwng'
24
30
  end
25
31
 
26
32
  puts client.codes.create(code)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iron_worker_ng
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-05-31 00:00:00.000000000 Z
13
+ date: 2012-06-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: iron_core
@@ -144,7 +144,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
144
144
  version: '0'
145
145
  segments:
146
146
  - 0
147
- hash: 257115443
147
+ hash: 667437213
148
148
  required_rubygems_version: !ruby/object:Gem::Requirement
149
149
  none: false
150
150
  requirements: