|
1 | 1 | package pipeline
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
4 | 5 | "io/ioutil"
|
| 6 | + "strings" |
5 | 7 | "time"
|
6 | 8 |
|
7 | 9 | "github.com/gaia-pipeline/gaia"
|
8 | 10 | )
|
9 | 11 |
|
10 | 12 | const (
|
| 13 | + // tickerIntervalSeconds defines how often the ticker will tick. |
| 14 | + // Definition in seconds. |
11 | 15 | tickerIntervalSeconds = 5
|
12 | 16 | )
|
13 | 17 |
|
| 18 | +var ( |
| 19 | + // errMissingType is the error thrown when a pipeline is missing the type |
| 20 | + // in the file name. |
| 21 | + errMissingType = errors.New("couldnt find pipeline type definition") |
| 22 | +) |
| 23 | + |
14 | 24 | // InitTicker inititates the pipeline ticker.
|
15 | 25 | // This periodic job will check for new pipelines.
|
16 | 26 | func InitTicker() {
|
| 27 | + // Init global active pipelines slice |
| 28 | + GlobalActivePipelines = NewActivePipelines() |
| 29 | + |
| 30 | + // Check immediately to make sure we fill the list as fast as possible. |
| 31 | + checkActivePipelines() |
| 32 | + |
17 | 33 | // Create ticker
|
18 | 34 | ticker := time.NewTicker(tickerIntervalSeconds * time.Second)
|
19 |
| - quit := make(chan struct{}) |
20 |
| - |
21 |
| - // Actual ticker implementation |
22 | 35 | go func() {
|
23 | 36 | for {
|
24 | 37 | select {
|
25 | 38 | case <-ticker.C:
|
26 |
| - files, err := ioutil.ReadDir(gaia.Cfg.PipelinePath) |
27 |
| - if err != nil { |
28 |
| - gaia.Cfg.Logger.Error("cannot read pipelines folder", "error", err.Error(), "path", gaia.Cfg.PipelinePath) |
29 |
| - } else { |
30 |
| - // Iterate all found pipeline |
31 |
| - for _, file := range files { |
32 |
| - // TODO: Create for every file a pipeline object |
33 |
| - // and store it in a global pipeline array |
34 |
| - gaia.Cfg.Logger.Debug("pipeline found", "name", file.Name()) |
35 |
| - } |
36 |
| - } |
37 |
| - case <-quit: |
38 |
| - ticker.Stop() |
39 |
| - return |
| 39 | + checkActivePipelines() |
40 | 40 | }
|
41 | 41 | }
|
42 | 42 | }()
|
43 | 43 | }
|
| 44 | + |
| 45 | +// checkActivePipelines looks up all files in the pipeline folder. |
| 46 | +// Every file will be handled as an active pipeline and therefore |
| 47 | +// saved in the global active pipelines slice. |
| 48 | +func checkActivePipelines() { |
| 49 | + files, err := ioutil.ReadDir(gaia.Cfg.PipelinePath) |
| 50 | + if err != nil { |
| 51 | + gaia.Cfg.Logger.Error("cannot read pipelines folder", "error", err.Error(), "path", gaia.Cfg.PipelinePath) |
| 52 | + } else { |
| 53 | + // Iterate all found pipelines |
| 54 | + for _, file := range files { |
| 55 | + n := strings.TrimSpace(strings.ToLower(file.Name())) |
| 56 | + |
| 57 | + // Get pipeline type |
| 58 | + pType, err := getPipelineType(n) |
| 59 | + if err != nil { |
| 60 | + gaia.Cfg.Logger.Debug("at least one pipeline in pipeline folder is missing the type definition") |
| 61 | + gaia.Cfg.Logger.Debug("Info", "name", n) |
| 62 | + gaia.Cfg.Logger.Error("error thrown", "error", err.Error()) |
| 63 | + continue |
| 64 | + } |
| 65 | + |
| 66 | + // Get real pipeline name and check if the global active pipelines slice |
| 67 | + // already contains it. |
| 68 | + pName := getRealPipelineName(n, pType) |
| 69 | + if GlobalActivePipelines.Contains(pName) { |
| 70 | + continue |
| 71 | + } |
| 72 | + |
| 73 | + // Create pipeline object and fill it with information |
| 74 | + p := gaia.Pipeline{ |
| 75 | + Name: pName, |
| 76 | + Type: pType, |
| 77 | + Created: time.Now(), |
| 78 | + } |
| 79 | + |
| 80 | + // Append new pipeline |
| 81 | + GlobalActivePipelines.Append(p) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// getPipelineType looks up for specific suffix on the given file name. |
| 87 | +// If found, returns the pipeline type. |
| 88 | +func getPipelineType(n string) (gaia.PipelineType, error) { |
| 89 | + s := strings.Split(n, typeDelimiter) |
| 90 | + |
| 91 | + // Length must be higher than one |
| 92 | + if len(s) < 2 { |
| 93 | + return gaia.UNKNOWN, errMissingType |
| 94 | + } |
| 95 | + |
| 96 | + // Get last element and look for type |
| 97 | + t := s[len(s)-1] |
| 98 | + switch t { |
| 99 | + case gaia.GOLANG.String(): |
| 100 | + return gaia.GOLANG, nil |
| 101 | + } |
| 102 | + |
| 103 | + return gaia.UNKNOWN, errMissingType |
| 104 | +} |
| 105 | + |
| 106 | +// getRealPipelineName removes the suffix from the pipeline name. |
| 107 | +func getRealPipelineName(n string, pType gaia.PipelineType) string { |
| 108 | + return strings.TrimSuffix(n, typeDelimiter+pType.String()) |
| 109 | +} |
0 commit comments