Class: Flydata::Helper::ActionOwnershipChannel

Inherits:
Object
  • Object
show all
Includes:
FlydataCore::Logger, MonitorMixin
Defined in:
lib/flydata/helper/action_ownership_channel.rb

Overview

Take care of action ownership management

  • Receive a request from both scheduler and worker.

  • Give a action with ownership to workers

Instance Method Summary collapse

Constructor Details

#initializeActionOwnershipChannel

Returns a new instance of ActionOwnershipChannel.



13
14
15
16
17
# File 'lib/flydata/helper/action_ownership_channel.rb', line 13

def initialize
  @action_ownership_map = ActionOwnership.action_ownership_map
  @queue = {}  # key: ["action_name", {<action_info>}]
  super
end

Instance Method Details

#dumpObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/flydata/helper/action_ownership_channel.rb', line 101

def dump
  queue_info = @queue.collect{|k,v|
    "- #{k} #{v[:action_info]}"
  }.join("\n")

  map_info = @action_ownership_map.collect{|k,v|
    "- #{v.action_name} owner:\"#{v.owner}\" " +
    "retry:#{v.retry_count} " +
    "last:\"#{v.last_processed_time > 0 ? Time.at(v.last_processed_time).utc : '-'}\""
  }.join("\n")

  <<EOT

========
action_ownership_channel dump
# queue:
#{queue_info}
# action_ownership_map:
#{map_info}
========
EOT
end

#mapObject



90
91
92
# File 'lib/flydata/helper/action_ownership_channel.rb', line 90

def map
  @action_ownership_map.dup
end

#queueObject

For debug



86
87
88
# File 'lib/flydata/helper/action_ownership_channel.rb', line 86

def queue
  @queue.dup
end

#queue_key(action_name, action_info) ⇒ Object



44
45
46
# File 'lib/flydata/helper/action_ownership_channel.rb', line 44

def queue_key(action_name, action_info)
  [action_name, (action_info || {})[:id]].to_json
end

#remote_actions_exist?Boolean

Returns:

  • (Boolean)


94
95
96
97
98
99
# File 'lib/flydata/helper/action_ownership_channel.rb', line 94

def remote_actions_exist?
  queue.any?{|k, v| v.kind_of?(Hash) &&
             v[:action_info].kind_of?(Hash) &&
             v[:action_info].has_key?(:id) }

end

#request_action(action_name, action_info = {}) ⇒ Object

Called from schdeduler and worker action_name must be symbol



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/flydata/helper/action_ownership_channel.rb', line 21

def request_action(action_name, action_info = {})
  action_ownership = @action_ownership_map[action_name]
  if action_ownership.nil?
    # unknown action.  probably a new action
    log_error "[error] unsupported action `#{action_name}'. Skip."
    return false
  end

  qkey = queue_key(action_name, action_info)
  self.synchronize do
    if @queue.has_key?(qkey)
      false
    else
      @queue[qkey] =
                {
                  action_ownership: action_ownership,
                  action_info: action_info
                }
      true
    end
  end
end

#return_action_ownership(action_ownership) ⇒ Object

Called from worker only



78
79
80
81
82
83
# File 'lib/flydata/helper/action_ownership_channel.rb', line 78

def return_action_ownership(action_ownership)
  self.synchronize do
    action_ownership.owner = nil
    action_ownership.last_processed_time = Time.now.to_f
  end
end

#take_action_ownership(new_owner) ⇒ Object

Called from worker only



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/flydata/helper/action_ownership_channel.rb', line 49

def take_action_ownership(new_owner)
  self.synchronize do
    return nil if @queue.empty?

    # Check resource change flag
    rsc_act_running = @action_ownership_map.any? { |name, act_own|
        (act_own.resource_change && act_own.processing?) }

    target_key, target_action = @queue.find do |key, action|
      action_ownership = action[:action_ownership]

      # Wait until a same action will be processed
      next if action_ownership.processing?

      # Skip if another resource change action is running
      next if action_ownership.resource_change && rsc_act_running

      true
    end

    return nil if target_key.nil? || target_action.nil?
    @queue.delete(target_key)

    target_action[:action_ownership].owner = new_owner
    target_action
  end
end