func (p *PeerImpl) ensureConnected() { touchPeriod := viper.GetDuration("peer.discovery.touchPeriod") touchMaxNodes := viper.GetInt("peer.discovery.touchMaxNodes") tickChan := time.NewTicker(touchPeriod).C peerLogger.Debugf("Starting Peer reconnect service (touch service), with period = %s", touchPeriod) for { // Simply loop and check if need to reconnect <-tickChan peersMsg, err := p.GetPeers() if err != nil { peerLogger.Errorf("Error in touch service: %s", err.Error()) } allNodes := p.discHelper.GetAllNodes() // these will always be returned in random order if len(peersMsg.Peers) < len(allNodes) { peerLogger.Warning("Touch service indicates dropped connections, attempting to reconnect...") delta := util.FindMissingElements(allNodes, getPeerAddresses(peersMsg)) if len(delta) > touchMaxNodes { delta = delta[:touchMaxNodes] } p.chatWithSomePeers(delta) } else { peerLogger.Debug("Touch service indicates no dropped connections") } peerLogger.Debugf("Connected to: %v", getPeerAddresses(peersMsg)) peerLogger.Debugf("Discovery knows about: %v", allNodes) } }
// initialize the configuration func initConfiguration() { // try to load some configuration information from the config file viper.SetConfigName("config") viper.SetDefault("mode", MODE_PI) viper.SetDefault("timeout", 5*time.Second) viper.SetDefault("debounceContact", 1000*time.Millisecond) viper.SetDefault("startNumber", "1") err := viper.ReadInConfig() if err != nil { panic(fmt.Errorf("Fatal error config file: %s \n", err)) } // read configuration from file or use default Timeout = viper.GetDuration("timeout") DebounceContact = viper.GetDuration("debounceContact") Mode = viper.GetString("mode") StartNumberOfID = viper.GetString("startNumber") // create a new lock for concurrent access Mutex = &sync.RWMutex{} // initialize the game state setState(IS_STOPPED) // initialize our communication channels // note: the contactChannel is debounced to handle continuous triggering of the contact contactChannel = make(chan bool) contactChannelDebounced = debounceContactChannel(DebounceContact, contactChannel) // we need a signal channel to communicate events to the webserver signalChannel = make(chan string, 100) // handle start events startChannel = make(chan bool) // handle finish events finishChannel = make(chan StopReason) // parse all existing results idsInUse, _ = parseExistingStudyResults() }
// start starts the Peer server function func (d *Handler) start() error { discPeriod := viper.GetDuration("peer.discovery.period") tickChan := time.NewTicker(discPeriod).C peerLogger.Debug("Starting Peer discovery service") for { select { case <-tickChan: if err := d.ChatStream.Send(&pb.OpenchainMessage{Type: pb.OpenchainMessage_DISC_GET_PEERS}); err != nil { peerLogger.Error(fmt.Sprintf("Error sending %s during handler discovery tick: %s", pb.OpenchainMessage_DISC_GET_PEERS, err)) } // // TODO: For testing only, remove eventually. Test the blocks transfer functionality. // syncBlocksChannel, _ := d.GetBlocks(&pb.SyncBlockRange{Start: 0, End: 0}) // go func() { // for { // // peerLogger.Debug("Sleeping for 1 second...") // // time.Sleep(1 * time.Second) // // peerLogger.Debug("Waking up and pulling from sync channel") // syncBlocks, ok := <-syncBlocksChannel // if !ok { // // Channel was closed // peerLogger.Debug("Channel closed for SyncBlocks") // break // } else { // peerLogger.Debug("Received SyncBlocks on channel with Range from %d to %d", syncBlocks.Range.Start, syncBlocks.Range.End) // } // } // }() // // TODO: For testing only, remove eventually. Test the State Snapshot functionality // syncStateSnapshotChannel, _ := d.GetStateSnapshot() // go func() { // for { // // peerLogger.Debug("Sleeping for 1 second...") // // time.Sleep(1 * time.Second) // // peerLogger.Debug("Waking up and pulling from sync channel") // syncStateSnapshot, ok := <-syncStateSnapshotChannel // if !ok { // // Channel was closed // peerLogger.Debug("Channel closed for SyncStateSnapshot") // break // } else { // peerLogger.Debug("Received SyncStateSnapshot on channel with block = %d, correlationId = %d, sequence = %d, len delta = %d", syncStateSnapshot.BlockNumber, syncStateSnapshot.Request.CorrelationId, syncStateSnapshot.Sequence, len(syncStateSnapshot.Delta)) // } // } // }() case <-d.doneChan: peerLogger.Debug("Stopping discovery service") return nil } } }
// start starts the Peer server function func (d *Handler) start() error { discPeriod := viper.GetDuration("peer.discovery.period") tickChan := time.NewTicker(discPeriod).C peerLogger.Debug("Starting Peer discovery service") for { select { case <-tickChan: if err := d.SendMessage(&pb.Message{Type: pb.Message_DISC_GET_PEERS}); err != nil { peerLogger.Errorf("Error sending %s during handler discovery tick: %s", pb.Message_DISC_GET_PEERS, err) } case <-d.doneChan: peerLogger.Debug("Stopping discovery service") return nil } } }
func main() { log.SetPrefix("TWEELACK ") viper.SetConfigName("config") viper.AddConfigPath(".") viper.AddConfigPath("$HOME/.tweelack") err := viper.ReadInConfig() if err != nil { log.Fatalln(err) } twitterConsumerKey := viper.GetString("twitter.consumer.key") if len(twitterConsumerKey) == 0 { log.Fatalln("Twitter consumer key required") } twitterConsumerSecret := viper.GetString("twitter.consumer.secret") if len(twitterConsumerSecret) == 0 { log.Fatalln("Twitter consumer secret required") } twitterTokenValue := viper.GetString("twitter.token.value") if len(twitterTokenValue) == 0 { log.Fatalln("Twitter token value required") } twitterTokenSecret := viper.GetString("twitter.token.secret") if len(twitterTokenSecret) == 0 { log.Fatalln("Twitter token secret required") } if TwitterHandle = viper.GetString("twitter.handle"); len(TwitterHandle) == 0 { log.Fatalln("Twitter handle required") } if TweetCount = viper.GetInt("twitter.count"); TweetCount == 0 { TweetCount = 1 } if TwitterTimeout = viper.GetDuration("twitter.timeout"); TwitterTimeout == 0 { TwitterTimeout = 1 } TwitterHashtag = viper.GetString("twitter.hashtag") slackToken := viper.GetString("slack.token") if len(slackToken) == 0 { log.Fatalln("Slack token required") } if SlackChannel = viper.GetString("slack.channel"); len(SlackChannel) == 0 { SlackChannel = "general" } if Purpose = viper.GetString("slack.purpose"); len(Purpose) == 0 { Purpose = "post" } SlackMessageParameters = slack.NewPostMessageParameters() SlackMessageParameters.AsUser = viper.GetBool("slack.parameters.as_user") SlackMessageParameters.Username = viper.GetString("slack.parameters.username") SlackMessageParameters.IconURL = viper.GetString("slack.parameters.icon.url") SlackMessageParameters.IconEmoji = viper.GetString("slack.parameters.icon.emoji") anaconda.SetConsumerKey(twitterConsumerKey) anaconda.SetConsumerSecret(twitterConsumerSecret) TwitterApi = anaconda.NewTwitterApi(twitterTokenValue, twitterTokenSecret) SlackApi = slack.New(slackToken) UrlValues = url.Values{} UrlValues.Set("screen_name", TwitterHandle) UrlValues.Set("exclude_replies", "true") UrlValues.Set("include_rts", "false") if channels, err := SlackApi.GetChannels(true); err == nil { for _, channel := range channels { if channel.Name == SlackChannel { SlackChannel = channel.ID break } } } PostToSlack(FetchTweets()) }
func main() { logging.InitializeLogging() // user needs help var needHelp bool // configuration file var configFile string // set up flag to point at conf, parse arguments and then verify flag.BoolVar(&needHelp, "help", false, "Display this dialog") flag.StringVar(&configFile, "config", "", "JSON configuration file") flag.Parse() if needHelp { flag.Usage() os.Exit(1) } fmt.Println(" ...~.+=:,.I+... ") fmt.Println(" ... .+$7II?I?IZ7$??+~?I~.. ") fmt.Println(" .,.:M=.MINI++I7I??????7I$7I=+7~... ") fmt.Println(" O7+8.IOIII??7+I+7Z?7$I?I?I+??+$??O?. ") fmt.Println(" ..8MMMM8I??+?+=I?+I?=7++:7???I?==++??$?+?. ") fmt.Println(" :MMMMM.:~+?I=++$I++???=+??I==~+++?~I=7+=+~?7. ") fmt.Println(" MMM..++I~?I?$I$$??+?+~$I++=I?7==++I++I+=?+=~=~. ") fmt.Println(" .MM..~.7?~==~I+?7=II=8~?++?~?=7+$~?$D?=~7?+?+~=I:. ") fmt.Println(" .MM.?~~$7+=~?=:+?7I=???I7II?7I++DZ?+=+7===+=??=~+?+. ") fmt.Println(" .MM..NZ?M87+??+7?++?7=I$I7=+=IMMMMD=+$~=?++?:??=~+77I. ") fmt.Println(" MMNMMMMMMMMMN$Z++7I7+=+++==MMMI~~+I=?+=?++I+=I?=I7$$?: ") fmt.Println(" .MMMMMMMMMMMMMM8I+++77++?:?MMM::?~=I?+??II?=+?+=?I++I?Z= ") fmt.Println(" IDMMMMMMMMMMMMMMM$I?I$7==NMM=++~~=7+=I???7I??8I$+I+?I?I$??. ") fmt.Println(" .MMMMMMMMMMMMMMMMMMOI7?II~MMM+=7=+~7:+$?+II7I?+~I?++I??I??+I.. ") fmt.Println(" .MMMMMMMMMMMMMMMMMMMD==:+~NMM+=+?:=+?+~=8=++?$7+I?7+=+?II$?+==. ") fmt.Println(" .MMMMMMMMMMMMDMMMMMMM8I?:MMM+?+~$,=:??7==???Z?7II$7II7??7++=7:I. ") fmt.Println(" MMMMMMMMMMMM8NMMMMDMMNMMMM??78~:,+=??~:~II?~+II?II7+I+??7I7=ZI=~. ") fmt.Println(" MMMMMMMMMMMNNNMMMMNMM$?II==?=?+:==++?I++==+II$I=77I??II?II8$??7I~ ") fmt.Println(" $M7M8MMMMMMMMMMMMMMMM+?~~==+$~~7:$I+7I??~+++=?7I=?$??I?$III?7?I$7Z ") fmt.Println(" NDMMMMMMMMMMMMMMMMMMM7???$$I?+III787+?7?????==+++?~??I?8ZI77I?I77?. ") fmt.Println(" NMMMMMMMMMMMMMMMMMMMMD==??=:~77?I=?77?7+I=?I7=I?~+??77~??+IIZ$7$7$7. ") fmt.Println(" ZMMMMMMMMMMMMMMMMMMMMM8??+=,=I?IZ+++=7?I+?II???+?+?7?=?I$+IZ$O7$ZI7=. ") fmt.Println(" MMMMMMMMMMMMMMMMMMM8MMMDIZ7+++?~?+MMMII$?~7II=I?I7ZI=7$?II7$77I$7I7Z ") fmt.Println(" .NMMMMMMMMDMMMMMMMMMOMO=77?=+++?MMMI=$+III??+IOO+?I?I=???ZI?I7I7$ZZZ?. ") fmt.Println(" DMMMMMMMMMMMMMMNMMMM7?++$~+OMMMO~II?777III?I$Z=?I7I7?+II+I7II7787O. ") fmt.Println(" .8MMMMMMMMMMMMN7NMMZ+I=~~IOMMOD/?O=7$7+$7?777I+?I+I?I7Z?7?7I7$8OZ. ") fmt.Println(" . ..$MMNMMMMMMMMMMMMMNM7+7MMMMMNO+++I??IO77II+8$I?I=+?+I7?+?Z$I7I$$7~, ") fmt.Println(" .NNMMMMMMMMMMMMMMMMMIMMMMMMZM+?7+?+I+++7ZI7$777??I7I7II777$D8I?+II7Z,. ") fmt.Println(" .MMMMMMMMMMMMMNMMMMI+~,==?~~7~+++7II7=?II$?IZI7+?II?=IO7?I=+?7Z7O??$D. ") fmt.Println(".MMNMMM MMMMMMMMMNDMMNMMMZ+~==~::?~I$?I~~II78=7?77?I?II=7??I$II?II$$IZ7$$. ") fmt.Println(" .O NMMMMMMMMMMD..~NMMM.7=,+~~+?7I?=$+I$7II7I7IIII888$Z$I?II7ZI$7II+$~ ") fmt.Println(" .~NMNMMMMMDZ. ..MMM.+~=+==+Z7777$7Z77I?7Z$II77ZII$O7I+II?7+7I+~I ") fmt.Println(" .MM$=. .M .:NMM~.=+?II$III7IIIII$Z?+$O$$?I77777III77I?7?++: ") fmt.Println(" 8NN. MM .ZMMM$ .:????8887+7III7O$$+Z$$$7Z$O77I??+$=????= ") fmt.Println(" .MM:. MM. .DMM. ..7+~==?+7$88$$OIOID$7I77???+$7===+I ") fmt.Println(" ,M. +M, .M,. .~:~,~~=7$Z87I7II7Z7I$I=~~=+=?.. ") fmt.Println(" ,MMZ,. 7. Z~I+:?++==~7Z:7~=7OZ=+++, ") fmt.Println(" .MMMMM, ..~:... ....... ") fmt.Println(" .D?. ?, ") fmt.Println(" ") // defult configuration viper.SetDefault("log-level", "INFO") viper.SetDefault("maximum-age", "1h") viper.SetDefault("wait-interval", "10m") // load config if configFile != "" { viper.SetConfigFile(configFile) if parseErr := viper.ReadInConfig(); parseErr != nil { logging.Log.Criticalf("%v", parseErr) os.Exit(1) } logging.Log.Notice("Config file loaded") } logging.ConfigureLogging(viper.GetString("log-level")) logging.Log.Infof("Log level: %v", viper.GetString("log-level")) maxAge := viper.GetDuration("maximum-age") waitInterval := viper.GetDuration("wait-interval") rootDirs := viper.GetStringSlice("root-dirs") if len(rootDirs) == 0 { logging.Log.Critical("No root directories were provided") os.Exit(1) } // Clean up and check the root directories for rdInd, rootDir := range rootDirs { rootDirAbs, rdErr := filepath.Abs(filepath.Clean(rootDir)) if rdErr != nil { logging.Log.Criticalf("Unable to get absolute form of the root directory <%s>", rootDir) os.Exit(1) } // Do a couple checks on the root directory rootDirInfo, statErr := os.Stat(rootDirAbs) if statErr != nil { logging.Log.Criticalf("Unable to \"Stat\" the root directory <%s>", rootDirAbs) os.Exit(1) } if !rootDirInfo.IsDir() { logging.Log.Criticalf("Root directory <%s> is not a directory", rootDirAbs) os.Exit(1) } rootDirs[rdInd] = rootDirAbs } logging.Log.Notice("Watching for stale directories. Use ctrl-c to exit") //mainLoop: for { // Loop over the contents of rootDirs // We don't apply processDir() directly to the rootDirs because we don't want to delete rootDir if it's empty for _, rootDir := range rootDirs { logging.Log.Debugf("Processing directory <%s>", rootDir) dirContents, readDirErr := ioutil.ReadDir(rootDir) if readDirErr != nil { logging.Log.Criticalf("Unable to read directory <%s>", rootDir) os.Exit(1) } exitOnErrors := false for _, fileInfo := range dirContents { logging.Log.Debugf("Directory <%s> is not empty; processing contents", rootDir) if fileInfo.IsDir() { if procErr := processDir(fileInfo, rootDir, maxAge); procErr != nil { logging.Log.Errorf("An error occurred while processing directory <%s>: %v", fileInfo.Name(), procErr) exitOnErrors = true } } // else it's a file; ignore it } logging.Log.Debugf("Finished processing <%s>", rootDir) if exitOnErrors == true { logging.Log.Critical("Exiting due to directory-processing errors") break } } // Wait the specified amount of time before running again time.Sleep(waitInterval) } logging.Log.Notice("DungBeetle says: \"My job here is done\"") }
// GetPostgresConnectionRetrySleep returns the number of seconds (as set via default, config file, or environment variable) // to wait before trying to connect again func GetPostgresConnectionRetrySleep() time.Duration { return viper.GetDuration(varPostgresConnectionRetrySleep) }
// NewPeerHandler returns a new Peer handler // Is instance of HandlerFactory func NewPeerHandler(coord MessageHandlerCoordinator, stream ChatStream, initiatedStream bool) (MessageHandler, error) { d := &Handler{ ChatStream: stream, initiatedStream: initiatedStream, Coordinator: coord, } d.doneChan = make(chan struct{}) if dur := viper.GetDuration("peer.sync.state.snapshot.writeTimeout"); dur == 0 { d.syncSnapshotTimeout = DefaultSyncSnapshotTimeout } else { d.syncSnapshotTimeout = dur } d.snapshotRequestHandler = newSyncStateSnapshotRequestHandler() d.syncStateDeltasRequestHandler = newSyncStateDeltasHandler() d.syncBlocksRequestHandler = newSyncBlocksRequestHandler() d.FSM = fsm.NewFSM( "created", fsm.Events{ {Name: pb.Message_DISC_HELLO.String(), Src: []string{"created"}, Dst: "established"}, {Name: pb.Message_DISC_GET_PEERS.String(), Src: []string{"established"}, Dst: "established"}, {Name: pb.Message_DISC_PEERS.String(), Src: []string{"established"}, Dst: "established"}, {Name: pb.Message_SYNC_BLOCK_ADDED.String(), Src: []string{"established"}, Dst: "established"}, {Name: pb.Message_SYNC_GET_BLOCKS.String(), Src: []string{"established"}, Dst: "established"}, {Name: pb.Message_SYNC_BLOCKS.String(), Src: []string{"established"}, Dst: "established"}, {Name: pb.Message_SYNC_STATE_GET_SNAPSHOT.String(), Src: []string{"established"}, Dst: "established"}, {Name: pb.Message_SYNC_STATE_SNAPSHOT.String(), Src: []string{"established"}, Dst: "established"}, {Name: pb.Message_SYNC_STATE_GET_DELTAS.String(), Src: []string{"established"}, Dst: "established"}, {Name: pb.Message_SYNC_STATE_DELTAS.String(), Src: []string{"established"}, Dst: "established"}, }, fsm.Callbacks{ "enter_state": func(e *fsm.Event) { d.enterState(e) }, "before_" + pb.Message_DISC_HELLO.String(): func(e *fsm.Event) { d.beforeHello(e) }, "before_" + pb.Message_DISC_GET_PEERS.String(): func(e *fsm.Event) { d.beforeGetPeers(e) }, "before_" + pb.Message_DISC_PEERS.String(): func(e *fsm.Event) { d.beforePeers(e) }, "before_" + pb.Message_SYNC_BLOCK_ADDED.String(): func(e *fsm.Event) { d.beforeBlockAdded(e) }, "before_" + pb.Message_SYNC_GET_BLOCKS.String(): func(e *fsm.Event) { d.beforeSyncGetBlocks(e) }, "before_" + pb.Message_SYNC_BLOCKS.String(): func(e *fsm.Event) { d.beforeSyncBlocks(e) }, "before_" + pb.Message_SYNC_STATE_GET_SNAPSHOT.String(): func(e *fsm.Event) { d.beforeSyncStateGetSnapshot(e) }, "before_" + pb.Message_SYNC_STATE_SNAPSHOT.String(): func(e *fsm.Event) { d.beforeSyncStateSnapshot(e) }, "before_" + pb.Message_SYNC_STATE_GET_DELTAS.String(): func(e *fsm.Event) { d.beforeSyncStateGetDeltas(e) }, "before_" + pb.Message_SYNC_STATE_DELTAS.String(): func(e *fsm.Event) { d.beforeSyncStateDeltas(e) }, }, ) // If the stream was initiated from this Peer, send an Initial HELLO message if d.initiatedStream { // Send intiial Hello helloMessage, err := d.Coordinator.NewOpenchainDiscoveryHello() if err != nil { return nil, fmt.Errorf("Error getting new HelloMessage: %s", err) } if err := d.SendMessage(helloMessage); err != nil { return nil, fmt.Errorf("Error creating new Peer Handler, error returned sending %s: %s", pb.Message_DISC_HELLO, err) } } return d, nil }
func main() { logging.InitializeLogging() // user needs help var needHelp bool // configuration file var configFile string // set up flag to point at conf, parse arguments and then verify flag.BoolVar(&needHelp, "help", false, "Display this dialog") flag.StringVar(&configFile, "config", "", "JSON configuration file") flag.Parse() if needHelp { flag.Usage() os.Exit(1) } // defult configuration viper.SetDefault("log-level", "INFO") viper.SetDefault("broker", "localhost") viper.SetDefault("wait-interval", "1m") viper.SetDefault("subscribe-queue", "diopsid-queue") viper.SetDefault("alerts-queue", "disk_status.machinename") // load config if configFile != "" { viper.SetConfigFile(configFile) if parseErr := viper.ReadInConfig(); parseErr != nil { logging.Log.Criticalf("%v", parseErr) os.Exit(1) } logging.Log.Notice("Config file loaded") } logging.ConfigureLogging(viper.GetString("log-level")) logging.Log.Infof("Log level: %v", viper.GetString("log-level")) wheretolook := viper.GetStringSlice("where-to-look") if len(wheretolook) == 0 { logging.Log.Critical("No directories were provided") os.Exit(1) } // computername := viper.GetString("computer-name") // computername,e := os.Hostname() // if e != nil { // logging.Log.Criticalf("Couldn't get the hostname") // return // } broker := viper.GetString("broker") queueName := viper.GetString("subscribe-queue") alertsQueueName := viper.GetString("alerts-queue") waitInterval := viper.GetDuration("wait-interval") // check authentication for desired username if authErr := authentication.Load(); authErr != nil { logging.Log.Criticalf("Error in loading authenticators: %v", authErr) os.Exit(1) } if !authentication.AmqpAvailable() { logging.Log.Critical("Authentication for AMQP is not available") os.Exit(1) } amqpUser := authentication.AmqpUsername() amqpPassword := authentication.AmqpPassword() url := "amqp://" + amqpUser + ":" + amqpPassword + "@" + broker service := dripline.StartService(url, queueName) if service == nil { logging.Log.Critical("AMQP service did not start") os.Exit(1) } logging.Log.Info("AMQP service started") // add .# to the queue name for the subscription // the queue name does not have to be the same as the queue where to send the alerts! // it is better to define a proper queueName in the config file to prevent // conflict between services subscribing to the same queue (which is not allowed!) subscriptionKey := queueName + ".#" if subscribeErr := service.SubscribeToAlerts(subscriptionKey); subscribeErr != nil { logging.Log.Criticalf("Could not subscribe to alerts at <%v>: %v", subscriptionKey, subscribeErr) os.Exit(1) } if msiErr := fillMasterSenderInfo(); msiErr != nil { logging.Log.Criticalf("Could not fill out master sender info: %v", MasterSenderInfo) os.Exit(1) } for { for _, dir := range wheretolook { alert := dripline.PrepareAlert(alertsQueueName, "application/json", MasterSenderInfo) disk := DiskUsage(dir) var payload map[string]interface{} payload = make(map[string]interface{}) payload["directory"] = dir payload["all"] = float64(disk.All) / float64(GB) payload["used"] = float64(disk.Used) / float64(GB) alert.Message.Payload = payload e := service.SendAlert(alert) logging.Log.Infof("Alert sent: [%s] All: %.2f GB Used: %.2f GB", dir, float64(disk.All)/float64(GB), float64(disk.Used)/float64(GB)) if e != nil { logging.Log.Errorf("Could not send the alert: %v", e) } } logging.Log.Infof("Sleeping now") time.Sleep(waitInterval) } }