From f2d0eca3da4d922b8ac4f741af30cacd0f39acd4 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 13 Jan 2016 20:45:51 +0100 Subject: [PATCH] Improved background job for import. --- .../app/controllers/import_otrs.coffee | 41 +++++----- .../javascripts/app/views/import/otrs.jst.eco | 2 + app/controllers/import_otrs_controller.rb | 21 ++--- app/models/setting.rb | 16 +++- lib/import/otrs.rb | 77 +++++++++++++++++-- 5 files changed, 115 insertions(+), 42 deletions(-) diff --git a/app/assets/javascripts/app/controllers/import_otrs.coffee b/app/assets/javascripts/app/controllers/import_otrs.coffee index 887737c97..da6e8f92c 100644 --- a/app/assets/javascripts/app/controllers/import_otrs.coffee +++ b/app/assets/javascripts/app/controllers/import_otrs.coffee @@ -18,12 +18,6 @@ class Index extends App.ControllerContent @fetch() - @bind('import:finished', => - console.log('import:finished') - @Config.set('system_init_done', true) - @navigate '#' - ) - fetch: -> # get data @@ -95,7 +89,7 @@ class Index extends App.ControllerContent @nextStartMigration.addClass('hide') ) - @delay( callback, 700, 'import_otrs_url' ) + @delay(callback, 700, 'import_otrs_url') startMigration: (e) => e.preventDefault() @@ -106,10 +100,8 @@ class Index extends App.ControllerContent url: @apiPath + '/import/otrs/import_start', processData: true, success: (data, status, xhr) => - - # validate form if data.result is 'ok' - @delay( @updateMigration, 3000 ) + @delay(@updateMigration, 3000) ) @@ -127,17 +119,24 @@ class Index extends App.ControllerContent @navigate '#' return - for key, item of data.data - element = @$('.js-' + key.toLowerCase() ) - element.find('.js-done').text(item.done) - element.find('.js-total').text(item.total) - element.find('progress').attr('max', item.total ) - element.find('progress').attr('value', item.done ) - if item.total <= item.done - element.addClass('is-done') - else - element.removeClass('is-done') - @delay( @updateMigration, 5000 ) + if data.result is 'error' + @$('.js-error').removeClass('hide') + @$('.js-error').html(App.i18n.translateContent(data.message)) + else + @$('.js-error').addClass('hide') + + if data.result is 'in_progress' + for key, item of data.data + element = @$('.js-' + key.toLowerCase() ) + element.find('.js-done').text(item.done) + element.find('.js-total').text(item.total) + element.find('progress').attr('max', item.total ) + element.find('progress').attr('value', item.done ) + if item.total <= item.done + element.addClass('is-done') + else + element.removeClass('is-done') + @delay(@updateMigration, 6500) ) App.Config.set( 'import/otrs', Index, 'Routes' ) diff --git a/app/assets/javascripts/app/views/import/otrs.jst.eco b/app/assets/javascripts/app/views/import/otrs.jst.eco index 97def7444..caa7222c7 100644 --- a/app/assets/javascripts/app/views/import/otrs.jst.eco +++ b/app/assets/javascripts/app/views/import/otrs.jst.eco @@ -41,6 +41,8 @@

<%- @T('OTRS Migration') %>

+ +
diff --git a/app/controllers/import_otrs_controller.rb b/app/controllers/import_otrs_controller.rb index 24a174659..b92625a8d 100644 --- a/app/controllers/import_otrs_controller.rb +++ b/app/controllers/import_otrs_controller.rb @@ -80,7 +80,6 @@ class ImportOtrsController < ApplicationController def import_start return if setup_done_response - Setting.set('import_mode', true) welcome = Import::OTRS.connection_test if !welcome @@ -92,7 +91,10 @@ class ImportOtrsController < ApplicationController end # start migration - Import::OTRS.delay.start + Import::OTRS.delay.start_bg( + import_otrs_endpoint: Setting.get('import_otrs_endpoint'), + import_otrs_endpoint_key: Setting.get('import_otrs_endpoint_key'), + ) render json: { result: 'ok', @@ -100,18 +102,11 @@ class ImportOtrsController < ApplicationController end def import_status - if !Setting.get('import_mode') - render json: { - setup_done: true, - } - return + result = Import::OTRS.status_bg + if result[:setup_done] == true + Setting.reload end - - state = Import::OTRS.current_state - render json: { - data: state, - result: 'in_progress', - } + render json: result end private diff --git a/app/models/setting.rb b/app/models/setting.rb index abd47367c..495ba1713 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -77,13 +77,25 @@ reset config setting to default @@current[:settings_config][name] end +=begin + +reload config settings + + Setting.reload + +=end + + def self.reload + load(true) + end + private # load values and cache them - def self.load + def self.load(force = false) # check if config is already generated - if @@current[:settings_config] + if !force && @@current[:settings_config] return false if cache_valid? end diff --git a/lib/import/otrs.rb b/lib/import/otrs.rb index 0ec12c2ba..50b084d26 100644 --- a/lib/import/otrs.rb +++ b/lib/import/otrs.rb @@ -214,7 +214,7 @@ module Import::OTRS =begin - get object statistic from server ans save it in cache + get object statistic from remote server ans save it in cache result = statistic('Subaction=List') @@ -409,15 +409,80 @@ module Import::OTRS threads[thread].join } + true + end + +=begin + start import in background + + Import::OTRS.start_bg( + import_otrs_endpoint: 'http://vz599.demo.znuny.com/otrs/public.pl?Action=ZammadMigrator +', + import_otrs_endpoint_key: '01234567899876543210', + ) +=end + + def self.start_bg(params) + Setting.set('import_mode', 'true') + Setting.set('import_backend', 'otrs') + Setting.set('import_otrs_endpoint', params[:import_otrs_endpoint]) + Setting.set('import_otrs_endpoint_key', params[:import_otrs_endpoint_key]) + + status_update_thread = Thread.new { + loop do + result = { + data: current_state, + result: 'in_progress', + } + Cache.write('import:state', result, expires_in: 10.minutes) + sleep 8 + end + } + + sleep 5 + + begin + import_thread = Thread.new { + Import::OTRS.start + } + rescue => e + status_update_thread.exit + status_update_thread.join + Rails.logger.error e.message + Rails.logger.error e.backtrace.inspect + result = { + message: e.message, + result: 'error', + } + Cache.write('import:state', result, expires_in: 10.hours) + end + import_thread.join + status_update_thread.exit + status_update_thread.join + Setting.set('system_init_done', true) Setting.set('import_mode', false) + end - # broadcast import finish - Sessions.broadcast( - event: 'import:finished', - ) +=begin - true + get import state from background process + + result = Import::OTRS.status_bg + +=end + + def self.status_bg + if !Setting.get('import_mode') + return { + setup_done: true, + } + end + state = Cache.get('import:state') + return state if state + { + message: 'not running', + } end def self.diff_worker