Initial rough draft switch to sqlite.

This commit is contained in:
mikestefanello 2024-06-18 14:27:39 -04:00
parent a70003d290
commit ab55705b9f
15 changed files with 315 additions and 553 deletions

View file

@ -3,8 +3,10 @@ package main
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"log"
"log/slog"
"net/http"
"os"
"os/signal"
@ -12,6 +14,7 @@ import (
"github.com/mikestefanello/pagoda/pkg/handlers"
"github.com/mikestefanello/pagoda/pkg/services"
"github.com/mikestefanello/pagoda/pkg/tasks"
)
func main() {
@ -54,12 +57,17 @@ func main() {
}
}()
// Start the scheduler service to queue periodic tasks
go func() {
if err := c.Tasks.StartScheduler(); err != nil {
log.Fatalf("scheduler shutdown: %v", err)
c.Tasks.Register(tasks.TypeExample, func(ctx context.Context, m []byte) error {
var t tasks.ExampleTask
if err := json.Unmarshal(m, &t); err != nil {
return err
}
}()
slog.Info("Example task received", "message", t.Message)
return nil
})
// Start the scheduler service to queue periodic tasks
c.Tasks.StartRunner(context.Background()) // use main context
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
quit := make(chan os.Signal, 1)

View file

@ -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)
}
}