Manager: make access to job compilers script thread-safe

When on-disk job compiler scripts are supported, they will be reloaded
often, and it becomes more important to have the access to the map of
loaded job compilers thread-safe.
This commit is contained in:
Sybren A. Stüvel 2022-06-20 18:09:33 +02:00
parent a833064fc1
commit a0e8eebcb3
2 changed files with 14 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
"context"
"errors"
"sort"
"sync"
"time"
"github.com/dop251/goja"
@ -26,6 +27,9 @@ type Service struct {
compilers map[string]Compiler // Mapping from job type name to the job compiler of that type.
registry *require.Registry // Goja module registry.
timeService TimeService
// mutex protects 'compilers' from race conditions.
mutex *sync.Mutex
}
type Compiler struct {
@ -52,6 +56,7 @@ func Load(ts TimeService) (*Service, error) {
service := Service{
compilers: map[string]Compiler{},
timeService: ts,
mutex: new(sync.Mutex),
}
if err := service.loadScripts(); err != nil {
@ -128,6 +133,11 @@ func (s *Service) Compile(ctx context.Context, sj api.SubmittedJob) (*AuthoredJo
// ListJobTypes returns the list of available job types.
func (s *Service) ListJobTypes() api.AvailableJobTypes {
jobTypes := make([]api.AvailableJobType, 0)
// Protect access to s.compilers.
s.mutex.Lock()
defer s.mutex.Unlock()
for typeName := range s.compilers {
compiler, err := s.compilerForJobType(typeName)
if err != nil {

View File

@ -22,7 +22,11 @@ func (s *Service) loadScripts() error {
return err
}
// Assign the new set of compilers in a thread-safe way.
s.mutex.Lock()
defer s.mutex.Unlock()
s.compilers = compilers
return nil
}