From 41bb9fa5938125217a075c60f1e39dc3a9a27537 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Mon, 21 Jun 2021 17:01:39 +0300 Subject: - Rework dispatcher, pipeline, job (not completely) Create a config sample with RR2 support. Progress on root JOBS plugin. Signed-off-by: Valery Piashchynski --- plugins/jobs/dispatcher.go | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 plugins/jobs/dispatcher.go (limited to 'plugins/jobs/dispatcher.go') diff --git a/plugins/jobs/dispatcher.go b/plugins/jobs/dispatcher.go new file mode 100644 index 00000000..9fde8fac --- /dev/null +++ b/plugins/jobs/dispatcher.go @@ -0,0 +1,47 @@ +package jobs + +import ( + "strings" +) + +var separators = []string{"/", "-", "\\"} + +// Dispatcher provides ability to automatically locate the pipeline for the specific job +// and update job options (if none set). +type Dispatcher map[string]*Options + +// pre-compile patterns +func initDispatcher(routes map[string]*Options) Dispatcher { + dispatcher := make(Dispatcher) + for pattern, opts := range routes { + pattern = strings.ToLower(pattern) + pattern = strings.Trim(pattern, "-.*") + + for _, s := range separators { + pattern = strings.Replace(pattern, s, ".", -1) + } + + dispatcher[pattern] = opts + } + + return dispatcher +} + +// match clarifies target job pipeline and other job options. Can return nil. +func (dispatcher Dispatcher) match(job *Job) (found *Options) { + var best = 0 + + jobName := strings.ToLower(job.Job) + for pattern, opts := range dispatcher { + if strings.HasPrefix(jobName, pattern) && len(pattern) > best { + found = opts + best = len(pattern) + } + } + + if best == 0 { + return nil + } + + return found +} -- cgit v1.2.3