Fix server file browse flow
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/subtle"
|
||||
"fmt"
|
||||
"sort"
|
||||
@@ -85,6 +86,68 @@ func (svc *CoreService) ClaimRunJob(claim domain.RunJobClaim) (domain.RunJobClai
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (svc *CoreService) ClaimRunJobWithWait(ctx context.Context, claim domain.RunJobClaim) (domain.RunJobClaimResult, error) {
|
||||
claim = domain.CopyRunJobClaim(claim)
|
||||
result, err := svc.ClaimRunJob(claim)
|
||||
if err != nil || result.HasJob || claim.WaitSeconds <= 0 || runJobClaimAtCapacity(claim) {
|
||||
return result, err
|
||||
}
|
||||
waiter := svc.registerRunJobWaiter(claim.RunEndpointID)
|
||||
defer svc.unregisterRunJobWaiter(claim.RunEndpointID, waiter)
|
||||
result, err = svc.ClaimRunJob(claim)
|
||||
if err != nil || result.HasJob {
|
||||
return result, err
|
||||
}
|
||||
timer := time.NewTimer(time.Duration(claim.WaitSeconds) * time.Second)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return domain.RunJobClaimResult{}, ctx.Err()
|
||||
case <-waiter:
|
||||
case <-timer.C:
|
||||
}
|
||||
return svc.ClaimRunJob(claim)
|
||||
}
|
||||
|
||||
func runJobClaimAtCapacity(claim domain.RunJobClaim) bool {
|
||||
return claim.Capacity.MaxJobs > 0 && claim.Capacity.RunningJobs >= claim.Capacity.MaxJobs
|
||||
}
|
||||
|
||||
func (svc *CoreService) registerRunJobWaiter(runEndpointID string) chan struct{} {
|
||||
waiter := make(chan struct{})
|
||||
svc.jobWaitMu.Lock()
|
||||
svc.jobWaiters[runEndpointID] = append(svc.jobWaiters[runEndpointID], waiter)
|
||||
svc.jobWaitMu.Unlock()
|
||||
return waiter
|
||||
}
|
||||
|
||||
func (svc *CoreService) unregisterRunJobWaiter(runEndpointID string, waiter chan struct{}) {
|
||||
svc.jobWaitMu.Lock()
|
||||
waiters := svc.jobWaiters[runEndpointID]
|
||||
for index, candidate := range waiters {
|
||||
if candidate == waiter {
|
||||
waiters = append(waiters[:index], waiters[index+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(waiters) == 0 {
|
||||
delete(svc.jobWaiters, runEndpointID)
|
||||
} else {
|
||||
svc.jobWaiters[runEndpointID] = waiters
|
||||
}
|
||||
svc.jobWaitMu.Unlock()
|
||||
}
|
||||
|
||||
func (svc *CoreService) notifyRunJobWaiters(runEndpointID string) {
|
||||
svc.jobWaitMu.Lock()
|
||||
waiters := svc.jobWaiters[runEndpointID]
|
||||
delete(svc.jobWaiters, runEndpointID)
|
||||
svc.jobWaitMu.Unlock()
|
||||
for _, waiter := range waiters {
|
||||
close(waiter)
|
||||
}
|
||||
}
|
||||
|
||||
func withoutCapability(capabilities []string, forbidden string) []string {
|
||||
filtered := make([]string, 0, len(capabilities))
|
||||
for _, capability := range capabilities {
|
||||
@@ -570,7 +633,11 @@ func (svc *CoreService) updateScheduledJob(job domain.Job) error {
|
||||
if err := validator.ValidateJob(job); err != nil {
|
||||
return err
|
||||
}
|
||||
return svc.store.Jobs().Update(job)
|
||||
if err := svc.store.Jobs().Update(job); err != nil {
|
||||
return err
|
||||
}
|
||||
svc.notifyRunJobWaiters(job.RunEndpointID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func normalizeJobScheduling(job domain.Job, stamp time.Time) domain.Job {
|
||||
|
||||
Reference in New Issue
Block a user