takama / daemon

A daemon package for use with Go (golang) services

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

no idea how to persist items in dameon process

maximus12793 opened this issue · comments

    package main

    import (
        "fmt"
        "github.com/robfig/cron"
        "github.com/takama/daemon"
        "log"
        "os"
    )

    const (
        // name of the service
        name        = "cronTool"
        description = "Cron service task"
    )

    var stdlog, errlog *log.Logger

    // Service is the daemon service struct
    type Service struct {
        d daemon.Daemon
        c cron.Cron
    }

    func startCron(c *cron.Cron) {
        // Run 1x everymin
        c.AddFunc("* * * * * *", func() { makeFile() })
        c.Start()
    }
    func stopCron(c *cron.Cron) {
        c.Stop()
    }

    var times int

    func makeFile() {
        times++
        f, err := os.Create(fmt.Sprintf("%d.txt", times))
        if err != nil {
            log.Fatal(err)
        }
        defer f.Close()
    }

    // Manage by daemon commands or run the daemon
    func (service *Service) Manage() (string, error) {

        usage := "Usage: cronStock install | remove | start | stop | status"
        // if received any kind of command, do it
        if len(os.Args) > 1 {
            command := os.Args[1]
            switch command {
            case "install":
                startCron(&service.c)
                return service.d.Install()
            case "remove":
                return service.d.Remove()
            case "start":
                startCron(&service.c)
                return service.d.Start()
            case "stop":
                stopCron(&service.c)
                return service.d.Stop()
            case "status":
                return service.d.Status()
            default:
                return usage, nil
            }
        }

        // // c.AddFunc("@weekly", func() {}) // my actual usage will be as follows
        return usage, nil
    }
    func init() {
        stdlog = log.New(os.Stdout, "", log.Ldate|log.Ltime)
        errlog = log.New(os.Stderr, "", log.Ldate|log.Ltime)
    }
    func main() {
        c := cron.New()
        startCron(c)
        srv, err := daemon.New(name, description)
        if err != nil {
            errlog.Println("Error: ", err)
            os.Exit(1)
        }
        service := &Service{srv, *c}
        status, err := service.Manage()
        if err != nil {
            errlog.Println(status, "\nError: ", err)
            os.Exit(1)
        }
        fmt.Println(status)
    }

I am trying to simply run a daemon that starts a cronjob. I cannot see any examples of anything outside of reading from a channel so its unclear why this cron job is not being ran. Any ideas?
both sudo ./cronTool install and sudo ./cronTool start seem to do nothing.

Edit: it works fine with cron.Run() rather than cron.Start() but Run() blocks the daemon so I cannot detach reliably. Is there some issue with goroutines inside a daemon? I have tried keeping an event loop, passing cron into main or into the service struct. Everything seems to block or not execute

resolved w/ example in dev branch