Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>
beterrabad: use log & fork
cmd/beterrabad/main.ha | 56 ++++++++++++++++++++++++-------------------
diff --git a/cmd/beterrabad/main.ha b/cmd/beterrabad/main.ha index 68ff3d85dd3d3adc236712cd991e1122990215c1..03ca71b51481f484397c2196127ec4b45780e76d 100644 --- a/cmd/beterrabad/main.ha +++ b/cmd/beterrabad/main.ha @@ -2,6 +2,7 @@ use beterraba; use os; use os::exec; use fs; +use log; use io; use strings; use path; @@ -59,7 +60,7 @@ //const sock = bind(&services, sigfd); //defer shutdown(&sock); //for (dispatch(&sock)) void; - fmt::println("beterrabad service running")!; + log::println("beterrabad service running"); // Boot all services up for (let i = 0z; i < len(services); i += 1) { @@ -73,22 +74,24 @@ for (true) { for (let i = 0z; i < len(services); i += 1) { switch (services[i].status) { case beterraba::status::STOPPED => - boot(&services[i]); + log::printfln("Service is stopped"); case beterraba::status::STARTED => + log::printfln("Service is started"); peek(&services[i]); case beterraba::status::CRASHED => + log::printfln("Service is crashed"); recover(&services[i]); }; }; }; - fmt::println("beterrabad service shutdown")!; + log::println("beterrabad service shutdown"); }; // Check if the process is still alive and bail out if it's ok fn peek(service: *beterraba::service) void = { // TODO: Move this kind of thing into a better structured log facility - //fmt::printfln("Peeking on {}", service.name)!; + log::printfln("Peeking on {}", service.name); match (exec::peek(&service.process)) { case let s: exec::status => @@ -102,6 +105,7 @@ // TODO: Add the exit msg service.status = beterraba::status::CRASHED; }; case void => + log::println("Still running... Moving on"); return; case let err: exec::error => service.status = beterraba::status::CRASHED; @@ -114,29 +118,31 @@ fmt::printfln("Implement!")!; }; fn boot(service: *beterraba::service) void = { - fmt::printfln("Booting up {}", service.name)!; - - let cmd = exec::cmd(service.definition.cmd, service.definition.args); - - match (cmd) { - case let cmddef: exec::command => - let proc = exec::start(&cmddef); - - // If there's a PID, the process is probably fine - match (proc) { - case let e: exec::error => - service.status = beterraba::status::CRASHED; + log::printfln("Booting up {}", service.name); - case let p: exec::process => + match (exec::fork()) { + case let childpid: int => service.status = beterraba::status::STARTED; - service.process = p; - }; + service.process = childpid; + log::printfln("Starting process {}", service.name); + case let err: exec::error => + log::fatal("Couldn't fork {}, error", exec::strerror(err)); + case void => + let cmd = exec::cmd("bash", "-c", "~/test.sh"); - case exec::nocmd => - fmt::printfln("Couldn't execute cmd {}", service.definition.cmd)!; - case exec::error => - fmt::printfln("Couldn't execute cmd {}, error", service.definition.cmd)!; - case => - fmt::println("Something went terrible wrong, good luck!")!; + match (cmd) { + case let cmddef: exec::command => + exec::exec(&cmddef); + case exec::nocmd => + fmt::printfln("Couldn't build cmd {}", service.definition.cmd)!; + case exec::error => + fmt::printfln("Couldn't execute cmd {}, error", service.definition.cmd)!; + case => + fmt::println("Something went terrible wrong, good luck!")!; + }; }; + + //let cmd = exec::cmd(service.definition.cmd, service.definition.args); + let cmd = exec::cmd("bash", "-c", "~/test.sh"); + };