module GiJob module Jobs module Concerns module JobConstructor extend ActiveSupport::Concern included do # begin included #### job作成 def self.create_job(**args) self.create_job_with(args) end end # end included class_methods do # begin class_methods def create_job_with( owner: nil, parameter: {}, gi_job_transaction_params: {}, option: { create_only: false, uploaded_file: nil, } ) # transactionレコード作成 gi_job_transaction = self.create_gi_job_transaction( owner: owner, parameter: parameter, gi_job_transaction_params: gi_job_transaction_params, option: option ) if option[:create_only].present? # 作成のみの場合はstartしない return {gi_job_transaction: gi_job_transaction} else self.start_job_with(gi_job_transaction: gi_job_transaction) end end def start_job_with(gi_job_transaction:) # 前処理 # GiJob.logger.ap({message: "pre_process"}) result = job_process_call(process_name: "前処理", gi_job_transaction: gi_job_transaction) do |**args| self.job_pre_process_impl(args) end if result.present? && result[:error].present? # 前処理エラー return {gi_job_transaction: gi_job_transaction}.merge(result) end # 本処理 # GiJob.logger.ap({message: "call_process"}) result = job_process_call(process_name: "呼出処理", gi_job_transaction: gi_job_transaction) do |**args| self.job_call_process_impl(args) end return {gi_job_transaction: gi_job_transaction}.merge(result) end def create_gi_job_transaction( owner: nil, parameter: {}, gi_job_transaction_params: {}, option: {} ) # GiJob.logger.ap({create_job_transaction: {owner: owner, parameter: parameter, gi_job_transaction_params: gi_job_transaction_params, option: option,}}) tmp_parameter = parameter.deep_dup # jobレコードを作成 merged_params = ({ owner: owner, name: "#{self.name.underscore}", parameter: parameter.present? ? parameter.deep_symbolize_keys! : {}, }).merge(gi_job_transaction_params) gi_job_transaction = GiJobTransaction.new(merged_params) gi_job_transaction.save! # GiJob.logger.ap(created: {gi_job_transaction: gi_job_transaction}) if option.has_key?(:uploaded_file) && option[:uploaded_file].present? # uploaded_fileを保存 uploaded_file = option.delete(:uploaded_file) gi_job_transaction.create_file!(uploaded_file: uploaded_file) end # # その他のadditional_infoを保存 # gi_job_transaction.update!(additional_info: tmp_additional_info) # # # master_jobを紐づけ # self.relation_master_schedule_job!(gi_job_transaction, master_schedule_job) gi_job_transaction end def job_process_call(process_name: nil, **args, &func) # GiJob.logger.ap(process_name: process_name, args: args, func: func) return_object = {error: nil, messages: [], result: nil} begin return_object = func.call(args) rescue => e GiJob.logger.fatal(e.message) GiJob.logger.fatal(e.backtrace.join("\n")) message = "#{process_name.present? ? "#{process_name}で" : ""}エラーが発生しました。エラーコード: #{e}" return_object = {error: e, messages: [message], result: nil} end if return_object.present? && return_object[:error].present? gi_job_transaction = args[:gi_job_transaction] gi_job_transaction.status_error! gi_job_transaction.append_log_error( division: "#{self.name}", description: "#{return_object[:messages].present? ? [return_object[:messages]].flatten.join(" ") : ""}" ) end return_object end def job_pre_process_impl(**args) # 前処理が必要であれば派生クラスで拡張する事 # この処理は、同期/非同期に関わらず、同期処理で行われる。 # また、ジョブ再開時には再度呼び出しは行われない。 # エラーの場合は {error: true} とする事 {error: nil, messages: [], result: nil} end def job_call_process_impl(**args) # 呼び出し処理の変更必要であれば派生クラスで拡張する事 # エラーの場合は {error: true} とする事 return self.job_call_process_default(args[:gi_job_transaction]) end def job_call_process_default(gi_job_transaction) # GiJob.logger.ap(job_call_process_default: {gi_job_transaction: gi_job_transaction}) # TODO 再開時にsuspendをnoneに変更する JobUtils.check_job_suspended(gi_job_transaction) application_job = nil if gi_job_transaction.parameter[:_perform_now].present? # 即時実行指定 application_job = perform_now({gi_job_transaction: gi_job_transaction}) else application_job = perform_later({gi_job_transaction: gi_job_transaction}) end {error: nil, messages: [], result: {application_job: application_job}} end def job_post_process_impl(**args) # 後処理必要であれば派生クラスで拡張する事 # この処理は、同期/非同期にかかわらず、ジョブ終了後に呼び出される # エラーの場合は {error: true} とする事 return self.job_post_process_default(args[:gi_job_transaction]) end def job_post_process_default(gi_job_transaction) if gi_job_transaction.parameter[:_one_shot].present? # 単発ジョブ指定の場合は過去のjobを削除する GiJobTransaction.delete_old_for_one_shot(gi_job_transaction) end {error: nil, messages: [], result: nil} end def aaa() end end # end class_methods end # end GiJobable end end end