Подключите RabbitMQ и отдельный обработчик заданий
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Jobs struct {
|
||||
URL string
|
||||
Concurrency, MaxAttempts int
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
func LoadJobs(lookup Lookup) (Jobs, error) {
|
||||
url, err := secret(lookup, "BROKER_URL_FILE")
|
||||
if err != nil {
|
||||
return Jobs{}, err
|
||||
}
|
||||
c := Jobs{URL: url, Concurrency: 2, MaxAttempts: 5}
|
||||
seconds := 60
|
||||
for _, p := range []struct {
|
||||
key string
|
||||
value *int
|
||||
max int
|
||||
}{
|
||||
{"WORKER_CONCURRENCY", &c.Concurrency, 2},
|
||||
{"JOB_MAX_ATTEMPTS", &c.MaxAttempts, 100},
|
||||
{"JOB_TIMEOUT_SECONDS", &seconds, 3600},
|
||||
} {
|
||||
if s, ok := lookup(p.key); ok {
|
||||
n, err := strconv.Atoi(s)
|
||||
if err != nil || n < 1 || n > p.max {
|
||||
return Jobs{}, fmt.Errorf("%s must be between 1 and %d", p.key, p.max)
|
||||
}
|
||||
*p.value = n
|
||||
}
|
||||
}
|
||||
c.Timeout = time.Duration(seconds) * time.Second
|
||||
return c, nil
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLoadJobs(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "url")
|
||||
if err := os.WriteFile(path, []byte("amqp://worker:secret@broker:5672/\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
values := map[string]string{"BROKER_URL_FILE": path}
|
||||
lookup := func(k string) (string, bool) { v, ok := values[k]; return v, ok }
|
||||
cfg, err := LoadJobs(lookup)
|
||||
if err != nil || cfg.Timeout != 60*time.Second || cfg.MaxAttempts != 5 || cfg.Concurrency < 1 || cfg.URL != "amqp://worker:secret@broker:5672/" {
|
||||
t.Fatalf("unexpected defaults or secret read, err=%v", err)
|
||||
}
|
||||
for _, key := range []string{"JOB_TIMEOUT_SECONDS", "WORKER_CONCURRENCY", "JOB_MAX_ATTEMPTS"} {
|
||||
values[key] = "0"
|
||||
if _, err := LoadJobs(lookup); err == nil {
|
||||
t.Errorf("%s accepted zero", key)
|
||||
}
|
||||
delete(values, key)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user