Skip to content

Commit

Permalink
Extract filtered action list to class method on NodeRecord.
Browse files Browse the repository at this point in the history
  • Loading branch information
knowtheory committed Aug 25, 2015
1 parent e084765 commit f1ca0cd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 44 deletions.
4 changes: 4 additions & 0 deletions lib/cloud_crowd/models/node_record.rb
Expand Up @@ -40,6 +40,10 @@ def self.check_in(params, request)
create!(attrs.merge(host_attr))
end
end

def self.available_actions
all.map(&:actions).flatten.uniq - BlackListedAction.all.pluck(:action)
end

# Dispatch a WorkUnit to this node. Places the node at back at the end of
# the rotation. If we fail to send the WorkUnit, we consider the node to be
Expand Down
14 changes: 1 addition & 13 deletions lib/cloud_crowd/models/work_unit.rb
Expand Up @@ -42,9 +42,7 @@ def self.distribute_to_nodes
# Find the available nodes, and determine what actions we're capable
# of running at the moment.
available_nodes = NodeRecord.available.to_a
available_actions = available_nodes.map {|node| node.actions }.flatten.uniq
# Filter out any actions that are blacklisted
available_actions = self.filter_blacklist(available_actions)
available_actions = NodeRecord.available_actions

filter = "action in (#{available_actions.map{|a| "'#{a}'"}.join(',')})"

Expand Down Expand Up @@ -108,20 +106,10 @@ def self.find_by_worker_name(name)

# Convenience method for starting a new WorkUnit.
def self.start(job, action, input, status)
blacklist_check = self.filter_blacklist([action]).empty?
raise "Action [#{action}] is blacklisted. Unable to start job." if blacklist_check
input = input.to_json unless input.is_a? String
self.create(:job => job, :action => action, :input => input, :status => status)
end

def self.filter_blacklist(actions)
# Update blacklist
#BlackListedAction.update_black_list
# Check blacklist for the item
actions.reject!{|x| BlackListedAction.all.map(&:action).include? x}
actions || []
end

# Mark this unit as having finished successfully.
# Splitting work units are handled differently (an optimization) -- they
# immediately fire off all of their resulting WorkUnits for processing,
Expand Down
34 changes: 3 additions & 31 deletions test/unit/test_black_listed_action.rb
Expand Up @@ -7,6 +7,7 @@ class BlackListedActionTest < Minitest::Test
setup do
BlackListedAction.destroy_all
@black_listed_action = BlackListedAction.create(action: 'word_count')
@node = NodeRecord.make!
end

teardown do
Expand All @@ -23,39 +24,10 @@ class BlackListedActionTest < Minitest::Test
end

should "not execute job because it is blacklisted" do
assert_raises RuntimeError do
Job.create_from_request(JSON.parse(<<-EOS
{ "inputs" : ["one", "two", "three"],
"action" : "word_count",
"email" : "bob@example.com",
"callback_url" : "http://example.com/callback" }
EOS
))
end
assert NodeRecord.all.map(&:actions).flatten.uniq.include? @black_listed_action.action
refute NodeRecord.available_actions.include? @black_listed_action.action
end

# should "sucessfully execute job because blacklist item expires" do
# @black_listed_action.duration_in_seconds = 2
# @black_listed_action.save
# sleep(3)
# job = Job.create_from_request(JSON.parse(<<-EOS
# { "inputs" : ["one", "two", "three"],
# "action" : "word_count",
# "email" : "bob@example.com",
# "callback_url" : "http://example.com/callback" }
# EOS
# ))
# assert job.present?
# # end
# should "create blacklist job with a POST" do
# result = RestClient.post "localhost:9173/blacklist", {action: "graphics_magick"}
# assert BlackListedAction.where(action: "graphics_magick").present?
# end

# should "delete blacklist job with a DELETE" do

# assert job.present?
# end
end

end

0 comments on commit f1ca0cd

Please sign in to comment.