Skip to content

Commit

Permalink
Many improvements to example scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
tailhook committed Apr 6, 2016
1 parent 0471a40 commit 65133e5
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 24 deletions.
10 changes: 10 additions & 0 deletions example-configs/pyapp/runtime/pyapp/v3/processes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
daemons:
web:
image: web-v1.0.0
config: /configs/web.yaml
celery:
image: celery-v1.0.0
config: /configs/celery.yaml
celery-beat:
image: celery-v1.0.0
config: /configs/celery-beat.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
web: 10
celery: 20
celery-beat: 1
32 changes: 32 additions & 0 deletions example-configs/pyapp/scheduler/v1/debug.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
inspect = require "inspect"

function debugger()
local x = {text=""}
function x.object(self, title, data)
self.text = self.text
.. string.format('----- %s ----\n', title)
.. inspect(data)
.. "\n"
end
return x
end

function wrap_scheduler(real_scheduler)
return function(state)
local dbg = debugger()
_G.print = function(...) dbg.print(...) end
flag, value = pcall(_scheduler, state, dbg)
_G.print = nil
if flag then
return value, dbg.text
else
text = dbg.text .. string.format("\nError: %s", value)
return nil, text
end
end
end

return {
debugger=debugger,
wrap_scheduler=wrap_scheduler,
}
20 changes: 20 additions & 0 deletions example-configs/pyapp/scheduler/v1/func.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function map(func, array)
local new_array = {}
for i,v in ipairs(array) do
new_array[i] = func(v)
end
return new_array
end

function map_reverse(func, array)
local new_array = {}
for i=#array,1,-1 do
new_array[#new_array+1] = func(array[i])
end
return new_array
end

return {
map=map,
map_reverse=map_reverse,
}
49 changes: 25 additions & 24 deletions example-configs/pyapp/scheduler/v1/main.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
JSON = require "JSON"
inspect = require "inspect"
debug = require "debug"
version = require "version"
func = require "func"

function cycle(items)
local i = 0
Expand All @@ -11,32 +13,25 @@ function cycle(items)
end
end

function debugger()
local x = {text=""}
function x.object(self, title, data)
self.text = self.text
.. string.format('----- %s ----\n', title)
.. inspect(data)
.. "\n"
end
return x
end

function scheduler(state)
local dbg = debugger()
flag, value = pcall(_scheduler, state, dbg)
if flag then
return value, dbg.text
else
text = dbg.text .. string.format("\nError: %s", value)
return nil, text
end
end

function _scheduler(state, debugger)
debugger:object("INPUT", state)
local template_version = "v1"
local runtime_version = "example-1" -- TODO(tailhook)

-- Naive algorithm: get the biggest version in every parent schedule
-- TODO(tailhook) better idea it to get the one with the latest timestamp
local versions = func.map(
function(s) return s.role_metadata.pyapp.info.version end,
state.parents)
-- If there was no previous schedules, use the latest existing config
if #versions == 0 then
for ver, _ in pairs(state.roles.pyapp.runtime) do
versions[#versions+1] = ver
end
end
-- Sort and get the latest/biggest one
table.sort(versions)
local runtime_version = versions[#versions]

local runtime = state.roles.pyapp.runtime[runtime_version]
local req = runtime.required_processes
local node_list = {"n1", "n2", "n3"}
Expand Down Expand Up @@ -87,9 +82,15 @@ function _scheduler(state, debugger)
info={
version=runtime_version,
},
buttons=func.map_reverse(function (v)
return {id="switch_to_" .. v,
title="Switch to " .. v}
end, versions),
},
},
nodes=nodes,
}
return JSON:encode(result)
end

scheduler = debug.wrap_scheduler(_scheduler)
54 changes: 54 additions & 0 deletions example-configs/pyapp/scheduler/v1/version.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
string = require "string"

function compare(a, b)
if a:sub(1, 1) == 'v' then a = a:sub(2) end
if b:sub(1, 1) == 'v' then b = b:sub(2) end
local aiter = string.gmatch(a, "%w+")
local biter = string.gmatch(b, "%w+")
while true do
local aitem = aiter()
local bitem = biter()
if aitem == nil then return false end
if bitem == nil then return true end
if not (aitem == bitem) then
if string.match("%d+", aitem) then
if string.match("%d+", bitem) then
local anum = tonumber(aitem)
local bnum = tonumber(bitem)
return anum < bnum
else -- numbers are always less than letters
return false
end
else
if string.match("%d+", bitem) then
-- numbers are always less than letters
return true
else
return aitem < bitem
end
end
end
end
end


----- SELF TESTS -----
assert(compare("v1", "v2"))
assert(not compare("v2", "v1"))
assert(compare("v1.1", "v2.1"))
assert(compare("v2.1", "v2.3"))
assert(not compare("v2.3", "v2.1"))

-- false for equal versions
assert(not compare("v1", "v1"))
assert(not compare("v2", "v2"))
assert(not compare("v2.3.4", "v2.3.4"))

local _sorttable = {"v1.1.0", "v1.0", "v3.4.6", "v1", "v2.3"}
table.sort(_sorttable)
assert(_sorttable[1] == "v1")
assert(_sorttable[2] == "v1.0")
assert(_sorttable[3] == "v1.1.0")
assert(_sorttable[4] == "v2.3")
assert(_sorttable[5] == "v3.4.6")
_sorttable = nil

0 comments on commit 65133e5

Please sign in to comment.