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:
parent
a833064fc1
commit
a0e8eebcb3
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user