local ss = analiz.libs.serv
local finn_serv = analiz.mix.finnova.serv
local log = analiz.libs.logserv
log.init()
SetSecLevel()

local uniq_id = ss.b("$(service_family):$(UNIQUE_IDENTIFIER)")
seclevel = CreateSecLevel("lex")

if analiz:get("family_id") ~= "management" then
    send_error_custom_logic = send_error_custom_logic_for_worker
end

full_service_uniq_id = ss.b(
                           "$(COMPUTERNAME)_$(service_family)_$(UNIQUE_IDENTIFIER)")
full_service_uniq_id2 = GetProsperoHostName() ..
                            ss.b(".$(service_family).$(UNIQUE_IDENTIFIER)")

record_status_obj = CreateRecordStatusObject({name = uniq_id, lazy = true})
rstatus = record_status_obj
-- record_status_obj:disable(ss.g("TYPE2")=="Kundenstamm")
do
    analiz:log("migration 1")
    local migration = create_migration_engine()
    migration_check(migration)
end

do
    analiz:log("migration 2(batch.multi)")
    local migration = create_migration_engine_batch()
    migration_check(migration)
end

ss.l("queue init...")

local engine = CreateQueueEngine()

engine:init()

local uname = analiz:get("UNIQUE_IDENTIFIER")
local family_id = analiz:get("family_id")
local family_count = analiz:get("family_count")
local is_managment = "0"
if analiz:get("family_id") == "management" then is_managment = "1" end

uname = ss.ReplaceValue(uname, "_" .. family_id .. "_" .. family_count, "")
ss.s("FAMILY_NAME_", uname)

local function GetProfileName(servicename, paramname, def_value)
    return finn_serv.GetNodeProfileName(servicename, paramname, def_value,
                                        is_managment)
end
local servicename = uname .. "_" .. family_count

local profilename_default = "default_profile"
if uname:find("kd") == 1 then profilename_default = "KundestammDelta" end
local profilename = GetProfileName(servicename, "Profile", profilename_default)

if ss.g("TYPE2") == "Kundenstamm" then
    lex_task_template_config.name = "Kundenstamm"
end
lex_task_template_config.profilename = profilename

ss.l("lex.setting for :" .. uname)
ss.l("name:" .. lex_task_template_config.name)
ss.l("profilename:" .. lex_task_template_config.profilename)
--  ss.a(1)

local list_ver_id_ = GetCurrentListID({pwd_plib = "PLIB"})

local queue_param = {
    typei = analiz:get("type"),
    name = uname,
    i = family_id,
    count = family_count,
    queue_engine = engine
}

--   ss.tprint(queue_param)
local ora = oracle_engine:Create(queue_param)
ctx = create_ctx(ora)
--   analiz:execute("abort 1")

analiz:log("ctx:init()!!!")

local function init_lex_lazy(ctx)

    do
        local isok, emessage = start_lex_engine(queue_param,
                                                {list_ver_id = list_ver_id_})
        if not isok then
            ss.l("failed.lex:" .. emessage)
            ss.l("sleep 15 sec")
            ss.e("sleep sec 15")
            analiz:execute("_ABORT 1") -- restart
        end
    end
end

-- check start all link services
if ss.IsService() then
    local count = math.floor(analiz:tonumber(analiz:get("family_count")))
    local service_prefix = ss.g("service_prefix")
    local types = ss.g("TYPE")
    ss.l("srv.service_prefix:" .. service_prefix)

    local function get(types, i, count)
        -- return   types.."_"..uname.."_"..i.."_"..count 
        return uname .. "_" .. i .. "_" .. count
    end
    local function start_service(service_name)
        ss.e("START_WIN32_SERVICE . " .. service_name .. " 0")
    end

    for i = 0, count - 1 do
        local gg = get(types, i, count)
        local service_name = service_prefix .. gg
        --   ss.l("service_name:"..service_prefix.."]["..gg)
        start_service(service_name)
    end
    local service_name = service_prefix .. get(types, "management", count)
    start_service(service_name)

end

--   analiz:execute("abort 1")
ctx:init()

while true do
    --  ss.l("z1")
    --  ss.tprint(deadline.time.WAIT)
    deadline:DeadLineLazy("wait", deadline.time.WAIT)
    if not main_loop(ctx) then
        analiz:log("main restart")
        break
    end
    record_status_obj:commit({lazy = true, lazyKoef = 5})
    --      analiz:log("ctx:step")
    if ctx:IsRestart(null, true) then
        analiz:log("command restart")
        break
    end
    init_lex_lazy(ctx)
    if analiz:IsStop() then
        analiz:log("user break")
        break
    end

end

record_status_obj:close()

engine:destroy()

--  ctx.ora:free()
--  oracle_engine:Cleanup()



