Skip to content

Commit

Permalink
Handle potential race during conversation close (#4)
Browse files Browse the repository at this point in the history
* Avoid race while closing conversation

* Document bot env variables
  • Loading branch information
venkytv authored May 21, 2023
1 parent fe0c143 commit 0fa0eff
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
14 changes: 13 additions & 1 deletion BOT-WRITING-GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ See the [sample config file](examples/sample-config.yaml) for details on all
available options and defaults. The config files for the [examples](examples)
also illustrate most common config options.

## Environment variables

The bot will have access to the following environment variables:

* `BOTTERS_USER_ID`: ID of the bot user account; normally used to identify messages which mention the bot
* `BOTTERS_USER_NAME` User name of the bot user account
* `BOTTERS_CHANNEL`: Name of the channel this bot instance is running in
* `BOTTERS_CHANNEL_ID`: ID of the channel this bot instance is running in
* `BOTTERS_LOCALE`: Locale of the channel the bot is running in

See [gptbot](examples/gptbot/gptbot.py) for an example of how a bot might use these variables.

## Botters commands

Botters uses a `botters://` URL scheme for commands. A bot can include these
Expand All @@ -66,7 +78,7 @@ following commands are supported currently:
message. The follow-up message will automatically create the thread in
Slack.

For example on using Botters commands, see [gamebot](examples/gamebot).
For an example on using Botters commands, see [gamebot](examples/gamebot).

## Things to keep in mind

Expand Down
8 changes: 8 additions & 0 deletions conversation/conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type Conversation struct {
engineQueues engine.EngineQueues
prefixUsername bool
directMessagesOnly bool

// Flag to indicate that the conversation is closing
convClosing bool
}

func (c *Conversation) Start(ctx context.Context) {
Expand Down Expand Up @@ -62,6 +65,7 @@ func (c *Conversation) LaunchEngine(ctx context.Context) {
defer cancel()
defer func() {
logrus.Debug("Closing engine queues")
c.convClosing = true
close(c.engineQueues.ReadQ)
close(c.engineQueues.WriteQ)
}()
Expand Down Expand Up @@ -125,6 +129,10 @@ func (c *Conversation) LaunchEngine(ctx context.Context) {
}

func (c *Conversation) Post(m *message.Message) {
if c.convClosing {
logrus.Debug("Conversation is closing, not posting message: %#v: %s", c, m.Text)
return
}
msg := m.Text
if c.prefixUsername {
msg = m.User + ": " + msg
Expand Down
2 changes: 2 additions & 0 deletions conversation/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ func (cm *Manager) GetConversations(ctx context.Context, m *message.Message) []*
}
cm.triggerLock.RUnlock()

logrus.Debugf("Found %d conversations for message %s", len(conversations), m.Text)

return conversations
}

Expand Down

0 comments on commit 0fa0eff

Please sign in to comment.