Skip to content

Commit

Permalink
Cleanup channels if engine dies uncleanly
Browse files Browse the repository at this point in the history
Also add sample bot demonstrating timed messages
  • Loading branch information
venkytv committed Apr 26, 2023
1 parent 5907d86 commit e52561d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 deletions.
9 changes: 5 additions & 4 deletions conversation/conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func (c *Conversation) Start(ctx context.Context) {
func (c *Conversation) LaunchEngine(ctx context.Context) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
defer func() {
logrus.Debug("Closing engine queues")
close(c.engineQueues.ReadQ)
close(c.engineQueues.WriteQ)
}()

stdin, stdout, stderr, err := c.engine.Setup(ctx)
if err != nil {
Expand Down Expand Up @@ -108,10 +113,6 @@ func (c *Conversation) LaunchEngine(ctx context.Context) {
logrus.Error("Engine failed:", err)
return
}

// Close engine queues
close(c.engineQueues.ReadQ)
close(c.engineQueues.WriteQ)
}

func (c *Conversation) Post(m *message.Message) {
Expand Down
4 changes: 2 additions & 2 deletions conversation/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ func (cm *Manager) GetConversations(ctx context.Context, m *message.Message) []*
conversations = append(conversations, &c)
logrus.Debugf("New channel conversation with %s: %+v", config.Name, c)
} else {
logrus.Debugf("Ignoring trigger as bot already active: channel='%s' msg='%s' trigger='%s'",
c.channelName, m.Text, re.String())
logrus.Debugf("Ignoring trigger as bot already active: %s: channel='%s' msg='%s' trigger='%s'",
config.Name, c.channelName, m.Text, re.String())
}
}

Expand Down
16 changes: 16 additions & 0 deletions examples/cuckoobot/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CONFIG_DIR = $(HOME)/teabot-engines

all:
@echo Run \"make install\" to install the cuckoobot engine

install: $(CONFIG_DIR) $(CONFIG_DIR)/cuckoobot.yaml $(CONFIG_DIR)/cuckoobot.sh

$(CONFIG_DIR)/cuckoobot.yaml: cuckoobot.yaml.tmpl
sed -e 's|@CONFIG_DIR@|$(CONFIG_DIR)|' $< > $@

$(CONFIG_DIR)/cuckoobot.sh: cuckoobot.sh
cp $< $@
chmod 755 $@

$(CONFIG_DIR):
mkdir -p $@
19 changes: 19 additions & 0 deletions examples/cuckoobot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Basic bot engine

A simple bot which prints the message "cuckoo" on the hour every hour in any
channel it is invited into.

### Installation

```
make install
```

This copies the bot's config as well as the bot script to the
default config directory: `~/teabot-engines`.

If `teabot` is already running, make it reload its engines:

```
pkill -HUP teabot
```
9 changes: 9 additions & 0 deletions examples/cuckoobot/cuckoobot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

while :; do
# Say cuckoo every hour
if ! (( `date +'%M'` % 60 )); then
echo "cuckoo"
fi
sleep 60
done
3 changes: 3 additions & 0 deletions examples/cuckoobot/cuckoobot.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
handler: @CONFIG_DIR@/cuckoobot.sh
threaded: false
direct-messages-only: false

0 comments on commit e52561d

Please sign in to comment.