Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

期限切れのセッションを非同期に削除するジョブ #31

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion backend/app/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,31 @@ package main

import (
"context"
"log"
"sudoku/config"
"sudoku/infra/gorm"
"sudoku/job"
"sudoku/server"
"time"
)

// NOTE: ctx を正しく使えてない気がする
func main() {
cfg := config.NewEnvConfig()

db, err := gorm.NewGormConnection()
if err != nil {
log.Fatalln(err)
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

server.Run(ctx)
sessionCleanupJob := job.NewSessionCleanupJob(
gorm.NewSessionRepository(db),
time.Hour, // TODO: env から読む?
)
go sessionCleanupJob.Run(ctx)

server.Run(ctx, cfg, db)
}
5 changes: 5 additions & 0 deletions backend/app/infra/gorm/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"gorm.io/gorm"
"sudoku/model"
"sudoku/repository"
"time"
)

var _ repository.ISessionRepository = (*SessionRepository)(nil)
Expand Down Expand Up @@ -49,3 +50,7 @@ func (r *SessionRepository) Save(session *model.Session) error {

return nil
}

func (r *SessionRepository) DeleteExpired() error {
return r.db.Where("expires_at < ?", time.Now()).Delete(&Session{}).Error
}
41 changes: 41 additions & 0 deletions backend/app/job/session_cleanup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package job

import (
"context"
"log"
"sudoku/repository"
"time"
)

type SessionCleanupJob struct {
sessionRepo repository.ISessionRepository
interval time.Duration
}

func NewSessionCleanupJob(sessionRepo repository.ISessionRepository, interval time.Duration) *SessionCleanupJob {
return &SessionCleanupJob{
sessionRepo: sessionRepo,
interval: interval,
}
}

func (j *SessionCleanupJob) Run(ctx context.Context) {
log.Println("Starting session cleanup job")

ticker := time.NewTicker(j.interval)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
log.Println("Session cleanup job stopped")
return
case <-ticker.C:
if err := j.sessionRepo.DeleteExpired(); err != nil {
log.Printf("Error during session cleanup: %v", err)
} else {
log.Println("Session cleanup completed successfully")
}
}
}
}
14 changes: 14 additions & 0 deletions backend/app/repository/mock_session.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/app/repository/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type ISessionRepository interface {
FindByID(id uuid.UUID) (mo.Option[*model.Session], error)
// Save 新規レコード作成の場合, 採番された ID をセットする
Save(session *model.Session) error
DeleteExpired() error
}
13 changes: 2 additions & 11 deletions backend/app/server/route/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package route

import (
"connectrpc.com/connect"
"log"
"gorm.io/gorm"
"net/http"
"sudoku/config"
"sudoku/gen/sudoku/auth/v1/authv1connect"
Expand All @@ -13,16 +13,7 @@ import (
authS "sudoku/service/auth"
)

func Register(mux *http.ServeMux) {
cfg := config.NewEnvConfig()

// infrastructure
// ここで初期化するべきか?
db, err := gormRepo.NewGormConnection()
if err != nil {
log.Fatalln(err)
}

func Register(mux *http.ServeMux, cfg config.EnvConfig, db *gorm.DB) {
// construct services
authService := authS.NewService(
authS.ServiceConfig{
Expand Down
11 changes: 9 additions & 2 deletions backend/app/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import (
"errors"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"gorm.io/gorm"
"log"
"net/http"
"os"
"os/signal"
"sudoku/config"
"sudoku/handler/middleware"
"sudoku/server/route"
"time"
)

func Run(ctx context.Context) {
// cfg, db は引数でもらうべきなのか?
func Run(ctx context.Context, cfg config.EnvConfig, db *gorm.DB) {
mux := http.NewServeMux()
route.Register(mux)
route.Register(mux, cfg, db)

// TODO: 引数で設定を受け取る
address := ":3000"
Expand All @@ -29,6 +32,10 @@ func Run(ctx context.Context) {
),
}

runServer(ctx, srv)
}

func runServer(ctx context.Context, srv *http.Server) {
done := make(chan error, 1)
go func() {
done <- srv.ListenAndServe()
Expand Down
Loading