channel.rb 7.4 KB

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