-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a task manager that can run different tasks given a unique ID. This is needed to accommodate expensive tasks like importing a large repository. The current behavior uses the connection's context (the SSH connection) to import the repository. However, if the server has defined an SSH `idle_timeout`, `max_timeout`, and/or the connection drops, Soft Serve cancels the git clone process and aborts importing the repository. Instead, we add the import task to the "task manager" and wait on the connection context. If a task already exists for the same repository, return `Error: import already in progress`. Fixes: #348
- Loading branch information
1 parent
c7829a3
commit c4dde1c
Showing
4 changed files
with
224 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package task | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
var ( | ||
// ErrNotFound is returned when a process is not found. | ||
ErrNotFound = errors.New("task not found") | ||
|
||
// ErrAlreadyStarted is returned when a process is already started. | ||
ErrAlreadyStarted = errors.New("task already started") | ||
) | ||
|
||
// Task is a task that can be started and stopped. | ||
type Task struct { | ||
id string | ||
fn func(context.Context) error | ||
started atomic.Bool | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
err error | ||
} | ||
|
||
// Manager manages tasks. | ||
type Manager struct { | ||
m sync.Map | ||
ctx context.Context | ||
} | ||
|
||
// NewManager returns a new task manager. | ||
func NewManager(ctx context.Context) *Manager { | ||
return &Manager{ | ||
m: sync.Map{}, | ||
ctx: ctx, | ||
} | ||
} | ||
|
||
// Add adds a task to the manager. | ||
// If the process already exists, it is a no-op. | ||
func (m *Manager) Add(id string, fn func(context.Context) error) { | ||
if m.Exists(id) { | ||
return | ||
} | ||
|
||
ctx, cancel := context.WithCancel(m.ctx) | ||
m.m.Store(id, &Task{ | ||
id: id, | ||
fn: fn, | ||
ctx: ctx, | ||
cancel: cancel, | ||
}) | ||
} | ||
|
||
// Stop stops the task and removes it from the manager. | ||
func (m *Manager) Stop(id string) error { | ||
v, ok := m.m.Load(id) | ||
if !ok { | ||
return ErrNotFound | ||
} | ||
|
||
p := v.(*Task) | ||
p.cancel() | ||
|
||
m.m.Delete(id) | ||
return nil | ||
} | ||
|
||
// Exists checks if a task exists. | ||
func (m *Manager) Exists(id string) bool { | ||
_, ok := m.m.Load(id) | ||
return ok | ||
} | ||
|
||
// Run starts the task if it exists. | ||
// Otherwise, it waits for the process to finish. | ||
func (m *Manager) Run(id string, done chan<- error) { | ||
v, ok := m.m.Load(id) | ||
if !ok { | ||
done <- ErrNotFound | ||
return | ||
} | ||
|
||
p := v.(*Task) | ||
if p.started.Load() { | ||
<-p.ctx.Done() | ||
if p.err != nil { | ||
done <- p.err | ||
return | ||
} | ||
|
||
done <- p.ctx.Err() | ||
} | ||
|
||
p.started.Store(true) | ||
m.m.Store(id, p) | ||
defer p.cancel() | ||
defer m.m.Delete(id) | ||
|
||
errc := make(chan error, 1) | ||
go func(ctx context.Context) { | ||
errc <- p.fn(ctx) | ||
}(p.ctx) | ||
|
||
select { | ||
case <-m.ctx.Done(): | ||
done <- m.ctx.Err() | ||
case err := <-errc: | ||
p.err = err | ||
m.m.Store(id, p) | ||
done <- err | ||
} | ||
} |