Skip to content

Commit

Permalink
* Final fix for #72
Browse files Browse the repository at this point in the history
  • Loading branch information
tmptrash committed Oct 20, 2016
1 parent dff68fd commit 7bb25ec
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 33 deletions.
18 changes: 13 additions & 5 deletions src/AppManager.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,24 @@ const ARG_RECOVER = "recover"
# runs in a common mode.
#
function main()
Helper.info("Starting jevo...")

local args::Dict{String, String} = CommandLine.create()
local man::ManagerTypes.ManagerData = Manager.create()

local exitCode::Int
#
# According to returning value (exitCode) AppSatellite will run
# this app again (1) or just quit (0).
#
if CommandLine.has(args, ARG_RECOVER)
Manager.recover(man)
return Manager.run(man, true)
exitCode = Int(!Manager.run(man, true)) # 1 - error, 0 - okay
else
Helper.info("Running from scratch...")
exitCode = Int(!Manager.run(man)) # 1 - error, 0 - okay
end

Helper.info("Running from scratch...")
Manager.run(man)
Helper.info("Quit jevo...")
exit(exitCode)
end
#
# Application entry point
Expand Down
4 changes: 2 additions & 2 deletions src/AppSatellite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ end
function _removeBrokenBackup(stamp::Float64, cfg::Config.ConfigData)
local last::String

println("_removeBrokenBackup(), time: ", time(), ", stamp: ", stamp, ", period: ", cfg.BACKUP_PERIOD)
println("last file: ", Backup.lastFile())
#println("_removeBrokenBackup(), time: ", time(), ", stamp: ", stamp, ", period: ", cfg.BACKUP_PERIOD)
#println("last file: ", Backup.lastFile())
if (time() - stamp) < cfg.BACKUP_PERIOD
try
if (last = Backup.lastFile()) != ""
Expand Down
6 changes: 6 additions & 0 deletions src/Config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ module Config
#
WORLD_SCALE::Int
#
# Amount of backups, after which application will be resetted.
# Process will be stopped and runned again.
#
WORLD_RESET_AFTER_BACKUPS::Int
#
# Period of making automatic backup of application. In seconds
#
BACKUP_PERIOD::Float64
Expand Down Expand Up @@ -338,6 +343,7 @@ module Config
0.3, # WORLD_MIN_ENERGY_PERCENT
500, # WORLD_MIN_ENERGY_CHECK_PERIOD
3, # WORLD_SCALE
3, # WORLD_RESET_AFTER_BACKUPS
60.0, # BACKUP_PERIOD
10, # BACKUP_AMOUNT
650, # STAT_WIDTH
Expand Down
60 changes: 35 additions & 25 deletions src/manager/Manager.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ module Manager
# @return {Bool} run status
#
function run(man::ManagerTypes.ManagerData, recover::Bool = false)
local counter ::Int = 1 # must be started from 1!
local ips ::Int = 0
local istamp ::Float64 = time()
local bstamp ::Float64 = istamp
local ystamp ::Float64 = istamp
local cons ::ManagerTypes.Connections = man.cons
local tasks ::Array{ManagerTypes.OrganismTask, 1} = man.tasks
local cfg ::Config.ConfigData = man.cfg
local needYield::Bool = false
local counter ::Int = 1 # must be started from 1!
local ips ::Int = 0
local istamp ::Float64 = time()
local bstamp ::Float64 = istamp
local ystamp ::Float64 = istamp
local cons ::ManagerTypes.Connections = man.cons
local tasks ::Array{ManagerTypes.OrganismTask, 1} = man.tasks
local cfg ::Config.ConfigData = man.cfg
local needYield ::Bool = false
local backups ::Int = 0
local stayInLoop::Bool = true
@if_profile local i::Int = 0

try
Expand All @@ -104,7 +106,7 @@ module Manager
# in this call. If we are in recover mode, then this step should
# be skipped.
#
if recover === false
if !recover
setRandomEnergy(man, cfg.WORLD_START_ENERGY_BLOCKS, cfg.WORLD_START_ENERGY_AMOUNT)
createOrganisms(man)
end
Expand Down Expand Up @@ -132,10 +134,6 @@ module Manager
#
ips, istamp = _updateIps(man, ips, stamp, istamp)
#
# Here we make auto-backup of application if there is a time
#
bstamp = _updateBackup(man, cfg, stamp, bstamp)
#
# This call switches between all non blocking asynchronous
# functions (see @async macro). For example, it handles all
# input connections for current server. But we don't need to
Expand All @@ -148,6 +146,11 @@ module Manager
# TODO: created by @async() macro.
ystamp, needYield = _updateTasks(man, stamp, ystamp, needYield)
#
# Here we make auto-backup of application if there is a time
#
bstamp, backups, stayInLoop = _updateBackup(man, stamp, bstamp, backups)
if !stayInLoop return false end
#
# It's important to skip this function if CodeConfig.showStatus
# flag is set to false. See CodeConfig::showStatus for details.
#
Expand All @@ -162,8 +165,10 @@ module Manager
@if_debug showerror(STDOUT, e, catch_backtrace())
return false
end

true
#
# true means, that everything is okay, false - something went wrong
#
stayInLoop
end
#
# This is how we stop the task. Stop means run last yieldto()
Expand Down Expand Up @@ -207,8 +212,7 @@ module Manager
local sock::Base.TCPSocket
local dataIndex::UInt8
local localIps::Int
local i::Int
# TODO: 5.0 seconds should be get from config
# TODO: 1.0 seconds should be get from config
if ts >= 1.0
localIps = trunc(Int, ips / ts)
dataIndex = UInt8(FastApi.API_UINT64)
Expand All @@ -225,19 +229,25 @@ module Manager
ips + 1, istamp
end
#
# Checks if it's a time to make application backup
# Checks if it's a time to make application backup. It also checks if
# system reaches specified amount of backups for app reset.
# @param man Manager data type
# @param cfg Global configuration type
# @param stamp Current UNIX timestamp
# @param bstamp Backup last UNIX time stamp value
# @param backups Amount of backups from previous app reset
# @return {(Float64, Bool)} Updated time stamp and main loop quit flag
#
function _updateBackup(man::ManagerTypes.ManagerData, cfg::Config.ConfigData, stamp::Float64, bstamp::Float64)
if stamp - bstamp >= cfg.BACKUP_PERIOD
if length(man.tasks) > 0 backup(man) end
return stamp
function _updateBackup(man::ManagerTypes.ManagerData, stamp::Float64, bstamp::Float64, backups::Int)
if stamp - bstamp >= man.cfg.BACKUP_PERIOD
if length(man.tasks) > 0
backup(man)
backups += 1
if backups === man.cfg.WORLD_RESET_AFTER_BACKUPS return (stamp, backups, false) end
end
return (stamp, backups, true)
end

bstamp
(bstamp, backups, true)
end
# TODO: describe yield() call logic
# Checks if active servers have bytes to read. It means, that we have to call
Expand Down
2 changes: 1 addition & 1 deletion src/net/Server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ module Server
try
local server::Base.TCPServer = listen(host, port)
con = ServerConnection(tasks, socks, server, obs, host, port, fast)
Helper.info(string("Server created: ", host, ":", port))
Helper.info(string(fast ? "Fast" : "Slow", " Server created: ", con.host, ":", con.port))
return con
catch e
Helper.warn("Server.create(): $e")
Expand Down

0 comments on commit 7bb25ec

Please sign in to comment.