github.com/fanux/shipyard@v0.0.0-20161009071005-6515ce223235/controller/plugin/plugin_pipeline/cmd/cron.go (about) 1 package cmd 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "log" 7 "time" 8 9 "github.com/robfig/cron" 10 "github.com/shipyard/shipyard/controller/plugin/client" 11 ) 12 13 // TODO plugin regist itself or through web add it manual, I think auto regist is better, 14 // web api just update a plugin info 15 const PLUGIN_NAME = "plugin_pipeline" 16 17 type PipelineRule struct { 18 Cron string 19 //Apps []client.AppScale 20 Apps []client.AppScale 21 } 22 23 type Job struct { 24 Client client.Client 25 Rule PipelineRule 26 } 27 28 func (this Job) Run() { 29 log.Printf("job info: Cron: [%s]", this.Rule.Cron) 30 fmt.Println("time: ", time.Now().Format("2016-01-02 03:04:05 PM")) 31 32 // add if strategy is enable 33 this.Client.ScaleApps(this.Rule.Apps) 34 } 35 36 func startJob(client client.Client, rules []PipelineRule) { 37 fmt.Println(rules) 38 39 crontab := cron.New() 40 crontab.Start() 41 //defer crontab.Stop() 42 43 for _, rule := range rules { 44 job := Job{Client: client, Rule: rule} 45 fmt.Println("Cron:", job.Rule.Cron) 46 crontab.AddFunc(job.Rule.Cron, job.Run) 47 } 48 } 49 50 func startCron(host, port string) { 51 client := client.NewClient(host, port) 52 log.Printf("start cron, plugin name is: %s", PLUGIN_NAME) 53 54 plugin, err := client.GetPluginInfo(PLUGIN_NAME) 55 if err != nil { 56 log.Printf("load plugin [%s] info error: %s", PLUGIN_NAME, err) 57 return 58 } 59 60 strategies, err := client.GetPluginStrategies(PLUGIN_NAME) 61 if err != nil { 62 log.Printf("load plugin [%s] strategies error: %s", PLUGIN_NAME, err) 63 return 64 } 65 66 // TODO enable disable plugin logic, I think use message bus change plugin 67 if plugin.Status != "enable" { 68 } 69 70 //rules := make([]PipelineRule, 200) 71 rules := []PipelineRule{} 72 73 for _, strategy := range strategies { 74 if strategy.Status == "enable" { 75 e := json.Unmarshal([]byte(strategy.Document), &rules) 76 if e != nil { 77 fmt.Println("umarshal error", e) 78 } 79 80 startJob(client, rules) 81 } 82 } 83 84 select {} 85 }