MCPcopy
hub / github.com/google/mtail / LoadAllPrograms

Method LoadAllPrograms

internal/runtime/runtime.go:49–100  ·  view source on GitHub ↗

LoadAllPrograms loads all programs in a directory and starts watching the directory for filesystem changes. Any compile errors are stored for later retrieival. This function returns an error if an internal error occurs.

()

Source from the content-addressed store, hash-verified

47// directory for filesystem changes. Any compile errors are stored for later retrieival.
48// This function returns an error if an internal error occurs.
49func (r *Runtime) LoadAllPrograms() error {
50 if r.programPath == "" {
51 glog.V(2).Info("Programpath is empty, loading nothing")
52 return nil
53 }
54 s, err := os.Stat(r.programPath)
55 if err != nil {
56 return errors.Wrapf(err, "failed to stat %q", r.programPath)
57 }
58 switch {
59 case s.IsDir():
60 dirents, rerr := os.ReadDir(r.programPath)
61 if rerr != nil {
62 return errors.Wrapf(rerr, "Failed to list programs in %q", r.programPath)
63 }
64
65 markDeleted := make(map[string]struct{})
66 r.handleMu.RLock()
67 for name := range r.handles {
68 glog.Infof("added %s", name)
69 markDeleted[name] = struct{}{}
70 }
71 r.handleMu.RUnlock()
72 for _, dirent := range dirents {
73 if dirent.IsDir() {
74 continue
75 }
76 err = r.LoadProgram(filepath.Join(r.programPath, dirent.Name()))
77 if err != nil {
78 if r.errorsAbort {
79 return err
80 }
81 glog.Warning(err)
82 }
83 glog.Infof("unmarking %s", filepath.Base(dirent.Name()))
84 delete(markDeleted, filepath.Base(dirent.Name()))
85 }
86 for name := range markDeleted {
87 glog.Infof("unloading %s", name)
88 r.UnloadProgram(name)
89 }
90 default:
91 err = r.LoadProgram(r.programPath)
92 if err != nil {
93 if r.errorsAbort {
94 return err
95 }
96 glog.Warning(err)
97 }
98 }
99 return nil
100}
101
102// LoadProgram loads or reloads a program from the full pathname programPath. The name of
103// the program is the basename of the file.

Callers 1

NewFunction · 0.95

Calls 2

LoadProgramMethod · 0.95
UnloadProgramMethod · 0.95

Tested by

no test coverage detected