channel.rb 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Channel < ApplicationModel
  3. load 'channel/assets.rb'
  4. include Channel::Assets
  5. belongs_to :group, class_name: 'Group'
  6. store :options
  7. store :preferences
  8. after_create :email_address_check
  9. after_update :email_address_check
  10. after_destroy :email_address_check
  11. # rubocop:disable Style/ClassVars
  12. @@channel_stream = {}
  13. # rubocop:enable Style/ClassVars
  14. =begin
  15. fetch all accounts
  16. Channel.fetch
  17. =end
  18. def self.fetch
  19. channels = Channel.where('active = ? AND area LIKE ?', true, '%::Account')
  20. channels.each(&:fetch)
  21. end
  22. =begin
  23. fetch one account
  24. channel = Channel.where(area: 'Email::Account').first
  25. channel.fetch
  26. =end
  27. def fetch(force = false)
  28. adapter = options[:adapter]
  29. adapter_options = options
  30. if options[:inbound] && options[:inbound][:adapter]
  31. adapter = options[:inbound][:adapter]
  32. adapter_options = options[:inbound][:options]
  33. end
  34. begin
  35. # we need to require each channel backend individually otherwise we get a
  36. # 'warning: toplevel constant Twitter referenced by Channel::Driver::Twitter' error e.g.
  37. # so we have to convert the channel name to the filename via Rails String.underscore
  38. # http://stem.ps/rails/2015/01/25/ruby-gotcha-toplevel-constant-referenced-by.html
  39. require "channel/driver/#{adapter.to_filename}"
  40. driver_class = Object.const_get("Channel::Driver::#{adapter.to_classname}")
  41. driver_instance = driver_class.new
  42. return if !force && !driver_instance.fetchable?(self)
  43. result = driver_instance.fetch(adapter_options, self)
  44. self.status_in = result[:result]
  45. self.last_log_in = result[:notice]
  46. preferences[:last_fetch] = Time.zone.now
  47. save
  48. rescue => e
  49. error = "Can't use Channel::Driver::#{adapter.to_classname}: #{e.inspect}"
  50. logger.error error
  51. logger.error e.backtrace
  52. self.status_in = 'error'
  53. self.last_log_in = error
  54. preferences[:last_fetch] = Time.zone.now
  55. save
  56. end
  57. end
  58. =begin
  59. stream instance of account
  60. channel = Channel.where(area: 'Twitter::Account').first
  61. stream_instance = channel.stream_instance
  62. # start stream
  63. stream_instance.stream
  64. =end
  65. def stream_instance
  66. adapter = options[:adapter]
  67. begin
  68. # we need to require each channel backend individually otherwise we get a
  69. # 'warning: toplevel constant Twitter referenced by Channel::Driver::Twitter' error e.g.
  70. # so we have to convert the channel name to the filename via Rails String.underscore
  71. # http://stem.ps/rails/2015/01/25/ruby-gotcha-toplevel-constant-referenced-by.html
  72. require "channel/driver/#{adapter.to_filename}"
  73. driver_class = Object.const_get("Channel::Driver::#{adapter.to_classname}")
  74. driver_instance = driver_class.new
  75. # check is stream exists
  76. return if !driver_instance.respond_to?(:stream_instance)
  77. driver_instance.stream_instance(self)
  78. # set scheduler job to active
  79. return driver_instance
  80. rescue => e
  81. error = "Can't use Channel::Driver::#{adapter.to_classname}: #{e.inspect}"
  82. logger.error error
  83. logger.error e.backtrace
  84. self.status_in = 'error'
  85. self.last_log_in = error
  86. save
  87. end
  88. end
  89. =begin
  90. stream all accounts
  91. Channel.stream
  92. =end
  93. def self.stream
  94. Thread.abort_on_exception = true
  95. auto_reconnect_after = 25
  96. last_channels = []
  97. loop do
  98. logger.debug 'stream controll loop'
  99. current_channels = []
  100. channels = Channel.where('active = ? AND area LIKE ?', true, '%::Account')
  101. channels.each { |channel|
  102. next if channel.options[:adapter] != 'twitter'
  103. channel_id = channel.id.to_s
  104. current_channels.push channel_id
  105. # exit it channel has changed or connection is older then 25 min.
  106. if @@channel_stream[channel_id]
  107. if @@channel_stream[channel_id][:updated_at] != channel.updated_at
  108. logger.info "channel (#{channel.id}) has changed, stop thread"
  109. @@channel_stream[channel_id][:thread].exit
  110. @@channel_stream[channel_id][:thread].join
  111. @@channel_stream[channel_id][:stream_instance].disconnect
  112. @@channel_stream[channel_id] = false
  113. elsif @@channel_stream[channel_id][:started_at] && @@channel_stream[channel_id][:started_at] < Time.zone.now - auto_reconnect_after.minutes
  114. logger.info "channel (#{channel.id}) reconnect - thread is older then #{auto_reconnect_after} minutes, restart thread"
  115. @@channel_stream[channel_id][:thread].exit
  116. @@channel_stream[channel_id][:thread].join
  117. @@channel_stream[channel_id][:stream_instance].disconnect
  118. @@channel_stream[channel_id] = false
  119. end
  120. end
  121. #logger.debug "thread for channel (#{channel.id}) already running" if channel_stream
  122. next if @@channel_stream[channel_id]
  123. @@channel_stream[channel_id] = {
  124. updated_at: channel.updated_at,
  125. started_at: Time.zone.now,
  126. }
  127. # start channels with delay
  128. sleep @@channel_stream.count
  129. # start threads for each channel
  130. @@channel_stream[channel_id][:thread] = Thread.new {
  131. begin
  132. logger.info "Started stream channel for '#{channel.id}' (#{channel.area})..."
  133. @@channel_stream[channel_id] ||= {}
  134. @@channel_stream[channel_id][:stream_instance] = channel.stream_instance
  135. @@channel_stream[channel_id][:stream_instance].stream
  136. @@channel_stream[channel_id][:stream_instance].disconnect
  137. @@channel_stream[channel_id] = false
  138. logger.info " ...stopped thread for '#{channel.id}'"
  139. rescue => e
  140. error = "Can't use channel (#{channel.id}): #{e.inspect}"
  141. logger.error error
  142. logger.error e.backtrace
  143. channel.status_in = 'error'
  144. channel.last_log_in = error
  145. channel.save
  146. @@channel_stream[channel_id] = false
  147. end
  148. }
  149. }
  150. # cleanup deleted channels
  151. last_channels.each { |channel_id|
  152. next if !@@channel_stream[channel_id.to_s]
  153. next if current_channels.include?(channel_id)
  154. logger.info "channel (#{channel_id}) not longer active, stop thread"
  155. @@channel_stream[channel_id.to_s][:thread].exit
  156. @@channel_stream[channel_id.to_s][:thread].join
  157. @@channel_stream[channel_id.to_s][:stream_instance].disconnect
  158. @@channel_stream[channel_id.to_s] = false
  159. }
  160. last_channels = current_channels
  161. sleep 20
  162. end
  163. end
  164. =begin
  165. send via account
  166. channel = Channel.where(area: 'Email::Account').first
  167. channel.deliver(mail_params, notification)
  168. =end
  169. def deliver(mail_params, notification = false)
  170. # ignore notifications in developer mode
  171. if notification == true && Setting.get('developer_mode') == true
  172. logger.info "Do not send notification #{mail_params.inspect} because of enabled developer_mode"
  173. return
  174. end
  175. adapter = options[:adapter]
  176. adapter_options = options
  177. if options[:outbound] && options[:outbound][:adapter]
  178. adapter = options[:outbound][:adapter]
  179. adapter_options = options[:outbound][:options]
  180. end
  181. result = nil
  182. begin
  183. # we need to require each channel backend individually otherwise we get a
  184. # 'warning: toplevel constant Twitter referenced by Channel::Driver::Twitter' error e.g.
  185. # so we have to convert the channel name to the filename via Rails String.underscore
  186. # http://stem.ps/rails/2015/01/25/ruby-gotcha-toplevel-constant-referenced-by.html
  187. require "channel/driver/#{adapter.to_filename}"
  188. driver_class = Object.const_get("Channel::Driver::#{adapter.to_classname}")
  189. driver_instance = driver_class.new
  190. result = driver_instance.send(adapter_options, mail_params, notification)
  191. self.status_out = 'ok'
  192. self.last_log_out = ''
  193. save
  194. rescue => e
  195. error = "Can't use Channel::Driver::#{adapter.to_classname}: #{e.inspect}"
  196. logger.error error
  197. logger.error e.backtrace
  198. self.status_out = 'error'
  199. self.last_log_out = error
  200. save
  201. raise error
  202. end
  203. result
  204. end
  205. private
  206. def email_address_check
  207. # reset non existing channel_ids
  208. EmailAddress.channel_cleanup
  209. end
  210. end