Introduced config.interface_handle to find out via which interface the source code got executed.

This commit is contained in:
Martin Edenhofer 2016-08-20 21:29:22 +02:00
parent e2eef259b4
commit 4628299d20
27 changed files with 126 additions and 54 deletions

View file

@ -15,7 +15,7 @@ class ApplicationController < ActionController::Base
:model_index_render
skip_before_action :verify_authenticity_token
before_action :set_user, :session_update, :user_device_check, :cors_preflight_check
before_action :set_interface_handle, :set_user, :session_update, :user_device_check, :cors_preflight_check
after_action :trigger_events, :http_log, :set_access_control_headers
rescue_from StandardError, with: :server_error
@ -58,6 +58,10 @@ class ApplicationController < ActionController::Base
private
def set_interface_handle
ApplicationHandleInfo.current = 'application_server'
end
# execute events
def trigger_events
Observer::Transaction.commit

View file

@ -390,6 +390,8 @@ retrns
end
def _process(channel, msg)
# parse email
mail = parse(msg)
# run postmaster pre filter
@ -410,7 +412,14 @@ retrns
}
# check ignore header
return true if mail[ 'x-zammad-ignore'.to_sym ] == 'true' || mail[ 'x-zammad-ignore'.to_sym ] == true
if mail[ 'x-zammad-ignore'.to_sym ] == 'true' || mail[ 'x-zammad-ignore'.to_sym ] == true
Rails.logger.info "ignored email with msgid '#{mail[:message_id]}' from '#{mail[:from]}' because of x-zammad-ignore header"
return true
end
# set interface handle
original_interface_handle = ApplicationHandleInfo.current
ApplicationHandleInfo.current = "#{original_interface_handle}.postmaster"
ticket = nil
article = nil
@ -497,11 +506,9 @@ retrns
set_attributes_by_x_headers(ticket, 'ticket', mail)
# create ticket
ticket.save
ticket.save!
end
# import mail
# set attributes
article = Ticket::Article.new(
ticket_id: ticket.id,
@ -521,7 +528,7 @@ retrns
set_attributes_by_x_headers(article, 'article', mail)
# create article
article.save
article.save!
# store mail plain
Store.add(
@ -546,6 +553,8 @@ retrns
end
end
ApplicationHandleInfo.current = original_interface_handle
# execute object transaction
Observer::Transaction.commit
@ -617,8 +626,8 @@ retrns
item_object[key] = mail[ header.to_sym ]
end
}
end
end
# workaround to parse subjects with 2 different encodings correctly (e. g. quoted-printable see test/fixtures/mail9.box)

View file

@ -8,7 +8,7 @@ module Channel::Filter::Database
# process postmaster filter
filters = PostmasterFilter.where( active: true, channel: 'email' ).order(:name, :created_at)
filters.each { |filter|
Rails.logger.info " proccess filter #{filter.name} ..."
Rails.logger.info " process filter #{filter.name} ..."
all_matches_ok = true
min_one_rule_exists = false
filter[:match].each { |key, meta|

View file

@ -8,7 +8,11 @@ class Observer::Ticket::Article::CommunicateEmail < ActiveRecord::Observer
# return if we run import mode
return if Setting.get('import_mode')
# if sender is customer, do not communication
# only do send email if article got created via application_server (e. g. not
# if article and sender type is set via *.postmaster)
return if ApplicationHandleInfo.current.split('.')[1] == 'postmaster'
# if sender is customer, do not communicate
sender = Ticket::Article::Sender.lookup(id: record.sender_id)
return 1 if sender.nil?
return 1 if sender['name'] == 'Customer'

View file

@ -66,7 +66,7 @@ class Observer::Ticket::Article::CommunicateEmail::BackgroundJob
record.preferences['delivery_status_message'] = nil
record.preferences['delivery_status'] = 'success'
record.preferences['delivery_status_date'] = Time.zone.now
record.save
record.save!
# store mail plain
Store.add(
@ -91,6 +91,8 @@ class Observer::Ticket::Article::CommunicateEmail::BackgroundJob
recipient_list += record[key]
}
Rails.logger.info "Send email to: '#{recipient_list}' (from #{record.from})"
return if recipient_list == ''
History.add(

View file

@ -10,7 +10,11 @@ class Observer::Ticket::Article::CommunicateFacebook < ActiveRecord::Observer
# return if we run import mode
return if Setting.get('import_mode')
# if sender is customer, do not communication
# only do send email if article got created via application_server (e. g. not
# if article and sender type is set via *.postmaster)
return if ApplicationHandleInfo.current.split('.')[1] == 'postmaster'
# if sender is customer, do not communicate
sender = Ticket::Article::Sender.lookup(id: record.sender_id)
return 1 if sender.nil?
return 1 if sender['name'] == 'Customer'

View file

@ -59,7 +59,11 @@ class Observer::Ticket::Article::CommunicateFacebook::BackgroundJob
article.preferences['delivery_status'] = 'success'
article.preferences['delivery_status_date'] = Time.zone.now
article.save
article.save!
Rails.logger.info "Send facebook to: '#{article.to}' (from #{article.from})"
article
end
def log_error(local_record, message)

View file

@ -8,7 +8,11 @@ class Observer::Ticket::Article::CommunicateTwitter < ActiveRecord::Observer
# return if we run import mode
return if Setting.get('import_mode')
# if sender is customer, do not communication
# only do send email if article got created via application_server (e. g. not
# if article and sender type is set via *.postmaster)
return if ApplicationHandleInfo.current.split('.')[1] == 'postmaster'
# if sender is customer, do not communicate
sender = Ticket::Article::Sender.lookup(id: record.sender_id)
return if sender.nil?
return if sender['name'] == 'Customer'

View file

@ -68,8 +68,11 @@ class Observer::Ticket::Article::CommunicateTwitter::BackgroundJob
article.preferences['delivery_status_date'] = Time.zone.now
article.message_id = tweet.id.to_s
article.save
article.save!
Rails.logger.info "Send twitter (#{tweet.class}) to: '#{article.to}' (from #{article.from})"
article
end
def log_error(local_record, message)

View file

@ -8,6 +8,10 @@ class Observer::Ticket::Article::FillupFromEmail < ActiveRecord::Observer
# return if we run import mode
return if Setting.get('import_mode')
# only do fill of email from if article got created via application_server (e. g. not
# if article and sender type is set via *.postmaster)
return if ApplicationHandleInfo.current.split('.')[1] == 'postmaster'
# if sender is customer, do not change anything
sender = Ticket::Article::Sender.lookup(id: record.sender_id)
return if sender.nil?

View file

@ -8,6 +8,10 @@ class Observer::Ticket::Article::FillupFromGeneral < ActiveRecord::Observer
# return if we run import mode
return if Setting.get('import_mode')
# only do fill of from if article got created via application_server (e. g. not
# if article and sender type is set via *.postmaster)
return if ApplicationHandleInfo.current.split('.')[1] == 'postmaster'
# if sender is customer, do not change anything
sender = Ticket::Article::Sender.lookup(id: record.sender_id)
return if sender.nil?

View file

@ -5,11 +5,10 @@ class Observer::Transaction < ActiveRecord::Observer
def self.commit(params = {})
# add attribute if execution is via web
params[:via_web] = false
if ENV['RACK_ENV'] || Rails.configuration.webserver_is_active
params[:via_web] = true
end
# add attribute of interface handle (e. g. to send (no) notifications if a agent
# is creating a ticket via application_server, but send it if it's created via
# postmaster)
params[:interface_handle] = ApplicationHandleInfo.current
# execute object transactions
Observer::Transaction.perform(params)

View file

@ -47,6 +47,7 @@ class Scheduler < ApplicationModel
def self.start_job(job)
Thread.new {
ApplicationHandleInfo.current = 'scheduler'
logger.info "Started job thread for '#{job.name}' (#{job.method})..."
@ -118,8 +119,12 @@ class Scheduler < ApplicationModel
# used for tests
if foreground
original_interface_handle = ApplicationHandleInfo.current
ApplicationHandleInfo.current = 'scheduler'
original_user_id = UserInfo.current_user_id
UserInfo.current_user_id = nil
loop do
success, failure = Delayed::Worker.new.work_off
if failure.nonzero?
@ -128,6 +133,7 @@ class Scheduler < ApplicationModel
break if success.zero?
end
UserInfo.current_user_id = original_user_id
ApplicationHandleInfo.current = original_interface_handle
return
end
@ -139,6 +145,7 @@ class Scheduler < ApplicationModel
logger.info "Starting worker thread #{Delayed::Job}"
loop do
ApplicationHandleInfo.current = 'scheduler'
result = nil
realtime = Benchmark.realtime do

View file

@ -8,7 +8,7 @@ class Transaction::BackgroundJob
object: 'Ticket',
type: 'update',
ticket_id: 123,
via_web: true,
interface_handle: 'application_server', # application_server|websocket|scheduler
changes: {
'attribute1' => [before,now],
'attribute2' => [before,now],

View file

@ -8,7 +8,7 @@ class Transaction::CtiCallerIdDetection
object: 'Ticket',
type: 'create',
object_id: 123,
via_web: true,
interface_handle: 'application_server', # application_server|websocket|scheduler
user_id: 123,
created_at: Time.zone.now,
},
@ -16,7 +16,7 @@ class Transaction::CtiCallerIdDetection
object: 'User',
type: 'update',
object_id: 123,
via_web: true,
interface_handle: 'application_server', # application_server|websocket|scheduler
changes: {
'attribute1' => [before, now],
'attribute2' => [before, now],

View file

@ -7,7 +7,7 @@ class Transaction::Karma
object: 'Ticket',
type: 'update',
object_id: 123,
via_web: true,
interface_handle: 'application_server', # application_server|websocket|scheduler
changes: {
'attribute1' => [before, now],
'attribute2' => [before, now],

View file

@ -7,7 +7,7 @@ class Transaction::Notification
object: 'Ticket',
type: 'update',
object_id: 123,
via_web: true,
interface_handle: 'application_server', # application_server|websocket|scheduler
changes: {
'attribute1' => [before, now],
'attribute2' => [before, now],
@ -95,7 +95,7 @@ class Transaction::Notification
channels = item[:channels]
# ignore user who changed it by him self via web
if @params[:via_web]
if @params[:interface_handle] == 'application_server'
next if article && article.updated_by_id == user.id
next if !article && @item[:user_id] == user.id
end

View file

@ -8,7 +8,7 @@ class Transaction::SignatureDetection
object: 'Ticket',
type: 'update',
object_id: 123,
via_web: true,
interface_handle: 'application_server', # application_server|websocket|scheduler
changes: {
'attribute1' => [before, now],
'attribute2' => [before, now],

View file

@ -1,30 +1,25 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Transaction::Slack
=begin
backend = Transaction::Slack.new(
object: 'Ticket',
type: 'create',
object_id: 1,
user_id: 123,
created_at: Time.zone.now,
)
backend.perform
{
object: 'Ticket',
type: 'update',
object_id: 123,
via_web: true,
interface_handle: 'application_server', # application_server|websocket|scheduler
changes: {
'attribute1' => [before, now],
'attribute2' => [before, now],
},
created_at: Time.zone.now,
user_id: 123,
},
)
backend.perform
=end
def initialize(item, params = {})
@item = item
@params = params

View file

@ -7,7 +7,7 @@ class Transaction::Trigger
object: 'Ticket',
type: 'update',
object_id: 123,
via_web: true,
interface_handle: 'application_server', # application_server|websocket|scheduler
changes: {
'attribute1' => [before, now],
'attribute2' => [before, now],

View file

@ -1,2 +0,0 @@
Rails.configuration.webserver_is_active = false

View file

@ -0,0 +1,9 @@
module ApplicationHandleInfo
def self.current
Thread.current[:application_handle] || 'unknown'
end
def self.current=(name)
Thread.current[:application_handle] = name
end
end

View file

@ -59,7 +59,7 @@ create/update/delete index
)
Rails.logger.info "# #{response.code}"
return true if response.success?
raise "Unable to proccess PUT at #{url}\n#{response.inspect}"
raise "Unable to process PUT at #{url}\n#{response.inspect}"
end
=begin
@ -91,7 +91,7 @@ add new object to search index
)
Rails.logger.info "# #{response.code}"
return true if response.success?
raise "Unable to proccess POST at #{url} (size: #{data.to_json.bytesize / 1024 / 1024}M)\n#{response.inspect}"
raise "Unable to process POST at #{url} (size: #{data.to_json.bytesize / 1024 / 1024}M)\n#{response.inspect}"
end
=begin
@ -321,7 +321,7 @@ get count of tickets and tickets which match on selector
Rails.logger.info "# #{response.code}"
if !response.success?
raise "Unable to proccess POST at #{url}\n#{response.inspect}"
raise "Unable to process POST at #{url}\n#{response.inspect}"
end
Rails.logger.debug response.data.to_json

View file

@ -95,6 +95,7 @@ if ARGV[0] == 'start' && @options[:d]
end
@clients = {}
Rails.configuration.interface = 'websocket'
EventMachine.run {
EventMachine::WebSocket.start( host: @options[:b], port: @options[:p], secure: @options[:s], tls_options: tls_options ) do |ws|

View file

@ -42,6 +42,9 @@ class ActiveSupport::TestCase
# set current user
UserInfo.current_user_id = nil
# set interface handle
ApplicationHandleInfo.current = 'unknown'
Rails.logger.info '++++NEW++++TEST++++'
end
@ -64,4 +67,22 @@ class ActiveSupport::TestCase
count
end
def email_count(recipient)
# read config file and count & recipients
file = "#{Rails.root}/log/#{Rails.env}.log"
lines = []
IO.foreach(file) do |line|
lines.push line
end
count = 0
lines.reverse.each { |line|
break if line =~ /\+\+\+\+NEW\+\+\+\+TEST\+\+\+\+/
next if line !~ /Send email to:/
next if line !~ /to:\s'#{recipient}'/
count += 1
}
count
end
end

View file

@ -42,7 +42,7 @@ class OnlineNotificationTest < ActiveSupport::TestCase
test 'ticket notification' do
Rails.configuration.webserver_is_active = true
ApplicationHandleInfo.current = 'application_server'
# case #1
ticket1 = Ticket.create(

View file

@ -109,6 +109,7 @@ class TicketNotificationTest < ActiveSupport::TestCase
test 'ticket notification - to all agents / to explicit agents' do
# create ticket in group
ApplicationHandleInfo.current = 'scheduler.postmaster'
ticket1 = Ticket.create(
title: 'some notification test 1',
group: Group.lookup(name: 'TicketNotificationTest'),
@ -134,7 +135,6 @@ class TicketNotificationTest < ActiveSupport::TestCase
assert(ticket1)
# execute object transaction
Rails.configuration.webserver_is_active = nil
Observer::Transaction.commit
Scheduler.worker(true)
@ -143,6 +143,7 @@ class TicketNotificationTest < ActiveSupport::TestCase
assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket1, agent2, 'email'), ticket1.id)
# create ticket in group
ApplicationHandleInfo.current = 'application_server'
ticket1 = Ticket.create(
title: 'some notification test 2',
group: Group.lookup(name: 'TicketNotificationTest'),
@ -168,7 +169,6 @@ class TicketNotificationTest < ActiveSupport::TestCase
assert(ticket1)
# execute object transaction
Rails.configuration.webserver_is_active = true
Observer::Transaction.commit
Scheduler.worker(true)
@ -180,6 +180,7 @@ class TicketNotificationTest < ActiveSupport::TestCase
test 'ticket notification - simple' do
# create ticket in group
ApplicationHandleInfo.current = 'application_server'
ticket1 = Ticket.create(
title: 'some notification test 3',
group: Group.lookup(name: 'TicketNotificationTest'),
@ -205,7 +206,6 @@ class TicketNotificationTest < ActiveSupport::TestCase
assert(ticket1, 'ticket created - ticket notification simple')
# execute object transaction
Rails.configuration.webserver_is_active = true
Observer::Transaction.commit
Scheduler.worker(true)
@ -474,6 +474,7 @@ class TicketNotificationTest < ActiveSupport::TestCase
agent2.save
# create ticket in group
ApplicationHandleInfo.current = 'scheduler.postmaster'
ticket1 = Ticket.create(
title: 'some notification test - z preferences tests 1',
group: Group.lookup(name: 'TicketNotificationTest'),
@ -498,7 +499,6 @@ class TicketNotificationTest < ActiveSupport::TestCase
)
# execute object transaction
Rails.configuration.webserver_is_active = false
Observer::Transaction.commit
Scheduler.worker(true)
@ -630,6 +630,7 @@ class TicketNotificationTest < ActiveSupport::TestCase
agent2.save
# create ticket in group
ApplicationHandleInfo.current = 'scheduler.postmaster'
ticket4 = Ticket.create(
title: 'some notification test - z preferences tests 4',
group: Group.lookup(name: 'TicketNotificationTest'),
@ -654,7 +655,6 @@ class TicketNotificationTest < ActiveSupport::TestCase
)
# execute object transaction
Rails.configuration.webserver_is_active = false
Observer::Transaction.commit
Scheduler.worker(true)
@ -694,6 +694,7 @@ class TicketNotificationTest < ActiveSupport::TestCase
agent2.save
# create ticket in group
ApplicationHandleInfo.current = 'scheduler.postmaster'
ticket5 = Ticket.create(
title: 'some notification test - z preferences tests 5',
group: Group.lookup(name: 'TicketNotificationTest'),
@ -718,7 +719,6 @@ class TicketNotificationTest < ActiveSupport::TestCase
)
# execute object transaction
Rails.configuration.webserver_is_active = false
Observer::Transaction.commit
Scheduler.worker(true)
@ -758,6 +758,7 @@ class TicketNotificationTest < ActiveSupport::TestCase
agent2.save
# create ticket in group
ApplicationHandleInfo.current = 'scheduler.postmaster'
ticket6 = Ticket.create(
title: 'some notification test - z preferences tests 6',
group: Group.lookup(name: 'TicketNotificationTest'),
@ -783,7 +784,6 @@ class TicketNotificationTest < ActiveSupport::TestCase
)
# execute object transaction
Rails.configuration.webserver_is_active = false
Observer::Transaction.commit
Scheduler.worker(true)
@ -835,6 +835,7 @@ class TicketNotificationTest < ActiveSupport::TestCase
agent2.save
# create ticket in group
ApplicationHandleInfo.current = 'scheduler.postmaster'
ticket7 = Ticket.create(
title: 'some notification test - z preferences tests 7',
group: Group.lookup(name: 'TicketNotificationTest'),
@ -860,7 +861,6 @@ class TicketNotificationTest < ActiveSupport::TestCase
)
# execute object transaction
Rails.configuration.webserver_is_active = false
Observer::Transaction.commit
Scheduler.worker(true)