|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "sync" |
| 7 | + |
| 8 | + // Packages |
| 9 | + server "github.com/mutablelogic/go-server" |
| 10 | + httpresponse "github.com/mutablelogic/go-server/pkg/httpresponse" |
| 11 | + types "github.com/mutablelogic/go-server/pkg/types" |
| 12 | +) |
| 13 | + |
| 14 | +//////////////////////////////////////////////////////////////////////////////// |
| 15 | +// TYPES |
| 16 | + |
| 17 | +type provider struct { |
| 18 | + // Map labels to plugins |
| 19 | + plugin map[string]server.Plugin |
| 20 | + |
| 21 | + // Map labels to tasks |
| 22 | + task map[string]*state |
| 23 | + |
| 24 | + // Order that the tasks were created |
| 25 | + order []string |
| 26 | + |
| 27 | + // Function to resolve plugin members |
| 28 | + resolver ResolverFunc |
| 29 | +} |
| 30 | + |
| 31 | +type state struct { |
| 32 | + server.Task |
| 33 | + context.Context |
| 34 | + context.CancelFunc |
| 35 | + sync.WaitGroup |
| 36 | +} |
| 37 | + |
| 38 | +// ResolverFunc is a function that resolves a plugin from label and plugin |
| 39 | +type ResolverFunc func(context.Context, string, server.Plugin) (server.Plugin, error) |
| 40 | + |
| 41 | +//////////////////////////////////////////////////////////////////////////////// |
| 42 | +// LIFECYCLE |
| 43 | + |
| 44 | +func New(resolver ResolverFunc, plugins ...server.Plugin) (*provider, error) { |
| 45 | + self := new(provider) |
| 46 | + self.plugin = make(map[string]server.Plugin, len(plugins)) |
| 47 | + self.task = make(map[string]*state, len(plugins)) |
| 48 | + self.order = make([]string, 0, len(plugins)) |
| 49 | + self.resolver = resolver |
| 50 | + |
| 51 | + // Add the plugins |
| 52 | + for _, plugin := range plugins { |
| 53 | + // Check plugin |
| 54 | + if plugin == nil { |
| 55 | + return nil, httpresponse.ErrInternalError.With("Plugin is nil") |
| 56 | + } else if !types.IsIdentifier(plugin.Name()) { |
| 57 | + return nil, httpresponse.ErrInternalError.Withf("Plugin name %q is not valid", plugin.Name()) |
| 58 | + } |
| 59 | + |
| 60 | + // TODO: Don't use names, use labels! |
| 61 | + label := plugin.Name() |
| 62 | + if _, exists := self.plugin[label]; exists { |
| 63 | + return nil, httpresponse.ErrInternalError.Withf("Plugin %q already exists", plugin.Name()) |
| 64 | + } else { |
| 65 | + self.plugin[label] = plugin |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Return success |
| 70 | + return self, nil |
| 71 | +} |
| 72 | + |
| 73 | +//////////////////////////////////////////////////////////////////////////////// |
| 74 | +// STRINGIFY |
| 75 | + |
| 76 | +func (provider *provider) String() string { |
| 77 | + type jtask struct { |
| 78 | + Name string `json:"name"` |
| 79 | + Description string `json:"description,omitempty"` |
| 80 | + Label string `json:"label,omitempty"` |
| 81 | + Plugin server.Plugin `json:"plugin,omitempty"` |
| 82 | + Task server.Task `json:"task,omitempty"` |
| 83 | + } |
| 84 | + result := make([]jtask, 0, len(provider.task)) |
| 85 | + for _, label := range provider.order { |
| 86 | + plugin := provider.plugin[label] |
| 87 | + result = append(result, jtask{ |
| 88 | + Name: plugin.Name(), |
| 89 | + Description: plugin.Description(), |
| 90 | + Label: label, |
| 91 | + Plugin: plugin, |
| 92 | + Task: provider.task[label].Task, |
| 93 | + }) |
| 94 | + } |
| 95 | + data, err := json.MarshalIndent(result, "", " ") |
| 96 | + if err != nil { |
| 97 | + return err.Error() |
| 98 | + } |
| 99 | + return string(data) |
| 100 | +} |
| 101 | + |
| 102 | +//////////////////////////////////////////////////////////////////////////////// |
| 103 | +// PUBLIC METHODS |
| 104 | + |
| 105 | +// Return a task from a label |
| 106 | +func (provider *provider) Task(ctx context.Context, label string) server.Task { |
| 107 | + provider.Print(ctx, "Called Task for ", label) |
| 108 | + |
| 109 | + // If the task is already created, then return it |
| 110 | + if task, exists := provider.task[label]; exists { |
| 111 | + return task.Task |
| 112 | + } |
| 113 | + |
| 114 | + // If the plugin doesn't exist, return nil |
| 115 | + plugin, exists := provider.plugin[label] |
| 116 | + if !exists { |
| 117 | + return nil |
| 118 | + } |
| 119 | + |
| 120 | + // Resolve the plugin |
| 121 | + if provider.resolver != nil { |
| 122 | + var err error |
| 123 | + plugin, err = provider.resolver(withProvider(ctx, provider), label, plugin) |
| 124 | + if err != nil { |
| 125 | + provider.Print(ctx, "Error: ", label, ": ", err) |
| 126 | + return nil |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Create the task |
| 131 | + task, err := plugin.New(withPath(ctx, label)) |
| 132 | + if err != nil { |
| 133 | + provider.Print(ctx, "Error: ", label, ": ", err) |
| 134 | + return nil |
| 135 | + } else if task == nil { |
| 136 | + provider.Print(ctx, "Error: ", label, ": ", httpresponse.ErrInternalError.With("Task is nil")) |
| 137 | + return nil |
| 138 | + } |
| 139 | + |
| 140 | + // Set the task and order |
| 141 | + provider.task[label] = &state{Task: task} |
| 142 | + provider.order = append(provider.order, label) |
| 143 | + |
| 144 | + // Return the task |
| 145 | + return task |
| 146 | +} |
| 147 | + |
| 148 | +//////////////////////////////////////////////////////////////////////////////// |
| 149 | +// PRIVATE METHODS |
| 150 | + |
| 151 | +// Make all tasks |
| 152 | +func (provider *provider) constructor(ctx context.Context) error { |
| 153 | + for label := range provider.plugin { |
| 154 | + if task := provider.Task(ctx, label); task == nil { |
| 155 | + return httpresponse.ErrConflict.Withf("Failed to create task %q", label) |
| 156 | + } |
| 157 | + } |
| 158 | + return nil |
| 159 | +} |
0 commit comments