Created
June 14, 2023 21:18
-
-
Save adampresley/9c93454213f76af95b56e18ee1bd6b65 to your computer and use it in GitHub Desktop.
Work Ticker - main.go
This file contains hidden or 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
| package main | |
| import ( | |
| "context" | |
| "fmt" | |
| "time" | |
| "github.com/app-nerds/kit/v6/workticker" | |
| "github.com/sirupsen/logrus" | |
| "go.uber.org/ratelimit" | |
| ) | |
| type Thing struct { | |
| ID string | |
| Name string | |
| } | |
| func main() { | |
| logger := logrus.New().WithField("app", "Sample Work Ticker") | |
| errorChan := make(chan workticker.HandleWorkError[[]Thing]) | |
| workTicker := workticker.NewWorkTicker(workticker.WorkTickerConfig[[]Thing]{ | |
| Name: "Sample Implementation Ticker", | |
| Logger: logger, | |
| NumWorkers: 3, | |
| RateLimitPerSecond: 10, | |
| TickFrequency: time.Millisecond * 200, | |
| WorkConfiguration: workticker.WorkConfiguration[[]Thing]{ | |
| Handler: GetWorkProcessor(logger), | |
| Retriever: GetWorkRetriever(logger), | |
| }, | |
| WorkErrorChan: errorChan, | |
| }) | |
| ctx, cancel := context.WithCancel(context.Background()) | |
| go func() { | |
| for { | |
| select { | |
| case err := <-errorChan: | |
| logger.WithFields(logrus.Fields{ | |
| "error": err.ErrorMessage, | |
| "data": err.Data, | |
| }).Error("Error handling work. This is where you might do something meaningful.") | |
| case <-ctx.Done(): | |
| break | |
| } | |
| } | |
| }() | |
| go workTicker.Run(ctx) | |
| time.Sleep(time.Second * 5) | |
| cancel() | |
| } | |
| func GetWorkProcessor[T []Thing](logger *logrus.Entry) workticker.WorkHandler[T] { | |
| iteration := -1 | |
| return func(workerID int, data T, limiter ratelimit.Limiter) error { | |
| iteration++ | |
| if iteration > 2 { | |
| return fmt.Errorf("error processing work") | |
| } | |
| logger.WithFields(logrus.Fields{ | |
| "workerID": workerID, | |
| "data": data, | |
| }).Info("In the work handler. This is where you might do something meaningful.") | |
| return nil | |
| } | |
| } | |
| /* | |
| GetWorkRetriever returns a function that returns work items. In this mock implementation | |
| we are returning a couple of different datasets based on our current iteration. In a real | |
| implementation, you might be pulling data from a database or some other source. | |
| */ | |
| func GetWorkRetriever[T []Thing](logger *logrus.Entry) workticker.WorkRetriever[T] { | |
| iteration := -1 | |
| return func(handler workticker.WorkHandler[T]) (workticker.WorkItem[T], error) { | |
| iteration++ | |
| if iteration == 0 { | |
| return workticker.WorkItem[T]{ | |
| Data: []Thing{ | |
| {ID: "1", Name: "Thing 1"}, | |
| {ID: "2", Name: "Thing 2"}, | |
| }, | |
| Handler: handler, | |
| }, nil | |
| } | |
| if iteration == 1 { | |
| return workticker.WorkItem[T]{ | |
| Data: []Thing{ | |
| {ID: "3", Name: "Thing 3"}, | |
| {ID: "4", Name: "Thing 4"}, | |
| }, | |
| Handler: handler, | |
| }, nil | |
| } | |
| return workticker.WorkItem[T]{ | |
| Data: []Thing{ | |
| {ID: "5", Name: "Thing 5"}, | |
| {ID: "6", Name: "Thing 6"}, | |
| }, | |
| Handler: handler, | |
| }, nil | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment