]> git.giacomolli.uk - onechat/blobdiff - daemon.go
Document private backups and a daily user timer
[onechat] / daemon.go
index 5ec220ed6d3f144db0805b2f3beb8a06da22ac96..e534aeac96a7e1e781c3f55ba9e636848b062fb7 100644 (file)
--- a/daemon.go
+++ b/daemon.go
@@ -7,7 +7,6 @@ import (
        "errors"
        "fmt"
        "io"
-       "log"
        "net"
        "os"
        "path/filepath"
@@ -111,19 +110,19 @@ func daemon(ctx context.Context, out io.Writer) (err error) {
                return err
        }
 
-       linked := "WhatsApp not linked"
+       linked := "not linked"
        if whatsapp.isLinked() {
-               linked = "WhatsApp linked"
+               linked = "linked"
        }
 
-       logger.Printf("ready: socket %s, model %s (%s), workdir %s, %s, %d schedules", listener.Addr(), agent.config.Model, agent.config.Effort, agent.config.Workdir, linked, schedules.Len())
+       logger.Info("ready", "version", version, "socket", listener.Addr(), "model", agent.config.Model, "effort", agent.config.Effort, "workdir", agent.config.Workdir, "whatsapp", linked, "schedules", schedules.Len())
 
        var telegram *TelegramBot
 
        if hasBot {
                telegram = newTelegramBot(newTelegramAPI(telegramAPIURL, botSettings.Token), botSettings.Owner, filepath.Join(agent.dir, telegramOffsetFileName), daemonLogger(out, "telegram: "))
 
-               logger.Print("Telegram bot started")
+               logger.Info("Telegram bot started")
        }
 
        return daemonServe(ctx, listener, telegram, whatsapp, schedules, func(notify func(Event)) (*TurnLoop, error) {
@@ -205,12 +204,24 @@ func (h *DaemonHub) read(ctx context.Context, client *DaemonClient, l *TurnLoop)
 
                err := json.Unmarshal(scanner.Bytes(), &request)
                if err == nil {
-                       err = daemonRunRequest(l, h.whatsapp, request, func(text string) {
-                               h.Send(client, Event{
-                                       Kind: "notice",
-                                       Text: text,
+                       if request.Type == "backup" {
+                               var size int64
+
+                               size, err = backupData(ctx, filepath.Dir(l.agent.dir), request.Text, h.whatsapp, l.runs.Snapshot)
+                               if err == nil {
+                                       h.Send(client, Event{
+                                               Kind: "backup",
+                                               Text: fmt.Sprintf("%s (%d bytes)", request.Text, size),
+                                       })
+                               }
+                       } else {
+                               err = daemonRunRequest(l, h.whatsapp, request, func(text string) {
+                                       h.Send(client, Event{
+                                               Kind: "notice",
+                                               Text: text,
+                                       })
                                })
-                       })
+                       }
                }
 
                if err != nil {
@@ -443,25 +454,3 @@ func daemonRunRequest(l *TurnLoop, whatsapp *WhatsApp, request SocketRequest, an
                return fmt.Errorf("unknown request %q", request.Type)
        }
 }
-
-// daemonLogger returns the logger of the part of the daemon with prefix, such
-// as "telegram: ", on out. The journal adds the time to each line and sets
-// JOURNAL_STREAM for a service, so then the lines have no time stamp. Otherwise
-// each line has the standard time stamp.
-func daemonLogger(out io.Writer, prefix string) *log.Logger {
-       if os.Getenv("JOURNAL_STREAM") != "" {
-               return log.New(out, prefix, 0)
-       }
-
-       return log.New(out, prefix, log.LstdFlags)
-}
-
-// durationText returns d for a log line: rounded to the second, or to the
-// millisecond when it is shorter than a second.
-func durationText(d time.Duration) string {
-       if d < time.Second {
-               return d.Round(time.Millisecond).String()
-       }
-
-       return d.Round(time.Second).String()
-}