analiz:log("--serv.lua-")

-- ----------------------------------------------------------------------------
  function create_engine()
    local engine = {}
    engine.processing={count=0}


    -- --------------------------
    function engine:processing_task(task)

     if IsHeartBeatTask(task) then
        task.heartbeat=true
        analiz:set("tmp",task.file)
        analiz:execute("LEX_PUBLISH_FINISH_TASK $(tmp) 0")
        return "0"
      end

      analiz:set("tmp",task.file)
      analiz:execute("echo task=$(tmp)")
      if engine.processing.count==1 then
        analiz:execute("STOPWATCH tmp1 timer START")
      else 
        analiz:execute("STOPWATCH tmp1 timer BEGIN")
      end
      analiz:execute("LEX_FAST_COMPARE_ENGINE ret_code $(server_name) $(tmp) $(is_delete)");
--      analiz:set("ret_code","0") -- for debug
      analiz:execute("STOPWATCH tmp1 timer STOP")
      analiz:execute("STOPWATCH tmp2 timer COUNT")
      analiz:execute("TO_INT tmp2 $(tmp2)")
      analiz:execute("CONVERT_NUMBER_FORMAT tmp1 tmp1 GENERAL2")
      local bid=""
      if not(task.bid==nil) then
        bid=",bid:"..bid
      end
      analiz:execute("SHOW_TITLE $(caption_str):count:$(tmp2),avg:$(tmp1)"..bid)
      analiz:execute("echo retcode=$(ret_code)")

      engine.processing.count = engine.processing.count+1

      local ret = analiz:get("ret_code")
      return ret
    end  
  -- --------------------------
    return engine
  end
  
  
function CheckPattern(a,b)
  local r = string.find(a,b)
  return not (r==nil)
end




  -- ------------------------------------------------------------------------------
  -- ------------------------------------------------------------------------------


  function create_ctx()
    local t = analiz:get("type")
    analiz:log("create_ctx.type:"..t) 
    local tag = analiz:get("family_id")
    if tag=="management" then
       if t=="BATCH" then
         return create_management_batch_ctx()
--          return create_management_ctx()
       else
          return create_management_ctx()
       end
    end
    return create_std_ctx()  
  end

  -- ------------------------------------------------------------------------------
  -- ------------------------------------------------------------------------------


  function create_ctx2()
    return create_std_ctx()  
  end
  -- ------------------------------------------------------------------------------
  -- ------------------------------------------------------------------------------
  function create_ctx1()

    local tag = analiz:get("work_dir")
    if CheckPattern(tag,"single") then
      return create_single_ctx()
    end  
    
    return create_std_ctx()  
  end  
  -- ------------------------------------------------------------------------------
  function restart_service(msg)
   l("restart_service.l:"..msg)
   l("echo !!!!restart!!!")
   l("calc_param pskill_exe $(_ANALIZ_BIN)\\pskill.exe")
   l("GET_SHORT_PATH pskill_exe $(pskill_exe)")
   l("GET_CURRENT_PROCESS_ID txm_pid")
   l("echo execute $(pskill_exe) $(txm_pid)")
   l("execute $(pskill_exe) $(txm_pid)")
   l("_abort 1")
  end
  -- ------------------------------------------------------------------------------
  function GetCurrentListID(ctx)
   local function get_(ctx)
--    l("get.1")
       analiz:set("tmp","")
       analiz:set("tmp_r","")
       analiz:set("tmp_m","")
       analiz:set("tmp_s","select max(ID) as C from CurrentListTable")
--    l("get.1.1")
       analiz:execute("ANALIZ_SAFE_EXECUTE tmp_r tmp_e  SET_PARAM_FROM_SQL tmp "..ctx.pwd_plib.." C $(tmp_s)")
--    l("get.2")
      local ret = {id=analiz:get("tmp"),emessage=analiz:get("tmp_m")}
      return ret
  end
  local i=0
  while true do
   local r = get_(ctx)
   if r.emessage=="" then
     return r.id
   end
   l("count:"..i..",emessage:"..r.emessage)
   i=i+1
   if i>600 then
     restart_service("failed:"..r.emessage)
     return -1
   end
   analiz:execute("SLEEP sec 1")

  end
 return -1
-- ANALIZ_SAFE_EXECUTE
--   analiz:set("tmp","")
--   analiz:load_from_sql("tmp","C",ctx.pwd_plib,"select max(ID) as C from CurrentListTable")
--   return analiz:get("tmp")
  end

  -- ------------------------------------------------------------------------------
 function IsHeartBeatTask(task)
      local hb= task.heartbeat -- GetParamFromTask(task,"heartbeat")
      if hb=="yes" then
        return true
      end 
      hb=  GetParamFromTask(task,"heartbeat")
      analiz:log("isheartbeat:"..hb)
      return hb=="yes" 
 end

  -- ------------------------------------------------------------------------------

 function CreateHeartBeatTask()
   return null
--[[    analiz:execute("calc_param tmp $(temp)\\HeartBeat_$(GUID_FOR_LOG).task")
    analiz:execute("CREATE_OR_CLEAR_STRING_MAP HeartBeat_map")
    analiz:execute("echo LEX_CREATE_TASK $(tmp) TXT HeartBeat_map  $(template_hb_str)")
    analiz:execute("LEX_CREATE_TASK $(tmp) TXT HeartBeat_map  $(template_hb_str)")

    local task={}
    task.file = analiz:get("tmp")
    task.heartbeat="yes"
    return task
--]]
 end

  -- ------------------------------------------------------------------------------
 function GetParamFromTask(task,name)
-- avral fix.start
   if task==null then return "" end
   if task.type==null then return "" end
-- avral fix.end

     if not(task.type == "file") then
       analiz:execute("sleep min 5")
       a("failed task type:"..task.type)
     end
     analiz:set("tmp",task.file)
     analiz:execute("LOAD_SOURCE_DATA tmp_data N $(tmp)")
     analiz:execute("CREATE_OR_CLEAR_STRING_MAP tmp_map")
     analiz:execute("STRING_MAP_LOAD_FROM_TABLE tmp_map tmp_data NAME	VALUE 0")
     local m = analiz:map_get("tmp_map")
     return m:get(name)
   end
  -- ------------------------------------------------------------------------------
 function GetScriptTable()
  analiz:set("tmp_plib","Sanction lists\\Std sanction list")
   analiz:execute("EXTRACT_FROM_PLIB_TABLENAME tmp PLIB $(tmp_plib) $(empty) $(empty)")
   analiz:execute("echo plib table:$(tmp)")
--   analiz:execute("abort 1")
   return analiz:get("tmp")

 end
  -- ------------------------------------------------------------------------------

