Refactor taggers. Refine Job Hooks. Start job classifier

This commit is contained in:
2025-05-22 07:10:41 +02:00
parent 9abc206d1a
commit ca634bb707
14 changed files with 316 additions and 61 deletions

View File

@@ -19,11 +19,6 @@ import (
"github.com/ClusterCockpit/cc-backend/pkg/schema"
)
const (
tagType = "app"
appPath = "./var/tagger/apps"
)
//go:embed apps/*
var appFiles embed.FS
@@ -33,7 +28,9 @@ type appInfo struct {
}
type AppTagger struct {
apps map[string]appInfo
apps map[string]appInfo
tagType string
cfgPath string
}
func (t *AppTagger) scanApp(f fs.File, fns string) {
@@ -53,7 +50,7 @@ func (t *AppTagger) EventMatch(s string) bool {
// FIXME: Only process the file that caused the event
func (t *AppTagger) EventCallback() {
files, err := os.ReadDir(appPath)
files, err := os.ReadDir(t.cfgPath)
if err != nil {
log.Fatal(err)
}
@@ -61,7 +58,7 @@ func (t *AppTagger) EventCallback() {
for _, fn := range files {
fns := fn.Name()
log.Debugf("Process: %s", fns)
f, err := os.Open(fmt.Sprintf("%s/%s", appPath, fns))
f, err := os.Open(fmt.Sprintf("%s/%s", t.cfgPath, fns))
if err != nil {
log.Errorf("error opening app file %s: %#v", fns, err)
}
@@ -70,6 +67,9 @@ func (t *AppTagger) EventCallback() {
}
func (t *AppTagger) Register() error {
t.cfgPath = "./var/tagger/apps"
t.tagType = "app"
files, err := appFiles.ReadDir("apps")
if err != nil {
return fmt.Errorf("error reading app folder: %#v", err)
@@ -79,28 +79,25 @@ func (t *AppTagger) Register() error {
fns := fn.Name()
log.Debugf("Process: %s", fns)
f, err := appFiles.Open(fmt.Sprintf("apps/%s", fns))
defer f.Close()
if err != nil {
return fmt.Errorf("error opening app file %s: %#v", fns, err)
}
t.scanApp(f, fns)
}
if util.CheckFileExists(appPath) {
if util.CheckFileExists(t.cfgPath) {
t.EventCallback()
log.Infof("Setup file watch for %s", appPath)
util.AddListener(appPath, t)
log.Infof("Setup file watch for %s", t.cfgPath)
util.AddListener(t.cfgPath, t)
}
return nil
}
func (t *AppTagger) Match(job *schema.Job) {
func (t *AppTagger) Match(job *schema.JobMeta) {
r := repository.GetJobRepository()
meta, err := r.FetchMetadata(job)
if err != nil {
log.Error("cannot fetch meta data")
}
jobscript, ok := meta["jobScript"]
jobscript, ok := job.MetaData["jobScript"]
if ok {
id := job.ID
@@ -109,8 +106,8 @@ func (t *AppTagger) Match(job *schema.Job) {
tag := a.tag
for _, s := range a.strings {
if strings.Contains(jobscript, s) {
if !r.HasTag(id, tagType, tag) {
r.AddTagOrCreateDirect(id, tagType, tag)
if !r.HasTag(*id, t.tagType, tag) {
r.AddTagOrCreateDirect(*id, t.tagType, tag)
break out
}
}