Default to SQLite rather than Postgres & Redis (#72)
* Initial rough draft switch to sqlite. * Rewrote cache implemenation. * Provide typed tasks. * Task cleanup. * Use same db for tasks. * Provide task queue registration and service container injection. * Added optional delay to tasks. Pool buffers when encoding. * Added tests for the task client and runner. * Added handler examples for caching and tasks. * Cleanup and documentation. * Use make in workflow. * Updated documentation. * Updated documentation.
This commit is contained in:
parent
5e9e502b42
commit
a096abd195
29 changed files with 956 additions and 910 deletions
|
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
|
@ -12,6 +13,7 @@ import (
|
|||
|
||||
"github.com/mikestefanello/pagoda/pkg/handlers"
|
||||
"github.com/mikestefanello/pagoda/pkg/services"
|
||||
"github.com/mikestefanello/pagoda/pkg/tasks"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
@ -49,24 +51,25 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
if err := c.Web.StartServer(&srv); err != http.ErrServerClosed {
|
||||
if err := c.Web.StartServer(&srv); errors.Is(err, http.ErrServerClosed) {
|
||||
log.Fatalf("shutting down the server: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// Start the scheduler service to queue periodic tasks
|
||||
go func() {
|
||||
if err := c.Tasks.StartScheduler(); err != nil {
|
||||
log.Fatalf("scheduler shutdown: %v", err)
|
||||
}
|
||||
}()
|
||||
// Register all task queues
|
||||
tasks.Register(c)
|
||||
|
||||
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
|
||||
// Start the task runner to execute queued tasks
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go c.Tasks.StartRunner(ctx)
|
||||
|
||||
// Wait for interrupt signal to gracefully shut down the server with a timeout of 10 seconds.
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, os.Interrupt)
|
||||
signal.Notify(quit, os.Kill)
|
||||
<-quit
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
cancel()
|
||||
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := c.Web.Shutdown(ctx); err != nil {
|
||||
log.Fatal(err)
|
||||
|
|
|
|||
|
|
@ -1,45 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/mikestefanello/pagoda/config"
|
||||
"github.com/mikestefanello/pagoda/pkg/tasks"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Load the configuration
|
||||
cfg, err := config.GetConfig()
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("failed to load config: %v", err))
|
||||
}
|
||||
|
||||
// Build the worker server
|
||||
srv := asynq.NewServer(
|
||||
asynq.RedisClientOpt{
|
||||
Addr: fmt.Sprintf("%s:%d", cfg.Cache.Hostname, cfg.Cache.Port),
|
||||
DB: cfg.Cache.Database,
|
||||
Password: cfg.Cache.Password,
|
||||
},
|
||||
asynq.Config{
|
||||
// See asynq.Config for all available options and explanation
|
||||
Concurrency: 10,
|
||||
Queues: map[string]int{
|
||||
"critical": 6,
|
||||
"default": 3,
|
||||
"low": 1,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
// Map task types to the handlers
|
||||
mux := asynq.NewServeMux()
|
||||
mux.Handle(tasks.TypeExample, new(tasks.ExampleProcessor))
|
||||
|
||||
// Start the worker server
|
||||
if err := srv.Run(mux); err != nil {
|
||||
log.Fatalf("could not run worker server: %v", err)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue