|
1 | 1 | package pluginlookup |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "sync" |
5 | 6 |
|
6 | 7 | "github.com/riverqueue/river/rivertype" |
@@ -86,26 +87,28 @@ func NewPluginLookup(plugins []rivertype.Plugin) PluginLookupInterface { |
86 | 87 | } |
87 | 88 |
|
88 | 89 | // NormalizePlugins converts hook, middleware, and plugin registrations into a |
89 | | -// single plugin slice. Hook and middleware registrations must already satisfy |
90 | | -// rivertype.Plugin to participate. |
| 90 | +// single plugin slice while preserving legacy hook and middleware registrations |
| 91 | +// that don't yet opt into Plugin. |
91 | 92 | func NormalizePlugins(hooks []rivertype.Hook, middlewares []rivertype.Middleware, plugins []rivertype.Plugin) []rivertype.Plugin { |
92 | 93 | normalizedPlugins := make([]rivertype.Plugin, 0, len(hooks)+len(middlewares)+len(plugins)) |
93 | 94 |
|
94 | 95 | for _, hook := range hooks { |
95 | 96 | if plugin, ok := hook.(rivertype.Plugin); ok { |
96 | 97 | normalizedPlugins = append(normalizedPlugins, plugin) |
| 98 | + continue |
97 | 99 | } |
| 100 | + |
| 101 | + normalizedPlugins = append(normalizedPlugins, newHookPlugin(hook)) |
98 | 102 | } |
99 | 103 | for _, middleware := range middlewares { |
100 | 104 | if plugin, ok := middleware.(rivertype.Plugin); ok { |
101 | 105 | normalizedPlugins = append(normalizedPlugins, plugin) |
| 106 | + continue |
102 | 107 | } |
| 108 | + |
| 109 | + normalizedPlugins = append(normalizedPlugins, newMiddlewarePlugin(middleware)) |
103 | 110 | } |
104 | | - for _, plugin := range plugins { |
105 | | - if plugin != nil { |
106 | | - normalizedPlugins = append(normalizedPlugins, plugin) |
107 | | - } |
108 | | - } |
| 111 | + normalizedPlugins = append(normalizedPlugins, plugins...) |
109 | 112 |
|
110 | 113 | return normalizedPlugins |
111 | 114 | } |
@@ -141,6 +144,89 @@ func (c *emptyPluginLookup) ByKind(kind PluginKind) []rivertype.Plugin { |
141 | 144 | return nil |
142 | 145 | } |
143 | 146 |
|
| 147 | +// |
| 148 | +// hookPlugin |
| 149 | +// |
| 150 | + |
| 151 | +// hookPlugin wraps a legacy hook so it can participate in plugin lookup. |
| 152 | +type hookPlugin struct { |
| 153 | + hook rivertype.Hook |
| 154 | +} |
| 155 | + |
| 156 | +func newHookPlugin(hook rivertype.Hook) rivertype.Plugin { |
| 157 | + return &hookPlugin{hook: hook} |
| 158 | +} |
| 159 | + |
| 160 | +func (p *hookPlugin) IsPlugin() bool { return true } |
| 161 | +func (p *hookPlugin) IsHook() bool { return true } |
| 162 | + |
| 163 | +func (p *hookPlugin) InsertBegin(ctx context.Context, params *rivertype.JobInsertParams) error { |
| 164 | + hook, ok := p.hook.(rivertype.HookInsertBegin) |
| 165 | + if !ok { |
| 166 | + return nil |
| 167 | + } |
| 168 | + return hook.InsertBegin(ctx, params) |
| 169 | +} |
| 170 | + |
| 171 | +func (p *hookPlugin) Start(ctx context.Context, params *rivertype.HookPeriodicJobsStartParams) error { |
| 172 | + hook, ok := p.hook.(rivertype.HookPeriodicJobsStart) |
| 173 | + if !ok { |
| 174 | + return nil |
| 175 | + } |
| 176 | + return hook.Start(ctx, params) |
| 177 | +} |
| 178 | + |
| 179 | +func (p *hookPlugin) WorkBegin(ctx context.Context, job *rivertype.JobRow) error { |
| 180 | + hook, ok := p.hook.(rivertype.HookWorkBegin) |
| 181 | + if !ok { |
| 182 | + return nil |
| 183 | + } |
| 184 | + return hook.WorkBegin(ctx, job) |
| 185 | +} |
| 186 | + |
| 187 | +func (p *hookPlugin) WorkEnd(ctx context.Context, job *rivertype.JobRow, err error) error { |
| 188 | + hook, ok := p.hook.(rivertype.HookWorkEnd) |
| 189 | + if !ok { |
| 190 | + return err |
| 191 | + } |
| 192 | + return hook.WorkEnd(ctx, job, err) |
| 193 | +} |
| 194 | + |
| 195 | +// |
| 196 | +// middlewarePlugin |
| 197 | +// |
| 198 | + |
| 199 | +// middlewarePlugin wraps a legacy middleware so it can participate in plugin |
| 200 | +// lookup. |
| 201 | +type middlewarePlugin struct { |
| 202 | + middleware rivertype.Middleware |
| 203 | +} |
| 204 | + |
| 205 | +func newMiddlewarePlugin(middleware rivertype.Middleware) rivertype.Plugin { |
| 206 | + return &middlewarePlugin{middleware: middleware} |
| 207 | +} |
| 208 | + |
| 209 | +func (p *middlewarePlugin) IsPlugin() bool { return true } |
| 210 | +func (p *middlewarePlugin) IsMiddleware() bool { |
| 211 | + return true |
| 212 | +} |
| 213 | + |
| 214 | +func (p *middlewarePlugin) InsertMany(ctx context.Context, manyParams []*rivertype.JobInsertParams, doInner func(context.Context) ([]*rivertype.JobInsertResult, error)) ([]*rivertype.JobInsertResult, error) { |
| 215 | + middleware, ok := p.middleware.(rivertype.JobInsertMiddleware) |
| 216 | + if !ok { |
| 217 | + return doInner(ctx) |
| 218 | + } |
| 219 | + return middleware.InsertMany(ctx, manyParams, doInner) |
| 220 | +} |
| 221 | + |
| 222 | +func (p *middlewarePlugin) Work(ctx context.Context, job *rivertype.JobRow, doInner func(context.Context) error) error { |
| 223 | + middleware, ok := p.middleware.(rivertype.WorkerMiddleware) |
| 224 | + if !ok { |
| 225 | + return doInner(ctx) |
| 226 | + } |
| 227 | + return middleware.Work(ctx, job, doInner) |
| 228 | +} |
| 229 | + |
144 | 230 | // |
145 | 231 | // JobPluginLookup |
146 | 232 | // |
|
0 commit comments