-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
d60cc6e
commit ccf8e49
Showing
4 changed files
with
222 additions
and
42 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,8 @@ | ||
version: "3" | ||
|
||
services: | ||
redis: | ||
image: redis:alpine | ||
container_name: asyncer-redis-example | ||
ports: | ||
- 6379:6379 |
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,60 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/dmitrymomot/asyncer" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
const ( | ||
redisAddr = "redis://localhost:6379/0" | ||
TestTaskName = "queued_task" | ||
) | ||
|
||
type TestTaskPayload struct { | ||
Name string | ||
} | ||
|
||
// test task handler function | ||
func testTaskHandler(ctx context.Context, payload TestTaskPayload) error { | ||
fmt.Printf("Hello, %s!\n", payload.Name) | ||
return nil | ||
} | ||
|
||
func main() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
eg, _ := errgroup.WithContext(ctx) | ||
|
||
// Run a new queue server with redis as the broker. | ||
eg.Go(asyncer.RunQueueServer( | ||
ctx, redisAddr, nil, | ||
// Register a handler for the task. | ||
asyncer.HandlerFunc[TestTaskPayload](TestTaskName, testTaskHandler), | ||
// ... add more handlers here ... | ||
)) | ||
|
||
// Create a new enqueuer with redis as the broker. | ||
enqueuer := asyncer.MustNewEnqueuer(redisAddr) | ||
defer enqueuer.Close() | ||
|
||
// Enqueue a task with payload. | ||
// The task will be processed after immediately. | ||
for i := 0; i < 10; i++ { | ||
if err := enqueuer.EnqueueTask(ctx, TestTaskName, TestTaskPayload{ | ||
Name: fmt.Sprintf("Test %d", i), | ||
}); err != nil { | ||
panic(err) | ||
} | ||
time.Sleep(500 * time.Millisecond) | ||
} | ||
|
||
// Wait for the queue server to exit. | ||
if err := eg.Wait(); err != nil { | ||
panic(err) | ||
} | ||
} |
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 main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/dmitrymomot/asyncer" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
const ( | ||
redisAddr = "redis://localhost:6379/0" | ||
TestTaskName = "scheduled_task" | ||
) | ||
|
||
type TestTaskPayload struct { | ||
Name string | ||
} | ||
|
||
// test task handler function | ||
func testTaskHandler(ctx context.Context) error { | ||
fmt.Println("scheduled test task handler called at", time.Now().Format(time.RFC3339)) | ||
return nil | ||
} | ||
|
||
func main() { | ||
eg, ctx := errgroup.WithContext(context.Background()) | ||
|
||
// Run a new queue server with redis as the broker. | ||
eg.Go(asyncer.RunQueueServer( | ||
ctx, redisAddr, nil, | ||
// Register a handler for the task. | ||
asyncer.ScheduledHandlerFunc(TestTaskName, testTaskHandler), | ||
// ... add more handlers here ... | ||
)) | ||
|
||
// Run a scheduler with redis as the broker. | ||
// The scheduler will schedule tasks to be enqueued at a specified time. | ||
eg.Go(asyncer.RunSchedulerServer( | ||
ctx, redisAddr, nil, | ||
// Schedule the scheduled_task task to be enqueued every 1 seconds. | ||
asyncer.NewTaskScheduler("@every 1s", TestTaskName), | ||
// ... add more scheduled tasks here ... | ||
)) | ||
|
||
// Wait for the queue server to exit. | ||
if err := eg.Wait(); err != nil { | ||
panic(err) | ||
} | ||
} |