feat: low files when runnin as nodaemon

This commit is contained in:
Aliberk Sandıkçı 2023-08-24 15:27:00 +03:00
parent efc07514b1
commit 73c6ceac7f
Signed by: asandikci
GPG key ID: 25C67A03B5666BC1
2 changed files with 29 additions and 4 deletions

View file

@ -4,6 +4,7 @@ import (
"log" "log"
"os" "os"
"syscall" "syscall"
"time"
"git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/utils" "git.aliberksandikci.com.tr/Liderahenk/ahenk-go/pkg/utils"
@ -13,7 +14,10 @@ import (
const PidFile = "/run/ahenkd-go.pid" const PidFile = "/run/ahenkd-go.pid"
const ExecutablePath = "/usr/bin/ahenkd-go" const ExecutablePath = "/usr/bin/ahenkd-go"
const ConfDir = "/etc/ahenk-go/" const DataDir = "/etc/ahenk-go/"
const LogFile = DataDir + "ahenk.log"
const LibDir = "/usr/share/ahenk-go/"
const PluginDir = LibDir + "/plugins/"
// FIXME there isn't any difference with Stop() function // FIXME there isn't any difference with Stop() function
// TODO There can be a Start() function in it but start function doesnt work properly right now // TODO There can be a Start() function in it but start function doesnt work properly right now
@ -40,13 +44,13 @@ func main() {
if len(os.Args) == 2 && slices.Contains([]string{"start", "stop", "restart", "nodaemon"}, os.Args[1]) { if len(os.Args) == 2 && slices.Contains([]string{"start", "stop", "restart", "nodaemon"}, os.Args[1]) {
switch os.Args[1] { switch os.Args[1] {
case "start": case "start":
utils.CreatePath(ConfDir) utils.CreatePath(DataDir)
cntxt := &daemon.Context{ cntxt := &daemon.Context{
PidFileName: PidFile, PidFileName: PidFile,
PidFilePerm: 0644, PidFilePerm: 0644,
LogFileName: ConfDir + "ahenkd.log", LogFileName: LogFile,
LogFilePerm: 0640, LogFilePerm: 0640,
WorkDir: ConfDir, WorkDir: LibDir,
Umask: 027, Umask: 027,
Args: []string{ExecutablePath, "start"}, Args: []string{ExecutablePath, "start"},
} }
@ -70,6 +74,19 @@ func main() {
os.Exit(0) os.Exit(0)
case "nodaemon": case "nodaemon":
log.Print("STARTED AS NO-DAEMON") log.Print("STARTED AS NO-DAEMON")
f := utils.OpenLogFile(LogFile)
defer f.Close()
log.SetOutput(f)
log.Printf("Log test")
time.Sleep(10 * time.Second)
log.Print("Killed")
case "tmptest":
log.Print("TEMPORARY TEST")
time.Sleep(3 * time.Second)
log.Print("Killed")
} }
} else { } else {
panic("Please enter a valid option !") panic("Please enter a valid option !")

View file

@ -47,3 +47,11 @@ func Check(err error) {
panic(err) panic(err)
} }
} }
func OpenLogFile(path string) *os.File {
f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
panic(err)
}
return f
}