github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/velodrome/fetcher/fetcher.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "flag" 21 "os" 22 "path/filepath" 23 "time" 24 25 "k8s.io/test-infra/velodrome/sql" 26 27 "github.com/golang/glog" 28 _ "github.com/jinzhu/gorm/dialects/mysql" 29 "github.com/spf13/cobra" 30 ) 31 32 type fetcherConfig struct { 33 Client 34 sql.MySQLConfig 35 36 once bool 37 frequency int 38 } 39 40 func addRootFlags(cmd *cobra.Command, config *fetcherConfig) { 41 cmd.PersistentFlags().IntVar(&config.frequency, "frequency", 2, "Number of iterations per hour") 42 cmd.PersistentFlags().BoolVar(&config.once, "once", false, "Run once and then leave") 43 cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine) 44 } 45 46 func runProgram(config *fetcherConfig) error { 47 if err := config.Client.CheckFlags(); err != nil { 48 return err 49 } 50 51 db, err := config.CreateDatabase() 52 if err != nil { 53 return err 54 } 55 56 ticker := time.Tick(time.Hour / time.Duration(config.frequency)) 57 58 for { 59 tx := db.Begin() 60 UpdateIssues(tx, config) 61 tx.Commit() 62 63 if config.once { 64 break 65 } 66 67 <-ticker 68 } 69 70 return nil 71 } 72 73 func main() { 74 config := &fetcherConfig{} 75 root := &cobra.Command{ 76 Use: filepath.Base(os.Args[0]), 77 Short: "Fetches github database: Pull-requests, issues, and events", 78 RunE: func(_ *cobra.Command, _ []string) error { 79 return runProgram(config) 80 }, 81 } 82 addRootFlags(root, config) 83 config.Client.AddFlags(root) 84 config.MySQLConfig.AddFlags(root) 85 86 if err := root.Execute(); err != nil { 87 glog.Fatalf("%v\n", err) 88 } 89 }