Skip to content

Commit

Permalink
Merge pull request #31 from EringiShimeji/auth/session-cleanup
Browse files Browse the repository at this point in the history
期限切れのセッションを非同期に削除するジョブ
  • Loading branch information
harsssh authored Aug 26, 2024
2 parents 8036eaf + 9de61e0 commit d3c98f5
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 14 deletions.
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

0 comments on commit d3c98f5

Please sign in to comment.