forked from convox/rack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcronjob.go
69 lines (56 loc) · 1.61 KB
/
cronjob.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
package models
import (
"crypto/sha256"
"encoding/base32"
"fmt"
"regexp"
"strings"
"github.com/convox/rack/manifest"
)
type CronJob struct {
Name string `yaml:"name"`
Schedule string `yaml:"schedule"`
Command string `yaml:"command"`
Service *manifest.Service
App *App
}
//CronJobs is a wrapper for sorting
type CronJobs []CronJob
func (a CronJobs) Len() int { return len(a) }
func (a CronJobs) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a CronJobs) Less(i, j int) bool { return a[i].Name < a[j].Name }
func NewCronJobFromLabel(key, value string) CronJob {
keySlice := strings.Split(key, ".")
name := keySlice[len(keySlice)-1]
tokens := strings.Fields(value)
cronjob := CronJob{
Name: name,
Schedule: fmt.Sprintf("cron(%s *)", strings.Join(tokens[0:5], " ")),
Command: strings.Join(tokens[5:], " "),
}
return cronjob
}
func (cr *CronJob) AppName() string {
return cr.App.Name
}
func (cr *CronJob) Process() string {
return cr.Service.Name
}
func (cr *CronJob) ShortName() string {
shortName := fmt.Sprintf("%s%s", strings.Title(cr.Service.Name), strings.Title(cr.Name))
reg, err := regexp.Compile("[^A-Za-z0-9]+")
if err != nil {
panic(err)
}
return reg.ReplaceAllString(shortName, "")
}
func (cr *CronJob) LongName() string {
prefix := fmt.Sprintf("%s-%s-%s", cr.App.StackName(), cr.Process(), cr.Name)
hash := sha256.Sum256([]byte(prefix))
suffix := "-" + base32.StdEncoding.EncodeToString(hash[:])[:7]
// $prefix-$suffix-schedule" needs to be <= 64 characters
if len(prefix) > 55-len(suffix) {
prefix = prefix[:55-len(suffix)]
}
return prefix + suffix
}