Created
March 21, 2017 00:51
-
-
Save panki/54f8a5c5dfb9c1074c49e5d9fc22b7ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func (b *bot) runLoop(ctx *schema.Ctx) { | |
| b.log.Info(ctx, "Initializing bot, channel id=%d", b.channelId) | |
| init: | |
| retryCounter := 0 | |
| for { | |
| err := b.init(ctx) | |
| if err == nil { | |
| break | |
| } | |
| retryCounter++ | |
| retryTimeout := retry.Delay(retryCounter) | |
| b.log.Info(ctx, "Failed to initialize, retry in %ds, error=%v", retryTimeout/time.Second, err) | |
| select { | |
| case <-b.stop: | |
| goto stop | |
| case <-time.After(retryTimeout): | |
| continue | |
| } | |
| } | |
| b.log.Info(ctx, "Bot initialized, channel id=%d", b.channelId) | |
| retryCounter = 0 | |
| for { | |
| err := b.sendNextMessage(ctx) | |
| delay := time.Duration(0) | |
| switch { | |
| case err == sql.ErrNoRows: | |
| retryCounter = 0 | |
| delay = time.Second * 30 | |
| case err == nil: | |
| retryCounter = 0 | |
| default: | |
| retryCounter++ | |
| delay = retry.Delay(retryCounter) | |
| } | |
| select { | |
| case <-bot.stop: | |
| goto stop | |
| case <-bot.configChanged: | |
| goto init | |
| case <-bot.send: | |
| if retryCounter == 0 { | |
| continue | |
| } | |
| case <-time.After(delay): | |
| continue | |
| } | |
| } | |
| stop: | |
| b.log.Info(ctx, "Bot stopped, channel id=%d", b.channelId) | |
| close(b.stopped) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment