Skip to content

Commit

Permalink
make it easier to fork the project with custom commands (part 2) (#214)
Browse files Browse the repository at this point in the history
* make it easier to fork the project with custom commands (part 2)

* make it easier to fork the project with custom commands (part 2)
  • Loading branch information
brainexe authored Aug 21, 2021
1 parent d50a1b6 commit 91c4c7c
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ clean:

# download go dependencies into ./vendor/
dep:
go mod vendor
@go mod vendor

lint:
golangci-lint run
Expand Down
9 changes: 0 additions & 9 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,6 @@ func (b *Bot) loadChannels() (map[string]string, error) {
return channels, nil
}

// disconnectRTM will do a clean shutdown and kills all connections
func (b *Bot) disconnectRTM() error {
if b.slackClient.RTM != nil {
return b.slackClient.RTM.Disconnect()
}

return nil
}

// load the public channels and list of all users from current space
func (b *Bot) loadSlackData() error {
// whitelist users by group
Expand Down
23 changes: 17 additions & 6 deletions bot/listener.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package bot

import (
"os"
"os/signal"
"syscall"

"github.com/innogames/slack-bot/v2/bot/util"
"github.com/innogames/slack-bot/v2/client"
"github.com/innogames/slack-bot/v2/client/vcs"
Expand All @@ -12,9 +16,6 @@ import (

// Run is blocking method to handle new incoming events...from different sources
func (b *Bot) Run(ctx *util.ServerContext) {
ctx.RegisterChild()
defer ctx.ChildDone()

// listen for old/deprecated RTM connection
// https://api.slack.com/rtm
var rtmChan chan slack.RTMEvent
Expand All @@ -33,6 +34,10 @@ func (b *Bot) Run(ctx *util.ServerContext) {
// fetch all branches regularly
go vcs.InitBranchWatcher(&b.config, ctx)

// graceful shutdown via sigterm/sigint
stopChan := make(chan os.Signal, 2)
signal.Notify(stopChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)

for {
select {
case event := <-socketChan:
Expand All @@ -53,10 +58,16 @@ func (b *Bot) Run(ctx *util.ServerContext) {
// -> will post in same channel as the user posted the original command
message.InternalMessage = true
go b.processMessage(message, false)
case <-ctx.Done():
if err := b.disconnectRTM(); err != nil {
log.Error(err)
case <-stopChan:
// wait until other services are properly shut down
ctx.StopTheWorld()

if b.slackClient.RTM != nil {
if err := b.slackClient.RTM.Disconnect(); err != nil {
log.Error(err)
}
}

return
}
}
Expand Down
14 changes: 1 addition & 13 deletions cmd/bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/innogames/slack-bot/v2/bot"
"github.com/innogames/slack-bot/v2/bot/config"
Expand Down Expand Up @@ -57,17 +55,7 @@ func main() {

// start main loop!
ctx := util.NewServerContext()
go b.Run(ctx)

stopChan := make(chan os.Signal, 2)

signal.Notify(stopChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)

// listen for messages until we receive sigterm/sigint
<-stopChan

ctx.StopTheWorld()
log.Infof("Shutdown done, bye bye!")
b.Run(ctx)
}

func checkError(err error) {
Expand Down
5 changes: 4 additions & 1 deletion cmd/cli/integration_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// +build !windows

package main

import (
"io"
"syscall"
"testing"
"time"

Expand Down Expand Up @@ -40,7 +43,7 @@ func TestAll(t *testing.T) {

time.Sleep(time.Second * 2)

ctx.StopTheWorld()
syscall.Kill(syscall.Getpid(), syscall.SIGINT)

assert.Equal(t, expectedOutput.String(), output.String())
}
Expand Down
15 changes: 5 additions & 10 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ func main() {
}

func startCli(ctx *util.ServerContext, input io.Reader, output io.Writer, cfg config.Config) {
ctx.RegisterChild()
defer ctx.ChildDone()

// set an empty storage -> just store data in Ram
_ = storage.InitStorage("")

Expand All @@ -46,18 +43,14 @@ func startCli(ctx *util.ServerContext, input io.Reader, output io.Writer, cfg co
defer fakeSlack.Stop()

realBot := tester.StartBot(cfg)
go realBot.Run(ctx)

color.SetOutput(output)
color.Red.Print("Type in your command:\n")
reader := bufio.NewReader(input)

// loop to send stdin input to slack bot
for {
select {
case <-ctx.Done():
return
default:
go func() {
for {
text, err := reader.ReadString('\n')
if err != nil {
continue
Expand All @@ -72,5 +65,7 @@ func startCli(ctx *util.ServerContext, input io.Reader, output io.Writer, cfg co

client.HandleMessageWithDoneHandler(message).Wait()
}
}
}()

realBot.Run(ctx)
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/alicebob/miniredis/v2 v2.15.1
github.com/andygrunwald/go-jira v1.14.0
github.com/bndr/gojenkins v1.1.0
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/fsnotify/fsnotify v1.5.0 // indirect
github.com/gfleury/go-bitbucket-v1 v0.0.0-20210707202713-7d616f7c18ac
github.com/go-redis/redis/v7 v7.4.1
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
Expand Down Expand Up @@ -37,8 +37,8 @@ require (
github.com/texttheater/golang-levenshtein/levenshtein v0.0.0-20200805054039-cae8b0eaed6c
github.com/xanzy/go-gitlab v0.50.3
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
golang.org/x/oauth2 v0.0.0-20210810183815-faf39c7919d5
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912 // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f
golang.org/x/sys v0.0.0-20210820121016-41cdb8703e55 // indirect
golang.org/x/text v0.3.7
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.5.0 h1:NO5hkcB+srp1x6QmwvNZLeaOgbM8cmBTN32THzjvu2k=
github.com/fsnotify/fsnotify v1.5.0/go.mod h1:BX0DCEr5pT4jm2CnQdVP1lFV521fcCNcyEeNp4DQQDk=
github.com/gfleury/go-bitbucket-v1 v0.0.0-20210707202713-7d616f7c18ac h1:XrZTje1i6Q8Y03z5AApzVTwn1rkPKYp5iyEEAhd0CV8=
github.com/gfleury/go-bitbucket-v1 v0.0.0-20210707202713-7d616f7c18ac/go.mod h1:LB3osS9X2JMYmTzcCArHHLrndBAfcVLQAvUddfs+ONs=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down Expand Up @@ -426,6 +428,8 @@ golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4Iltr
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20210810183815-faf39c7919d5 h1:Ati8dO7+U7mxpkPSxBZQEvzHVUYB/MqCklCN8ig5w/o=
golang.org/x/oauth2 v0.0.0-20210810183815-faf39c7919d5/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f h1:Qmd2pbz05z7z6lm0DrgQVVPuBm92jqujBKMHMOlOQEw=
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -474,8 +478,11 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912 h1:uCLL3g5wH2xjxVREVuAbP9JM5PPKjRbXKRa6IBjkzmU=
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210820121016-41cdb8703e55 h1:rw6UNGRMfarCepjI8qOepea/SXwIBVfTKjztZ5gBbq4=
golang.org/x/sys v0.0.0-20210820121016-41cdb8703e55/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down

0 comments on commit 91c4c7c

Please sign in to comment.