Skip to content

Commit

Permalink
feat(store): support to rotate logs by Ayd self
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed Dec 3, 2022
1 parent 9965df3 commit a21faec
Show file tree
Hide file tree
Showing 11 changed files with 825 additions and 111 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,8 +607,25 @@ You can change this with `-f` option like below.
$ ayd -f /path/to/ayd.log ping:example.com
```

There is no feature to log rotate.
Please consider using the log rotation tool if you have a plan to use it for a long time.
It is recommended to rotate logs if you run Ayd for long time.
You can rotate logs like below.

``` shell
$ ayd -f ./ayd_%Y%m%d.log ping:example.com
$ ayd -f /path/to/%Y/%m%d/ayd.log ping:example.com
```

There are some keywords to specify how to name log files.

- `%Y`: Full year like `2006`.
- `%y`: Short year like `06`.
- `%m`: Month.
- `%d`: Day of month.
- `%H`: Hour.
- `%M`: Minute.
- `%%`: A '%' character.

Unknown keywords will be just ignored and keep as is.

If you use `-f -` option, Ayd does not write log file.
This is not recommended for production use because Ayd can't restore last status when restore if don't save log file.
Expand Down
15 changes: 12 additions & 3 deletions cmd/ayd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ func (cmd *AydCommand) Run(args []string) (exitCode int) {
fmt.Fprintf(cmd.ErrStream, "error: failed to open log file: %s\n", err)
return 1
}
defer s.Close()

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
Expand All @@ -150,6 +149,7 @@ func (cmd *AydCommand) Run(args []string) (exitCode int) {
alert, err := scheme.NewAlerterSet(cmd.AlertURLs)
if err != nil {
fmt.Fprintln(cmd.ErrStream, err)
s.Close()
return 2
}
s.OnStatusChanged = append(s.OnStatusChanged, func(r api.Record) {
Expand All @@ -158,10 +158,19 @@ func (cmd *AydCommand) Run(args []string) (exitCode int) {
}

if cmd.OneshotMode {
return cmd.RunOneshot(ctx, s)
exitCode = cmd.RunOneshot(ctx, s)
} else {
return cmd.RunServer(ctx, s)
exitCode = cmd.RunServer(ctx, s)
}

s.Close()

healthy, _ := s.Errors()
if exitCode == 0 && !healthy {
return 1
}

return exitCode
}

func main() {
Expand Down
26 changes: 26 additions & 0 deletions cmd/ayd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package main_test
import (
"bytes"
"fmt"
"os"
"path/filepath"
"regexp"
"runtime"
"testing"

"github.com/macrat/ayd/cmd/ayd"
Expand Down Expand Up @@ -198,3 +201,26 @@ func TestAydCommand_Run(t *testing.T) {
})
}
}

func TestAydCommand_Run_permissionDenied(t *testing.T) {
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
t.Skip("permission test only works on *nix OS")
}

path := filepath.Join(t.TempDir(), "log")
if err := os.Mkdir(path, 0); err != nil {
t.Fatalf("failed to make test directory: %s", err)
}

buf := bytes.NewBuffer([]byte{})
cmd := main.AydCommand{
OutStream: buf,
ErrStream: buf,
}

code := cmd.Run([]string{"ayd", "-1", "-f", filepath.Join(path, "ayd.log"), "dummy:"})
t.Log(buf.String())
if code != 1 {
t.Errorf("unexpected return code: %d", code)
}
}
22 changes: 0 additions & 22 deletions cmd/ayd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package main_test
import (
"context"
"fmt"
"os"
"regexp"
"runtime"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -130,26 +128,6 @@ func TestRunServer_tls_error(t *testing.T) {
}
}

func TestRunServer_permissionError(t *testing.T) {
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
t.Skip("permission test only works on *nix OS")
}

s := testutil.NewStore(t)
defer s.Close()
os.Chmod(s.Path(), 0200)

ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()

cmd, _ := MakeTestCommand(t, []string{"dummy:"})

code := cmd.RunServer(ctx, s)
if code != 1 {
t.Errorf("unexpected return code: %d", code)
}
}

func BenchmarkRunServer(b *testing.B) {
s := testutil.NewStore(b)
defer s.Close()
Expand Down
3 changes: 0 additions & 3 deletions internal/endpoint/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
)

type Store interface {
// Path returns path to log file.
Path() string

// Targets returns target URLs include inactive target.
Targets() []string

Expand Down
Loading

0 comments on commit a21faec

Please sign in to comment.