-
Notifications
You must be signed in to change notification settings - Fork 0
/
initializer.go
83 lines (74 loc) · 1.82 KB
/
initializer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package gonewsyslog
import (
"errors"
"os/exec"
"github.com/spamoc/gonewsyslog/internal/archiver"
"github.com/spamoc/gonewsyslog/internal/rotate"
)
func canRotateBySize(info rotate.LogInfo, rule string) bool {
size, _ := sizeToInt(rule)
return info.Size >= size
}
func canRotateByCron(info rotate.LogInfo, rule string) bool {
//Not Implements yet
return false
}
type archiveType string
// if you change it, should change conf/conf const, too.
const (
GZIP archiveType = "gzip"
BZ2 = "bz2"
XZ = "xz"
NONE = "none"
)
func New(name string, from string, to string, archiveTypeStr string, ext string, success string, failed string, pidPath string, rTerm string, rSize string, rCount int) (NewSyslogJob, error) {
var archive archiver.Archive = nil
switch archiveType(archiveTypeStr) {
case GZIP:
archive = archiver.GZArchive
case BZ2:
archive = archiver.BZ2Archive
case XZ:
archive = archiver.XZArchive
case NONE:
archive = nil
default:
return NewSyslogJob{}, errors.New("archive type is invalid." + archiveTypeStr)
}
// define condition evaluator
cond := condition{}
if rTerm != "" {
cond = condition{
rule: rTerm,
evalIf: canRotateByCron,
}
} else if rSize != "" {
if _, err := sizeToInt(rSize); err != nil {
return NewSyslogJob{}, err
}
cond = condition{
rule: rSize,
evalIf: canRotateBySize,
}
} else {
//always return true(always rotate log)
cond = condition{
rule: "",
evalIf: func(_ rotate.LogInfo, _ string) bool {
return true
},
}
}
return NewSyslogJob{
name: name,
from: from,
to: to,
archiveIf: archive,
ext: ext,
cond: cond,
count: rCount,
success: func() { exec.Command(success).Start() },
failed: func() { exec.Command(failed).Start() },
pidPath: pidPath,
}, nil
}