-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(464): add support for queueing async actions in background (#544)
* feat(464): add support for queueing async actions in background * Tidy deps * Fix bug in using goroutines for running async actions * Add log message to see if Redis is enabled for publishing async tasks * Add default values for the config options --------- Co-authored-by: Mostafa Moradian <[email protected]>
- Loading branch information
Showing
13 changed files
with
442 additions
and
346 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
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,40 @@ | ||
package act | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/redis/go-redis/v9" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
type IPublisher interface { | ||
Publish(ctx context.Context, payload []byte) error | ||
} | ||
|
||
var _ IPublisher = (*Publisher)(nil) | ||
|
||
type Publisher struct { | ||
Logger zerolog.Logger | ||
RedisDB redis.Cmdable | ||
ChannelName string | ||
} | ||
|
||
func NewPublisher(publisher Publisher) (*Publisher, error) { | ||
if err := publisher.RedisDB.Ping(context.Background()).Err(); err != nil { | ||
publisher.Logger.Err(err).Msg("failed to connect redis") | ||
} | ||
return &Publisher{ | ||
Logger: publisher.Logger, | ||
RedisDB: publisher.RedisDB, | ||
ChannelName: publisher.ChannelName, | ||
}, nil | ||
} | ||
|
||
func (p *Publisher) Publish(ctx context.Context, payload []byte) error { | ||
if err := p.RedisDB.Publish(ctx, p.ChannelName, payload).Err(); err != nil { | ||
p.Logger.Err(err).Str("ChannelName", p.ChannelName).Msg("failed to publish task to redis") | ||
return fmt.Errorf("failed to publish task to redis: %w", err) | ||
} | ||
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
Oops, something went wrong.