Nenhuma Descrição http://j1x-huginn.herokuapp.com

agent.rb 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. require 'serialize_and_symbolize'
  2. require 'assignable_types'
  3. require 'markdown_class_attributes'
  4. require 'utils'
  5. class Agent < ActiveRecord::Base
  6. include SerializeAndSymbolize
  7. include AssignableTypes
  8. include MarkdownClassAttributes
  9. serialize_and_symbolize :options, :memory
  10. markdown_class_attributes :description, :event_description
  11. load_types_in "Agents"
  12. SCHEDULES = %w[every_2m every_5m every_10m every_30m every_1h every_2h every_5h every_12h every_1d every_2d every_7d
  13. midnight 1am 2am 3am 4am 5am 6am 7am 8am 9am 10am 11am noon 1pm 2pm 3pm 4pm 5pm 6pm 7pm 8pm 9pm 10pm 11pm]
  14. attr_accessible :options, :memory, :name, :type, :schedule, :source_ids
  15. validates_presence_of :name, :user
  16. validate :sources_are_owned
  17. validate :validate_schedule
  18. after_initialize :set_default_schedule
  19. before_validation :set_default_schedule
  20. before_validation :unschedule_if_cannot_schedule
  21. before_save :unschedule_if_cannot_schedule
  22. belongs_to :user, :inverse_of => :agents
  23. has_many :events, :dependent => :delete_all, :inverse_of => :agent, :order => "events.id desc"
  24. has_many :received_events, :through => :sources, :class_name => "Event", :source => :events, :order => "events.id desc"
  25. has_many :links_as_source, :dependent => :delete_all, :foreign_key => "source_id", :class_name => "Link", :inverse_of => :source
  26. has_many :links_as_receiver, :dependent => :delete_all, :foreign_key => "receiver_id", :class_name => "Link", :inverse_of => :receiver
  27. has_many :sources, :through => :links_as_receiver, :class_name => "Agent", :inverse_of => :receivers
  28. has_many :receivers, :through => :links_as_source, :class_name => "Agent", :inverse_of => :sources
  29. scope :of_type, lambda { |type|
  30. type = case type
  31. when String, Symbol, Class
  32. type.to_s
  33. when Agent
  34. type.class.to_s
  35. else
  36. type.to_s
  37. end
  38. where(:type => type)
  39. }
  40. def check
  41. # Implement me in your subclass of Agent.
  42. end
  43. def default_options
  44. # Implement me in your subclass of Agent.
  45. {}
  46. end
  47. def receive(events)
  48. # Implement me in your subclass of Agent.
  49. end
  50. # Implement me in your subclass to decide if your Agent is working.
  51. def working?
  52. raise "Implement me in your subclass"
  53. end
  54. def event_created_within(seconds)
  55. last_event = events.first
  56. last_event && last_event.created_at > seconds.ago && last_event
  57. end
  58. def sources_are_owned
  59. errors.add(:sources, "must be owned by you") unless sources.all? {|s| s.user == user }
  60. end
  61. def create_event(attrs)
  62. events.create!({ :user => user }.merge(attrs))
  63. end
  64. def validate_schedule
  65. unless cannot_be_scheduled?
  66. errors.add(:schedule, "is not a valid schedule") unless SCHEDULES.include?(schedule.to_s)
  67. end
  68. end
  69. def make_message(payload, message = options[:message])
  70. message.gsub(/<([^>]+)>/) { Utils.value_at(payload, $1) || "??" }
  71. end
  72. def set_default_schedule
  73. self.schedule = default_schedule unless schedule.present? || cannot_be_scheduled?
  74. end
  75. def unschedule_if_cannot_schedule
  76. self.schedule = nil if cannot_be_scheduled?
  77. end
  78. def last_event_at
  79. @memoized_last_event_at ||= events.select(:created_at).first.try(:created_at)
  80. end
  81. def default_schedule
  82. self.class.default_schedule
  83. end
  84. def cannot_be_scheduled?
  85. self.class.cannot_be_scheduled?
  86. end
  87. def can_be_scheduled?
  88. !cannot_be_scheduled?
  89. end
  90. def cannot_receive_events?
  91. self.class.cannot_receive_events?
  92. end
  93. def can_receive_events?
  94. !cannot_receive_events?
  95. end
  96. # Class Methods
  97. class << self
  98. def cannot_be_scheduled!
  99. @cannot_be_scheduled = true
  100. end
  101. def cannot_be_scheduled?
  102. !!@cannot_be_scheduled
  103. end
  104. def default_schedule(schedule = nil)
  105. @default_schedule = schedule unless schedule.nil?
  106. @default_schedule
  107. end
  108. def cannot_receive_events!
  109. @cannot_receive_events = true
  110. end
  111. def cannot_receive_events?
  112. !!@cannot_receive_events
  113. end
  114. def receive!
  115. sql = Agent.
  116. select("agents.id AS receiver_agent_id, sources.id AS source_agent_id, events.id AS event_id").
  117. joins("JOIN links ON (links.receiver_id = agents.id)").
  118. joins("JOIN agents AS sources ON (links.source_id = sources.id)").
  119. joins("JOIN events ON (events.agent_id = sources.id)").
  120. where("agents.last_checked_event_id IS NULL OR events.id > agents.last_checked_event_id").to_sql
  121. agents_to_events = {}
  122. Agent.connection.select_rows(sql).each do |receiver_agent_id, source_agent_id, event_id|
  123. agents_to_events[receiver_agent_id] ||= []
  124. agents_to_events[receiver_agent_id] << event_id
  125. end
  126. event_ids = agents_to_events.values.flatten.uniq.compact
  127. Agent.where(:id => agents_to_events.keys).each do |agent|
  128. agent.update_attribute :last_checked_event_id, event_ids.max
  129. Agent.async_receive(agent.id, agents_to_events[agent.id].uniq)
  130. end
  131. {
  132. :agent_count => agents_to_events.keys.length,
  133. :event_count => event_ids.length
  134. }
  135. end
  136. # Given an Agent id and an array of Event ids, load the Agent, call #receive on it with the Event objects, and then
  137. # save it with an updated _last_receive_at_ timestamp.
  138. #
  139. # This method is tagged with _handle_asynchronously_ and will be delayed and run with delayed_job. It accepts Agent
  140. # and Event ids instead of a literal ActiveRecord models because it is preferable to serialize delayed_jobs with ids.
  141. def async_receive(agent_id, event_ids)
  142. agent = Agent.find(agent_id)
  143. agent.receive(Event.where(:id => event_ids))
  144. agent.last_receive_at = Time.now
  145. agent.save!
  146. end
  147. handle_asynchronously :async_receive
  148. def run_schedule(schedule)
  149. types = where(:schedule => schedule).group(:type).pluck(:type)
  150. types.each do |type|
  151. type.constantize.bulk_check(schedule)
  152. end
  153. end
  154. # You can override this to define a custom bulk_check for your type of Agent.
  155. def bulk_check(schedule)
  156. raise "Call #bulk_check on the appropriate subclass of Agent" if self == Agent
  157. where(:schedule => schedule).pluck("agents.id").each do |agent_id|
  158. async_check(agent_id)
  159. end
  160. end
  161. # Given an Agent id, load the Agent, call #check on it, and then save it with an updated _last_check_at_ timestamp.
  162. #
  163. # This method is tagged with _handle_asynchronously_ and will be delayed and run with delayed_job. It accepts an Agent
  164. # id instead of a literal Agent because it is preferable to serialize delayed_jobs with ids.
  165. def async_check(agent_id)
  166. agent = Agent.find(agent_id)
  167. agent.check
  168. agent.last_check_at = Time.now
  169. agent.save!
  170. end
  171. handle_asynchronously :async_check
  172. end
  173. end