task locking

main
alex 2023-06-02 10:59:47 +02:00
parent 6cb469fdb3
commit 3e952c2921
7 changed files with 54 additions and 7 deletions

View File

@ -262,6 +262,9 @@ func GetAllGroupTasksSteps() []structs.GroupTaskSteps {
database.DB.Find(&groupTaskStepsLogs) database.DB.Find(&groupTaskStepsLogs)
// TODO: locked state
// groupTaskStepsLogs[0].LockedByUserId = cache...
return groupTaskStepsLogs return groupTaskStepsLogs
} }
@ -282,10 +285,10 @@ func GetCategoryGroupTaskByCategoryAndGroupId(category string, groupId string) s
func StartUnlockLockedGroupTaskStepsTicker() { func StartUnlockLockedGroupTaskStepsTicker() {
ticker := time.NewTicker(1 * time.Second) ticker := time.NewTicker(1 * time.Second)
for _ = range ticker.C { for range ticker.C {
for index, taskStep := range cache.GetLockedGroupTaskSteps() { for index, taskStep := range cache.GetLockedGroupTaskSteps() {
if time.Since(taskStep.LockedAt).Seconds() > 3 { if time.Since(taskStep.LockedAt).Seconds() > 3 {
log.Debug().Msgf("Unlocked task step", index) log.Debug().Msgf("Unlocked task step %v", index)
cache.RemoveLockedGroupTaskStep(index) cache.RemoveLockedGroupTaskStep(index)
socketclients.BroadcastMessageExceptUserSessionId(taskStep.LockedByUserSession, socketclients.BroadcastMessageExceptUserSessionId(taskStep.LockedByUserSession,

13
modules/cache/grouptaskstepsinput.go vendored Normal file
View File

@ -0,0 +1,13 @@
package cache
import (
"janex/admin-dashboard-backend/modules/structs"
"sync"
)
/*
This stores the inputs that a user makes when typing into the input fields at a Group Task step.
This storage is necessary so that other users when opening this GroupTask or reloading the browser receive the data entered by the one user.
*/
var groupTaskStepsInputs []structs.GroupTaskStepsInput
var gtsi sync.RWMutex

View File

@ -2,8 +2,11 @@ package cache
import ( import (
"janex/admin-dashboard-backend/modules/structs" "janex/admin-dashboard-backend/modules/structs"
"janex/admin-dashboard-backend/modules/utils"
"sync" "sync"
"time" "time"
"github.com/rs/zerolog/log"
) )
var lockedGroupTaskSteps []*structs.LockedGroupTaskSteps var lockedGroupTaskSteps []*structs.LockedGroupTaskSteps
@ -42,3 +45,20 @@ func GetLockedGroupTaskSteps() []*structs.LockedGroupTaskSteps {
return lockedGroupTaskSteps return lockedGroupTaskSteps
} }
func ExtendLockedGroupTaskStepLockedAtTime(lockedGroupTaskStep structs.LockedGroupTaskSteps) {
lgtMu.Lock()
log.Debug().Msg("start extending")
for i, step := range lockedGroupTaskSteps {
if step.GroupTaskId == lockedGroupTaskStep.GroupTaskId {
step.LockedAt = time.Now().Add(utils.GroupTaskLockedTime * time.Second)
lockedGroupTaskSteps[i] = step
log.Debug().Msg("time extended")
break
}
}
lgtMu.Unlock()
}

View File

@ -81,7 +81,6 @@ type TaskParameter struct {
} }
// used for ui when a user is writing into input field to lock the task step for other users // used for ui when a user is writing into input field to lock the task step for other users
type LockedGroupTaskSteps struct { type LockedGroupTaskSteps struct {
LockedByUserId string LockedByUserId string
LockedByUserSession string // user session is needed to prevent sending the unlocking message to the user who are writing LockedByUserSession string // user session is needed to prevent sending the unlocking message to the user who are writing
@ -89,3 +88,11 @@ type LockedGroupTaskSteps struct {
Step uint8 Step uint8
LockedAt time.Time LockedAt time.Time
} }
// used for ui when a user types into input fields to sync to other users and show them this when opening the group task view
type GroupTaskStepsInput struct {
GroupTaskId string
Step string
ParameterName string
Value string
}

View File

@ -22,7 +22,7 @@ func AddScanner(c *fiber.Ctx) error {
} }
if err := utils.ValidateStruct(body); err != nil { if err := utils.ValidateStruct(body); err != nil {
log.Error().Msgf("Failed to validate body, err: %s", err) log.Error().Msgf("Failed to validate body, err: %v", err)
return c.SendStatus(fiber.StatusBadRequest) return c.SendStatus(fiber.StatusBadRequest)
} }

View File

@ -73,7 +73,7 @@ func RunHub() {
continue continue
} }
log.Info().Msgf("Received message: %s", receivedMessage, receivedMessage.Cmd) log.Info().Msgf("Received message: %v %v", receivedMessage, receivedMessage.Cmd)
switch receivedMessage.Cmd { switch receivedMessage.Cmd {
case utils.ReceivedCmdStartGroupTasks: case utils.ReceivedCmdStartGroupTasks:
@ -178,10 +178,14 @@ func RunHub() {
Cmd: utils.SentCmdUpdateGroupTaskStepUserInputValue, Cmd: utils.SentCmdUpdateGroupTaskStepUserInputValue,
Body: receivedMessage.Body, Body: receivedMessage.Body,
}) })
cache.ExtendLockedGroupTaskStepLockedAtTime(structs.LockedGroupTaskSteps{
GroupTaskId: receivedMessage.Body["groupTaskId"].(string),
})
break break
default: default:
log.Error().Msgf("Received unknown message: %s", receivedMessage) log.Error().Msgf("Received unknown message: %v", receivedMessage)
break break
} }

View File

@ -31,7 +31,7 @@ func WebSocketServer(app *fiber.App) {
if messageType == websocket.TextMessage { if messageType == websocket.TextMessage {
broadcast <- structs.SocketMessage{Conn: c, Msg: msg} broadcast <- structs.SocketMessage{Conn: c, Msg: msg}
} else { } else {
log.Error().Msgf("websocket message received of type %s", messageType) log.Error().Msgf("websocket message received of type %v", messageType)
} }
} }
})) }))