refried 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 28d7c580a23420a626958fc23c26305ef78f116d
4
- data.tar.gz: 40dddd36c986edd1e76b9dd40b3c5ea4d9798077
3
+ metadata.gz: 5614a3de2476ba8b564bb1cce35e2580d5be4af8
4
+ data.tar.gz: 2a6a85e530fcc4f15176d820cbbb8f74da616b5d
5
5
  SHA512:
6
- metadata.gz: 14b379e31b5a890012f61434bf6e5c09d6d8db3216cdcb2517a06e5ea6e0f7229cc3ebc4703c7cec96d34377200701d5afca6a6fc9eb28156544b9b435ef4b38
7
- data.tar.gz: 2c1d025bdcc3eef07acccd4d2130d1da830d9f517e57c26d1bc7f8b089b28da5efa3192b13732f3edef3dc9207020539247b650b22ec57a161af8d38889266f1
6
+ metadata.gz: fb38fe056924ad13c94d28418c2c5992509273f096dcb9e50a610ffc2d3f15d39e86128fec96eb0e1afe6d800c247d97884d16bf2bb6b79f91657cdb80ced3e9
7
+ data.tar.gz: 013058133907e862d01f91f16d24289839974b4f287df202af1c37c091fb4969a3adf639c0fbefeb978377dd8186052d21959d25d2edda145da005801c1beb10
@@ -0,0 +1,31 @@
1
+ module Refried
2
+ # The Refried Configuration class
3
+ # based in general on the configuration approach in https://github.com/nesquena/backburner MIT license
4
+ class Configuration
5
+ PRIORITY_LABELS = { :high => 0, :medium => 100, :low => 200 }
6
+
7
+ attr_accessor :beanstalk_url # beanstalk url connection
8
+ attr_accessor :tube_namespace # namespace prefix for every queue
9
+ attr_accessor :default_priority # default job priority
10
+ attr_accessor :respond_timeout # default job timeout
11
+ attr_accessor :on_error # error handler
12
+ attr_accessor :max_job_retries # max job retries
13
+ attr_accessor :retry_delay # retry delay in seconds
14
+ attr_accessor :logger # logger
15
+ attr_accessor :priority_labels # priority labels
16
+ attr_accessor :reserve_timeout # duration to wait to reserve on a single server
17
+
18
+ def initialize
19
+ @beanstalk_url = "beanstalk://localhost"
20
+ @tube_namespace = "refried.queue"
21
+ @default_priority = 65536
22
+ @respond_timeout = 120
23
+ @on_error = nil
24
+ @max_job_retries = 0
25
+ @retry_delay = 5
26
+ @logger = nil
27
+ @priority_labels = PRIORITY_LABELS
28
+ @reserve_timeout = nil
29
+ end
30
+ end # Configuration
31
+ end # Refried
@@ -0,0 +1,132 @@
1
+ require 'beaneater'
2
+
3
+ module Refried
4
+ module Puter
5
+ module ActsAsPuter
6
+ def acts_as_puter
7
+ send :include, InstanceMethods
8
+ send :extend, ClassMethods
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ SUPPORTED_MODES = [:simple, :type_map, :alias_map]
14
+
15
+ # Set the mode of function that the Puter should follow
16
+ #
17
+ # @param mode [Symbol] which mapping mode the puter uses, `:simple`, `:type_map` or `:alias_map`
18
+ def puter_mode=(mode)
19
+ unless SUPPORTED_MODES.include? mode
20
+ raise ArgumentError, "Unsupported mode for acts as puter."
21
+ end
22
+ @puter_mode = mode
23
+ end
24
+
25
+ # Get the current mapping mode of the Puter
26
+ #
27
+ # @return [Symbol] the current mapping mode of the Puter
28
+ def puter_mode
29
+ @puter_mode ||= :simple
30
+ end
31
+ end
32
+
33
+ module InstanceMethods
34
+
35
+ def puter_tube_alias_map
36
+ @puter_tube_alias_map ||= Hash.new
37
+ end
38
+ alias_method :alias_map, :puter_tube_alias_map
39
+
40
+ def puter_tube_alias_map=(alias_map)
41
+ @puter_tube_alias_map = alias_map
42
+ end
43
+
44
+ # I want to support a number of types of mappings
45
+ # 1. in the simple case the tube name should be associated with the Puter's class
46
+ # 2. a map of item types to tube names
47
+ # 3. a map of tube aliases to tube names (this corresponds to how ESIndexer & funding override workers will use the gem)
48
+ def put (item, tube_alias = nil)
49
+ self.attempt_to_log "Puter#put message received. #{item} and tube_alias #{tube_alias}"
50
+ puts tube_alias
51
+ tube = self.tube(alias: tube_alias)
52
+ payload = self.generate_message item
53
+ r = tube.put payload
54
+ self.attempt_to_log "Puter#put message queued containing #{item}, result = #{r}"
55
+ r
56
+ end
57
+
58
+ # Get the Beaneater Tube object for the current class, the item, and the alias
59
+
60
+ # If the puter mode is :type_map the :type key should contain the class of the item being put as a symbol, which should be a key in the puter_tube_type_map
61
+ # If the puter mode is :alias_map the value for the :alias key is the tube_alias,
62
+ # the tube_alias is used as the key in the puter_tube_alias_map whose value is the tube's name
63
+
64
+ def tube (selectors = {})
65
+ unless self.locatable? selectors
66
+ raise ArgumentError, 'Selectors to #tube were unexpected for current puter_mode'
67
+ end
68
+ case self.class.puter_mode
69
+ when :simple
70
+ tube_name = self.class.to_s.downcase
71
+ when :alias_map
72
+ tube_alias = selectors[:alias]
73
+ tube_name = self.alias_map[tube_alias]
74
+ else
75
+ raise ArgumentError, 'Invalid puter mode detected in the #tube method.'
76
+ end
77
+ tube_name ||= nil
78
+ tube ||= Refried.tubes.find tube_name
79
+ end
80
+
81
+ # This method converts the payload object into a format for injection
82
+ # into the queue
83
+ #
84
+ # @param payload the object that should be converted into a message (it must respond to the to_json method)
85
+ # @return [String] the appropriately serialized representation of the payload
86
+ def generate_message(payload)
87
+ if payload.is_a? Fixnum
88
+ payload.to_json
89
+ else
90
+ if payload.nil?
91
+ nil
92
+ elsif payload.respond_to?(:empty?) && payload.empty?
93
+ nil
94
+ elsif payload.respond_to? :to_edible
95
+ payload.to_edible
96
+ else
97
+ # Not sure that this is the appropriate implementation, perhaps to_s is better
98
+ payload.to_json
99
+ end
100
+ end
101
+ end
102
+
103
+ protected
104
+ def locatable? (selectors = {})
105
+ case self.class.puter_mode
106
+ when :simple
107
+ selectors.nil? || selectors.is_a?(Hash) && selectors.count == 0
108
+ when :alias_map
109
+ if selectors.is_a? Hash
110
+ l = selectors.has_key?(:alias) && selectors[:alias].is_a?(Symbol) && self.alias_map.has_key?(selectors[:alias])
111
+ end
112
+ l ||= false
113
+ else
114
+ false
115
+ end
116
+ end
117
+
118
+ def attempt_to_log (message)
119
+ begin
120
+ logger.info message
121
+ rescue => e
122
+ puts "Failed to access logger, message that should have been logged = #{message}"
123
+ end
124
+ end
125
+ end
126
+
127
+ def self.included(base)
128
+ base.extend(ActsAsPuter)
129
+ end
130
+ end
131
+ Object.instance_eval { include Puter }
132
+ end
@@ -0,0 +1,14 @@
1
+ module Refried
2
+ class Tubes
3
+ # TODO: connect to configuration
4
+ def initialize
5
+ @pool ||= Beaneater::Pool.new(['localhost:11300 '])
6
+ @tubes = @pool.tubes
7
+ end
8
+
9
+ def find(canonical_tube_name)
10
+ @tubes.find canonical_tube_name
11
+ end
12
+
13
+ end
14
+ end
data/lib/refried.rb CHANGED
@@ -1,5 +1,35 @@
1
- class Refried
2
- def self.beans
3
- puts "BAKING BEANS!"
1
+ require 'beaneater'
2
+ require 'refried/configuration'
3
+ require 'refried/tubes'
4
+ require 'refried/puter'
5
+
6
+ # Refried core API methods
7
+ # based in part on the design of backburner (https://github.com/nesquena/backburner MIT license)
8
+ module Refried
9
+ class << self
10
+
11
+ # Allows the user to set configuration options
12
+ # by yielding the configuration block
13
+ #
14
+ # @param block [Block]
15
+ # @return [Configuration] the current configuration object
16
+ def configure(&block)
17
+ yield(configuration)
18
+ configuration
19
+ end
20
+
21
+ # Returns the singleton class's configuration object
22
+ #
23
+ # @return [Configuration] the current configuration object
24
+ def configuration
25
+ @configuration ||= Configuration.new
26
+ end
27
+
28
+ # Returns the Beaneater::Tubes object, which is the collection of all the Beaneater Tube objects
29
+ #
30
+ # @return [Beaneater::Tubes] the collection of Beaneater Tubes
31
+ def tubes
32
+ @tubes ||= Tubes.new
33
+ end
4
34
  end
5
35
  end
metadata CHANGED
@@ -1,15 +1,49 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: refried
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - caldwecr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-30 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2014-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: beaneater
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.3'
20
+ - - '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 0.3.3
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.3'
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 0.3.3
33
+ - !ruby/object:Gem::Dependency
34
+ name: simplecov
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ~>
38
+ - !ruby/object:Gem::Version
39
+ version: '0.9'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '0.9'
13
47
  description: An enhancement
14
48
  email: caldwecr@gmail.com
15
49
  executables: []
@@ -17,6 +51,9 @@ extensions: []
17
51
  extra_rdoc_files: []
18
52
  files:
19
53
  - lib/refried.rb
54
+ - lib/refried/configuration.rb
55
+ - lib/refried/puter.rb
56
+ - lib/refried/tubes.rb
20
57
  homepage: http://rubygems.org/gems/refried
21
58
  licenses: []
22
59
  metadata: {}