Skip to content

Commit

Permalink
errgroup追加
Browse files Browse the repository at this point in the history
  • Loading branch information
walnuts1018 committed Apr 11, 2024
1 parent 3234ffb commit 37c30c4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ require (
require (
github.com/gorilla/websocket v1.4.2 // indirect
github.com/itchyny/timefmt-go v0.1.5 // indirect
golang.org/x/sync v0.7.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
27 changes: 21 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package main

import (
"context"
"fmt"
"log/slog"
"os"
"time"

"github.com/kmc-jp/inviteallmcg/config"
"github.com/kmc-jp/inviteallmcg/slack"
"github.com/lmittmann/tint"
"golang.org/x/sync/errgroup"
)

func main() {
Expand All @@ -28,12 +30,25 @@ func main() {

slackClient := slack.NewSlackClient(cfg)

go func() {
slackClient.HandleSlackEvents(ctx)
}()

if err := slackClient.Listen(ctx); err != nil {
slog.Error("Error listening", "error", err)
eg := new(errgroup.Group)

eg.Go(func() error {
err := slackClient.HandleSlackEvents(ctx)
if err != nil {
return fmt.Errorf("error handling slack events: %w", err)
}
return nil
})

eg.Go(func() error {
if err := slackClient.Listen(ctx); err != nil {
return fmt.Errorf("error listening to slack: %w", err)
}
return nil
})

if err := eg.Wait(); err != nil {
slog.Error("Error running", "error", err)
os.Exit(1)
}
}
5 changes: 3 additions & 2 deletions slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,14 @@ func (c *Client) ForwardMessage(ctx context.Context, everythingChannelID string,
return err
}

func (c *Client) HandleSlackEvents(ctx context.Context) {
func (c *Client) HandleSlackEvents(ctx context.Context) error {
for event := range c.socketUserClient.Events {
slog.Debug("Event", "event", event)
switch event.Type {
case socketmode.EventTypeConnecting:
slog.Info("Connecting to Slack with Socket Mode...")
case socketmode.EventTypeConnectionError:
slog.Error("Connection error", "error", event.Data)
return fmt.Errorf("connection error: %v", event.Data)
case socketmode.EventTypeConnected:
slog.Info("Connected to Slack with Socket Mode")
case socketmode.EventTypeEventsAPI:
Expand Down Expand Up @@ -429,6 +429,7 @@ func (c *Client) HandleSlackEvents(ctx context.Context) {
}

slog.Info("End handling channel join event")
return nil
}

func (c *Client) Listen(ctx context.Context) error {
Expand Down

0 comments on commit 37c30c4

Please sign in to comment.