-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create jobs queue for scheduling new builds
- Loading branch information
Martin Sehnoutka
committed
Sep 24, 2019
1 parent
7df735e
commit cc671b3
Showing
5 changed files
with
220 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package queue | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
// Manifest contains additional metadata attached do a pipeline that are necessary for workers | ||
type Manifest struct { | ||
destination string | ||
} | ||
|
||
// SaveImage saves "src" into provided destination | ||
func (m *Manifest) SaveImage(src string) error { | ||
BUFFERSIZE := 4096 // Magic :) | ||
|
||
sourceFileStat, err := os.Stat(src) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !sourceFileStat.Mode().IsRegular() { | ||
return fmt.Errorf("%s is not a regular file", src) | ||
} | ||
|
||
source, err := os.Open(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer source.Close() | ||
|
||
_, err = os.Stat(m.destination) | ||
if err == nil { | ||
return fmt.Errorf("file %s already exists", m.destination) | ||
} | ||
|
||
destination, err := os.Create(m.destination) | ||
if err != nil { | ||
return err | ||
} | ||
defer destination.Close() | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
buf := make([]byte, BUFFERSIZE) | ||
for { | ||
n, err := source.Read(buf) | ||
if err != nil && err != io.EOF { | ||
return err | ||
} | ||
if n == 0 { | ||
break | ||
} | ||
|
||
if _, err := destination.Write(buf[:n]); err != nil { | ||
return err | ||
} | ||
} | ||
return err | ||
} |
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,75 @@ | ||
package queue | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
// Build is a request waiting for a worker | ||
type Build struct { | ||
Pipeline string `json:"pipeline"` | ||
Manifest string `json:"manifest"` | ||
} | ||
|
||
// Job is an image build already in progress | ||
type Job struct { | ||
UUID string `json:"uuid"` | ||
WorkerID string `json:"worker-id"` | ||
Build Build `json:"build"` | ||
} | ||
|
||
// JobQueue contains already running jobs waiting for | ||
type JobQueue struct { | ||
sync.Mutex | ||
incomingBuilds chan Build | ||
queue []Job | ||
timeoutSeconds int | ||
} | ||
|
||
// NewJobQueue creates object of type JobQueue | ||
func NewJobQueue(timeout int, builds chan Build) *JobQueue { | ||
return &JobQueue{ | ||
incomingBuilds: builds, | ||
queue: make([]Job, 10), | ||
timeoutSeconds: timeout, | ||
} | ||
} | ||
|
||
// StartNewJob starts a new job | ||
func (j *JobQueue) StartNewJob(id string, worker string) Job { | ||
newBuild := <-j.incomingBuilds | ||
job := Job{ | ||
UUID: id, | ||
WorkerID: worker, | ||
Build: newBuild, | ||
} | ||
|
||
j.Lock() | ||
j.queue = append(j.queue, job) | ||
go func() { | ||
var jobs *JobQueue = j | ||
// just to make it explicit, that we want a pointer | ||
// to that queue | ||
time.Sleep(time.Duration(jobs.timeoutSeconds) * time.Second) | ||
stillRunning := false | ||
idx := 0 | ||
jobs.Lock() | ||
for i := range jobs.queue { | ||
// I we iterated over elements in the queue, Go would copy each | ||
// element into the iterator, therefore we iterate over indexes only | ||
if jobs.queue[i].UUID == id { | ||
stillRunning = true | ||
idx = i | ||
} | ||
} | ||
if stillRunning { | ||
// Reschedule the build | ||
jobs.incomingBuilds <- jobs.queue[idx].Build | ||
// Skip this element in the queue => remove it | ||
copy(jobs.queue[idx:], jobs.queue[idx+1:]) | ||
} | ||
jobs.Unlock() | ||
}() | ||
j.Unlock() | ||
return job | ||
} |
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,31 @@ | ||
package queue_test | ||
|
||
import ( | ||
"osbuild-composer/queue" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestJobTimeout(t *testing.T) { | ||
builds := make(chan queue.Build, 10) | ||
jobs := queue.NewJobQueue(1, builds) | ||
b := queue.Build{ | ||
Pipeline: "pipeline", | ||
Manifest: "manifest", | ||
} | ||
builds <- b | ||
jobs.StartNewJob("uuid1", "worker1") | ||
time.Sleep(2 * time.Second) | ||
select { | ||
case build, ok := <-builds: | ||
if !ok { | ||
t.Error("Channel is not supposed to be closed.") | ||
} else { | ||
if build.Pipeline != "pipeline" || build.Manifest != "manifest" { | ||
t.Error("Unexpected build in the channel.") | ||
} | ||
} | ||
default: | ||
t.Error("Channel is not supposed to be empty.") | ||
} | ||
} |