local oMQ = analiz.libs.oracleMQ
local ss = analiz.libs.serv


local pwd = ss.CreatePwdServ()

local tick= CreateTimeServ()

--local membuffer = ss.CreateMemItem(10*1024)


 function go_pskill()
ss.l(debug.traceback())
  ss.l("go_pskill")
  ss.e("calc_param pskill_exe $(_ANALIZ_BIN)\\pskill.exe")
  ss.e("GET_SHORT_PATH pskill_exe $(pskill_exe)")
  ss.e("GET_CURRENT_PROCESS_ID txm_pid")
  ss.e("echo execute $(pskill_exe) $(txm_pid)")
  ss.e("execute $(pskill_exe) $(txm_pid)")
  ss.e("_abort 1")
end



local wait_count=0
local count_ok = 0

list_ver_id = GetCurrentListID({pwd_plib="PLIB"})

function main_loop(deq,enq)
  local msg,msgid,corrid = deq:get()
  if msg==nil then
     wait_count=wait_count+1
    return
  end

  local msgid  = nil
  wait_count=0
  ss.l("--------------------------------------------------------------------"..count_ok)
  local val1 = msg
  ss.l("load.rec:")
  ss.l("msgid:"..ss.def(msgid,"<nil>"))
  ss.l("corrid:"..ss.def(corrid,"<nil>"))
  ss.tprint(val1)

  if val1.firstname=="{C4BC6514-05DF-4C37-9ED4-78C25FAD2874}" or val1.lastname =="{C4BC6514-05DF-4C37-9ED4-78C25FAD2874}" then
   ss.l("check list change")
    list_ver_id = GetCurrentListID({pwd_plib="PLIB"})
   ss.l("list_ver_id:"..list_ver_id)
  end

  val1.list_ver_id =list_ver_id

  enq:send(val1)-- add corrid
  enq:commit()

  ss.l("-----------------------------------"..count_ok)
  count_ok=count_ok+1

end
-- -------------------------init
ss.l("OracleMq init...")
oracle_engine:Initialize()

local engine = oracle_engine:Create({})

ss.l("DequeueCreate..")
local   deq = engine:GetInQueue(finn_serv.GetFullQName(global_oracle_setting.internal.input_old_queue))
deq.deq:settimeout(10)


local   enq = deq:GetOutObj(global_oracle_setting.internal.input_queue)



ss.l("list_ver_id:"..list_ver_id)


local tt = tick.now()
  ss.l("Start")
while true do
  if analiz:IsStop() then
    break
  end

  main_loop(deq,enq)

  if tick.to_sec(tick.now()-tt)>10*50 then
    ss.l("wait_count:"..wait_count..",count_ok:"..count_ok)
    tt=tick.now()
  end
  local err = oMQ.serv.GetLastError()
  if err~="" then
    ss.l("oracle.error:"..err)
    ss.l("restart after cleep")
    ss.e("sleep sec 15")
    ss.l("abort")
    go_pskill()
    ss.a("1")
  end


end




deq:free()
enq:free()
engine:free()
oMQ.Cleanup()



