Improved logging, moved from get to post.
This commit is contained in:
parent
a96d288d48
commit
e3a4fe9f8a
1 changed files with 46 additions and 35 deletions
|
@ -4,14 +4,14 @@ module Import::OTRS2
|
||||||
|
|
||||||
=begin
|
=begin
|
||||||
|
|
||||||
result = request_json('Subaction=List', 1)
|
result = request_json( :Subaction => 'List', 1)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
{ some json structure }
|
{ some json structure }
|
||||||
|
|
||||||
|
|
||||||
result = request_json('Subaction=List')
|
result = request_json( :Subaction => 'List' )
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -19,8 +19,8 @@ module Import::OTRS2
|
||||||
|
|
||||||
=end
|
=end
|
||||||
|
|
||||||
def self.request_json(part, data_only = false)
|
def self.request_json(data, data_only = false)
|
||||||
response = request(part)
|
response = post(data)
|
||||||
if !response
|
if !response
|
||||||
raise "Can't connect to Zammad Migrator"
|
raise "Can't connect to Zammad Migrator"
|
||||||
end
|
end
|
||||||
|
@ -52,19 +52,19 @@ module Import::OTRS2
|
||||||
|
|
||||||
def self.request(part)
|
def self.request(part)
|
||||||
url = Setting.get('import_otrs_endpoint') + part + ';Key=' + Setting.get('import_otrs_endpoint_key')
|
url = Setting.get('import_otrs_endpoint') + part + ';Key=' + Setting.get('import_otrs_endpoint_key')
|
||||||
puts 'GET: ' + url
|
log 'GET: ' + url
|
||||||
response = UserAgent.get(
|
response = UserAgent.get(
|
||||||
url,
|
url,
|
||||||
{},
|
{},
|
||||||
{
|
{
|
||||||
:open_timeout => 6,
|
:open_timeout => 10,
|
||||||
:read_timeout => 60,
|
:read_timeout => 60,
|
||||||
:user => Setting.get('import_otrs_user'),
|
:user => Setting.get('import_otrs_user'),
|
||||||
:password => Setting.get('import_otrs_password'),
|
:password => Setting.get('import_otrs_password'),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if !response.success?
|
if !response.success?
|
||||||
puts "ERROR: #{response.error}"
|
log "ERROR: #{response.error}"
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
response
|
response
|
||||||
|
@ -85,9 +85,11 @@ module Import::OTRS2
|
||||||
def self.post(data, url = nil)
|
def self.post(data, url = nil)
|
||||||
if !url
|
if !url
|
||||||
url = Setting.get('import_otrs_endpoint')
|
url = Setting.get('import_otrs_endpoint')
|
||||||
|
data['Action'] = 'ZammadMigrator'
|
||||||
end
|
end
|
||||||
data['Key'] = Setting.get('import_otrs_endpoint_key')
|
data['Key'] = Setting.get('import_otrs_endpoint_key')
|
||||||
puts 'POST: ' + url
|
log 'POST: ' + url
|
||||||
|
log 'PARAMS: ' + data.inspect
|
||||||
response = UserAgent.post(
|
response = UserAgent.post(
|
||||||
url,
|
url,
|
||||||
data,
|
data,
|
||||||
|
@ -99,7 +101,7 @@ module Import::OTRS2
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if !response.success?
|
if !response.success?
|
||||||
puts "ERROR: #{response.error}"
|
log "ERROR: #{response.error}"
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
response
|
response
|
||||||
|
@ -184,7 +186,7 @@ module Import::OTRS2
|
||||||
=end
|
=end
|
||||||
|
|
||||||
def self.load( object, limit = '', offset = '', diff = 0 )
|
def self.load( object, limit = '', offset = '', diff = 0 )
|
||||||
request_json( ";Subaction=Export;Object=#{object};Limit=#{limit};Offset=#{offset};Diff=#{diff}", 1 )
|
request_json( { :Subaction => 'Export', :Object => object, :Limit => limit, :Offset => offset, :Diff => diff }, 1 )
|
||||||
end
|
end
|
||||||
|
|
||||||
=begin
|
=begin
|
||||||
|
@ -200,7 +202,7 @@ module Import::OTRS2
|
||||||
=end
|
=end
|
||||||
|
|
||||||
def self.connection_test
|
def self.connection_test
|
||||||
return self.request_json('')
|
return self.request_json({})
|
||||||
end
|
end
|
||||||
|
|
||||||
=begin
|
=begin
|
||||||
|
@ -228,7 +230,7 @@ module Import::OTRS2
|
||||||
end
|
end
|
||||||
|
|
||||||
# retrive statistic
|
# retrive statistic
|
||||||
statistic = self.request_json(';Subaction=List', 1)
|
statistic = self.request_json( { :Subaction => 'List' }, 1)
|
||||||
if statistic
|
if statistic
|
||||||
Cache.write('import_otrs_stats', statistic)
|
Cache.write('import_otrs_stats', statistic)
|
||||||
end
|
end
|
||||||
|
@ -286,14 +288,14 @@ module Import::OTRS2
|
||||||
#
|
#
|
||||||
|
|
||||||
def self.start
|
def self.start
|
||||||
puts 'Start import...'
|
log 'Start import...'
|
||||||
|
|
||||||
# check if system is in import mode
|
# check if system is in import mode
|
||||||
if !Setting.get('import_mode')
|
if !Setting.get('import_mode')
|
||||||
raise "System is not in import mode!"
|
raise "System is not in import mode!"
|
||||||
end
|
end
|
||||||
|
|
||||||
result = request_json('')
|
result = request_json({})
|
||||||
if !result['Success']
|
if !result['Success']
|
||||||
"API key not valid!"
|
"API key not valid!"
|
||||||
end
|
end
|
||||||
|
@ -348,7 +350,7 @@ module Import::OTRS2
|
||||||
count += steps
|
count += steps
|
||||||
records = load('CustomerUser', steps, count-steps)
|
records = load('CustomerUser', steps, count-steps)
|
||||||
if !records || !records[0]
|
if !records || !records[0]
|
||||||
puts "all customers imported."
|
log "all customers imported."
|
||||||
run = false
|
run = false
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
@ -356,30 +358,31 @@ module Import::OTRS2
|
||||||
end
|
end
|
||||||
|
|
||||||
Thread.abort_on_exception = true
|
Thread.abort_on_exception = true
|
||||||
thread_count = 8
|
thread_count = 10
|
||||||
threads = {}
|
threads = {}
|
||||||
count = 0
|
count = 0
|
||||||
locks = { :User => {} }
|
locks = { :User => {} }
|
||||||
(1..thread_count).each {|thread|
|
(1..thread_count).each {|thread|
|
||||||
threads[thread] = Thread.new {
|
threads[thread] = Thread.new {
|
||||||
|
Thread.current[:thread_no] = thread
|
||||||
sleep thread * 3
|
sleep thread * 3
|
||||||
puts "Started import thread# #{thread} ..."
|
log "Started import thread# #{thread} ..."
|
||||||
run = true
|
run = true
|
||||||
steps = 20
|
steps = 20
|
||||||
while run
|
while run
|
||||||
count += steps
|
count += steps
|
||||||
puts "loading... thread# #{thread} ..."
|
log "loading... thread# #{thread} ..."
|
||||||
offset = count-steps
|
offset = count-steps
|
||||||
if offset != 0
|
if offset != 0
|
||||||
offset = count - steps + 1
|
offset = count - steps + 1
|
||||||
end
|
end
|
||||||
records = load( 'Ticket', steps, count-steps)
|
records = load( 'Ticket', steps, count-steps)
|
||||||
if !records || !records[0]
|
if !records || !records[0]
|
||||||
puts "... thread# #{thread}, no more work."
|
log "... thread# #{thread}, no more work."
|
||||||
run = false
|
run = false
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
_ticket_result(records, locks)
|
_ticket_result(records, locks, thread)
|
||||||
end
|
end
|
||||||
ActiveRecord::Base.connection.close
|
ActiveRecord::Base.connection.close
|
||||||
}
|
}
|
||||||
|
@ -401,7 +404,7 @@ module Import::OTRS2
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.diff
|
def self.diff
|
||||||
puts 'Start diff...'
|
log 'Start diff...'
|
||||||
|
|
||||||
# check if system is in import mode
|
# check if system is in import mode
|
||||||
if !Setting.get('import_mode')
|
if !Setting.get('import_mode')
|
||||||
|
@ -447,14 +450,14 @@ module Import::OTRS2
|
||||||
locks = { :User => {} }
|
locks = { :User => {} }
|
||||||
while run
|
while run
|
||||||
count += steps
|
count += steps
|
||||||
puts "loading... diff ..."
|
log "loading... diff ..."
|
||||||
offset = count-steps
|
offset = count-steps
|
||||||
if offset != 0
|
if offset != 0
|
||||||
offset = count - steps + 1
|
offset = count - steps + 1
|
||||||
end
|
end
|
||||||
records = load( 'Ticket', steps, count-steps, 1 )
|
records = load( 'Ticket', steps, count-steps, 1 )
|
||||||
if !records || !records[0]
|
if !records || !records[0]
|
||||||
puts "... no more work."
|
log "... no more work."
|
||||||
run = false
|
run = false
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
@ -463,7 +466,7 @@ module Import::OTRS2
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self._ticket_result(result, locks)
|
def self._ticket_result(result, locks, thread = '-')
|
||||||
# puts result.inspect
|
# puts result.inspect
|
||||||
map = {
|
map = {
|
||||||
:Ticket => {
|
:Ticket => {
|
||||||
|
@ -553,10 +556,10 @@ module Import::OTRS2
|
||||||
|
|
||||||
# set state types
|
# set state types
|
||||||
if ticket_old
|
if ticket_old
|
||||||
puts "update Ticket.find(#{ticket_new[:id]})"
|
log "update Ticket.find(#{ticket_new[:id]})"
|
||||||
ticket_old.update_attributes(ticket_new)
|
ticket_old.update_attributes(ticket_new)
|
||||||
else
|
else
|
||||||
puts "add Ticket.find(#{ticket_new[:id]})"
|
log "add Ticket.find(#{ticket_new[:id]})"
|
||||||
ticket = Ticket.new(ticket_new)
|
ticket = Ticket.new(ticket_new)
|
||||||
ticket.id = ticket_new[:id]
|
ticket.id = ticket_new[:id]
|
||||||
ticket.save
|
ticket.save
|
||||||
|
@ -590,7 +593,7 @@ module Import::OTRS2
|
||||||
|
|
||||||
# create article user if not exists
|
# create article user if not exists
|
||||||
while locks[:User][ email ]
|
while locks[:User][ email ]
|
||||||
puts "user #{email} is locked"
|
log "user #{email} is locked"
|
||||||
sleep 1
|
sleep 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -670,10 +673,10 @@ module Import::OTRS2
|
||||||
|
|
||||||
# set state types
|
# set state types
|
||||||
if article_old
|
if article_old
|
||||||
puts "update Ticket::Article.find(#{article_new[:id]})"
|
log "update Ticket::Article.find(#{article_new[:id]})"
|
||||||
article_old.update_attributes(article_new)
|
article_old.update_attributes(article_new)
|
||||||
else
|
else
|
||||||
puts "add Ticket::Article.find(#{article_new[:id]})"
|
log "add Ticket::Article.find(#{article_new[:id]})"
|
||||||
article = Ticket::Article.new(article_new)
|
article = Ticket::Article.new(article_new)
|
||||||
article.id = article_new[:id]
|
article.id = article_new[:id]
|
||||||
article.save
|
article.save
|
||||||
|
@ -987,7 +990,7 @@ module Import::OTRS2
|
||||||
|
|
||||||
# create / update agent
|
# create / update agent
|
||||||
if user_old
|
if user_old
|
||||||
puts "update User.find(#{user_old[:id]})"
|
log "update User.find(#{user_old[:id]})"
|
||||||
|
|
||||||
# only update roles if different (reduce sql statements)
|
# only update roles if different (reduce sql statements)
|
||||||
if user_old.role_ids == user_new[:role_ids]
|
if user_old.role_ids == user_new[:role_ids]
|
||||||
|
@ -996,7 +999,7 @@ module Import::OTRS2
|
||||||
|
|
||||||
user_old.update_attributes(user_new)
|
user_old.update_attributes(user_new)
|
||||||
else
|
else
|
||||||
puts "add User.find(#{user_new[:id]})"
|
log "add User.find(#{user_new[:id]})"
|
||||||
user = User.new(user_new)
|
user = User.new(user_new)
|
||||||
user.id = user_new[:id]
|
user.id = user_new[:id]
|
||||||
user.save
|
user.save
|
||||||
|
@ -1109,11 +1112,11 @@ module Import::OTRS2
|
||||||
if user_old.role_ids == user_new[:role_ids]
|
if user_old.role_ids == user_new[:role_ids]
|
||||||
user_new.delete( :role_ids )
|
user_new.delete( :role_ids )
|
||||||
end
|
end
|
||||||
puts "update User.find(#{user_old[:id]})"
|
log "update User.find(#{user_old[:id]})"
|
||||||
user_old.update_attributes(user_new)
|
user_old.update_attributes(user_new)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
puts "add User.find(#{user_new[:id]})"
|
log "add User.find(#{user_new[:id]})"
|
||||||
user = User.new(user_new)
|
user = User.new(user_new)
|
||||||
user.save
|
user.save
|
||||||
end
|
end
|
||||||
|
@ -1227,6 +1230,14 @@ module Import::OTRS2
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# log
|
||||||
|
|
||||||
|
def self.log(message)
|
||||||
|
thread_no = Thread.current[:thread_no] || '-'
|
||||||
|
puts "#{Time.new.to_s}/thread##{thread_no}: #{message}"
|
||||||
|
end
|
||||||
|
|
||||||
# set translate valid ids to active = true|false
|
# set translate valid ids to active = true|false
|
||||||
|
|
||||||
def self._set_valid(record)
|
def self._set_valid(record)
|
||||||
|
|
Loading…
Reference in a new issue