-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
137 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package fx | ||
|
||
import ( | ||
"github.com/satont/twitch-notifier/internal/messagesender" | ||
"github.com/satont/twitch-notifier/internal/messagesender/temporal" | ||
"go.uber.org/fx" | ||
) | ||
|
||
var Module = fx.Options( | ||
fx.Provide( | ||
temporal.NewActivity, | ||
temporal.NewWorkflow, | ||
fx.Annotate(temporal.NewImpl, fx.As(new(messagesender.MessageSender))), | ||
), | ||
fx.Invoke(temporal.NewWorker), | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,23 @@ | ||
package temporal | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/satont/twitch-notifier/internal/messagesender" | ||
"go.uber.org/fx" | ||
) | ||
|
||
type ActivityOpts struct { | ||
fx.In | ||
} | ||
|
||
func NewActivity() *Activity { | ||
return &Activity{} | ||
} | ||
|
||
type Activity struct { | ||
} | ||
|
||
func (c *Activity) SendTelegram(ctx context.Context, opts messagesender.TelegramOpts) error { | ||
return nil | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package temporal | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/satont/twitch-notifier/pkg/logger" | ||
"go.temporal.io/sdk/client" | ||
"go.temporal.io/sdk/log" | ||
"go.temporal.io/sdk/worker" | ||
"go.uber.org/fx" | ||
) | ||
|
||
type WorkerOpts struct { | ||
fx.In | ||
LC fx.Lifecycle | ||
|
||
Workflow *Workflow | ||
Activity *Activity | ||
Logger logger.Logger | ||
} | ||
|
||
func NewWorker(opts WorkerOpts) error { | ||
temporalClient, err := client.Dial( | ||
client.Options{ | ||
Logger: log.NewStructuredLogger(opts.Logger.GetSlog()), | ||
}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
w := worker.New(temporalClient, queueName, worker.Options{}) | ||
|
||
w.RegisterWorkflow(opts.Workflow.SendTelegram) | ||
w.RegisterActivity(opts.Activity.SendTelegram) | ||
|
||
opts.LC.Append( | ||
fx.Hook{ | ||
OnStart: func(ctx context.Context) error { | ||
return w.Start() | ||
}, | ||
OnStop: func(ctx context.Context) error { | ||
w.Stop() | ||
temporalClient.Close() | ||
return nil | ||
}, | ||
}, | ||
) | ||
|
||
return nil | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,65 @@ | ||
package temporal | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/satont/twitch-notifier/internal/messagesender" | ||
"github.com/satont/twitch-notifier/pkg/logger" | ||
"go.temporal.io/sdk/temporal" | ||
"go.temporal.io/sdk/workflow" | ||
"go.uber.org/fx" | ||
) | ||
|
||
type WorkflowOpts struct { | ||
fx.In | ||
|
||
Logger logger.Logger | ||
Logger logger.Logger | ||
Activity *Activity | ||
} | ||
|
||
func NewWorkflow(opts WorkflowOpts) *Workflow { | ||
return &Workflow{} | ||
return &Workflow{ | ||
logger: opts.Logger, | ||
activity: opts.Activity, | ||
} | ||
} | ||
|
||
type Workflow struct { | ||
logger logger.Logger | ||
activity *Activity | ||
} | ||
|
||
const telegramActivityMaximumAttempts = 1 | ||
|
||
func (c *Workflow) SendTelegram(ctx workflow.Context, opts messagesender.TelegramOpts) error { | ||
ao := workflow.ActivityOptions{ | ||
TaskQueue: queueName, | ||
StartToCloseTimeout: 10 * time.Second, | ||
RetryPolicy: &temporal.RetryPolicy{ | ||
MaximumInterval: 15 * time.Second, | ||
MaximumAttempts: telegramActivityMaximumAttempts, | ||
NonRetryableErrorTypes: nil, | ||
}, | ||
} | ||
ctx = workflow.WithActivityOptions(ctx, ao) | ||
|
||
log := workflow.GetLogger(ctx) | ||
log.Info("Sending message", "chatId", opts.ServiceChatID) | ||
|
||
err := workflow.ExecuteActivity( | ||
ctx, | ||
c.activity.SendTelegram, | ||
opts, | ||
).Get( | ||
ctx, | ||
nil, | ||
) | ||
if err != nil { | ||
log.Error("Send failed", "Error", err) | ||
return err | ||
} | ||
|
||
log.Info("Message sent") | ||
|
||
return nil | ||
} |
This file contains 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