Skip to content

Commit

Permalink
共通化出来る処理を共通化した
Browse files Browse the repository at this point in the history
  • Loading branch information
p1ass committed Aug 10, 2020
1 parent 2fc69da commit 80c07d2
Showing 1 changed file with 43 additions and 52 deletions.
95 changes: 43 additions & 52 deletions usecase/session_timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (s *SessionTimerUseCase) handleTrackEnd(ctx context.Context, sessionID stri
return false, nil
}

// handleTrackEnd はある一曲の再生が終わったときの処理を行います
// handleNext は曲がスキップされたときの処理を行います
func (s *SessionTimerUseCase) handleNext(ctx context.Context, sessionID string) (bool, error) {
triggerAfterTrackEndResponse, err := s.sessionRepo.DoInTx(ctx, s.handleNextTx(sessionID))
if v, ok := triggerAfterTrackEndResponse.(*handleTrackEndResponse); ok {
Expand Down Expand Up @@ -204,18 +204,8 @@ func (s *SessionTimerUseCase) handleTrackEndTx(sessionID string) func(ctx contex
}
}()

// 曲の再生中にArchivedになった場合
if sess.StateType == entity.Archived {

s.pusher.Push(&event.PushMessage{
SessionID: sess.ID,
Msg: entity.EventArchived,
})

return &handleTrackEndResponse{
nextTrack: false,
err: nil,
}, nil
return s.handleArchiveInTransaction(sessionID)
}

if err := sess.GoNextTrack(); err != nil && errors.Is(err, entity.ErrSessionAllTracksFinished) {
Expand All @@ -226,20 +216,9 @@ func (s *SessionTimerUseCase) handleTrackEndTx(sessionID string) func(ctx contex
}, nil
}

track := sess.TrackURIShouldBeAddedWhenHandleTrackEnd()
if track != "" {
if err := s.playerCli.Enqueue(ctx, track, sess.DeviceID); err != nil {
s.handleInterrupt(sess)
if err := s.sessionRepo.Update(ctx, sess); err != nil {
logger.Errorj(map[string]interface{}{
"message": "handleWaitTimerExpired: failed to update session after Enqueue and handleInterrupt",
"sessionID": sessionID,
"error": err.Error(),
})
return &handleTrackEndResponse{err: err}, fmt.Errorf("failed to enqueue track")
}
return &handleTrackEndResponse{nextTrack: false, err: nil}, nil
}
res, err := s.enqueueTrackInTransaction(ctx, sess)
if res != nil {
return res, err
}

logger.Debugj(map[string]interface{}{"message": "next track", "sessionID": sess.ID, "queueHead": sess.QueueHead})
Expand All @@ -248,7 +227,7 @@ func (s *SessionTimerUseCase) handleTrackEndTx(sessionID string) func(ctx contex
}
}

// handleTrackEndTx はINTERRUPTになってerrorを帰す場合もトランザクションをコミットして欲しいので、
// handleNextTx はINTERRUPTになってerrorを帰す場合もトランザクションをコミットして欲しいので、
// アプリケーションエラーはhandleTrackEndResponseのフィールドで返すようにしてerrorの返り値はnilにしている
func (s *SessionTimerUseCase) handleNextTx(sessionID string) func(ctx context.Context) (interface{}, error) {
logger := log.New()
Expand All @@ -272,18 +251,8 @@ func (s *SessionTimerUseCase) handleNextTx(sessionID string) func(ctx context.Co
return &handleTrackEndResponse{nextTrack: false}, fmt.Errorf("GoNextTrack: %w", err)
}

// 曲の再生中にArchivedになった場合
if sess.StateType == entity.Archived {

s.pusher.Push(&event.PushMessage{
SessionID: sess.ID,
Msg: entity.EventArchived,
})

return &handleTrackEndResponse{
nextTrack: false,
err: nil,
}, nil
return s.handleArchiveInTransaction(sessionID)
}

if err := sess.GoNextTrack(); err != nil && errors.Is(err, entity.ErrSessionAllTracksFinished) {
Expand All @@ -294,20 +263,9 @@ func (s *SessionTimerUseCase) handleNextTx(sessionID string) func(ctx context.Co
}, nil
}

track := sess.TrackURIShouldBeAddedWhenHandleTrackEnd()
if track != "" {
if err := s.playerCli.Enqueue(ctx, track, sess.DeviceID); err != nil {
s.handleInterrupt(sess)
if err := s.sessionRepo.Update(ctx, sess); err != nil {
logger.Errorj(map[string]interface{}{
"message": "handleWaitTimerExpired: failed to update session after Enqueue and handleInterrupt",
"sessionID": sessionID,
"error": err.Error(),
})
return &handleTrackEndResponse{err: err}, fmt.Errorf("failed to enqueue track")
}
return &handleTrackEndResponse{nextTrack: false, err: nil}, nil
}
res, err := s.enqueueTrackInTransaction(ctx, sess)
if res != nil {
return res, err
}

logger.Debugj(map[string]interface{}{"message": "next track", "sessionID": sess.ID, "queueHead": sess.QueueHead})
Expand All @@ -316,6 +274,39 @@ func (s *SessionTimerUseCase) handleNextTx(sessionID string) func(ctx context.Co
}
}

func (s *SessionTimerUseCase) handleArchiveInTransaction(sessionID string) (*handleTrackEndResponse, error) {
s.pusher.Push(&event.PushMessage{
SessionID: sessionID,
Msg: entity.EventArchived,
})

return &handleTrackEndResponse{
nextTrack: false,
err: nil,
}, nil
}

func (s *SessionTimerUseCase) enqueueTrackInTransaction(ctx context.Context, sess *entity.Session) (*handleTrackEndResponse, error) {
logger := log.New()

track := sess.TrackURIShouldBeAddedWhenHandleTrackEnd()
if track != "" {
if err := s.playerCli.Enqueue(ctx, track, sess.DeviceID); err != nil {
s.handleInterrupt(sess)
if err := s.sessionRepo.Update(ctx, sess); err != nil {
logger.Errorj(map[string]interface{}{
"message": "handleWaitTimerExpired: failed to update session after Enqueue and handleInterrupt",
"sessionID": sess.ID,
"error": err.Error(),
})
return &handleTrackEndResponse{err: err}, fmt.Errorf("failed to enqueue track")
}
return &handleTrackEndResponse{nextTrack: false, err: nil}, nil
}
}
return nil, nil
}

// handleAllTrackFinish はキューの全ての曲の再生が終わったときの処理を行います。
func (s *SessionTimerUseCase) handleAllTrackFinish(sess *entity.Session) {
logger := log.New()
Expand Down

0 comments on commit 80c07d2

Please sign in to comment.