mirror of
https://github.com/safing/portbase
synced 2026-05-04 22:52:05 +00:00
Add module template, update run method
This commit is contained in:
parent
b4f014574b
commit
3f17d0e7a9
4 changed files with 170 additions and 36 deletions
116
template/module.go
Normal file
116
template/module.go
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
package template
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/safing/portbase/config"
|
||||
"github.com/safing/portbase/modules"
|
||||
"github.com/safing/portbase/modules/subsystems"
|
||||
)
|
||||
|
||||
const (
|
||||
eventStateUpdate = "state update"
|
||||
)
|
||||
|
||||
var (
|
||||
module *modules.Module
|
||||
)
|
||||
|
||||
func init() {
|
||||
// register module
|
||||
module = modules.Register("template", prep, start, stop) // add dependencies...
|
||||
|
||||
// register events that other modules can subscribe to
|
||||
module.RegisterEvent(eventStateUpdate)
|
||||
}
|
||||
|
||||
func prep() error {
|
||||
// register module as subsystem
|
||||
err := subsystems.Register(
|
||||
"Template Subsystem", // name
|
||||
"This subsystem is a template for quick setup", // description
|
||||
module,
|
||||
"config:template", // key space for configuration options registered
|
||||
&config.Option{
|
||||
Name: "Enable Template Subsystem",
|
||||
Key: "config:subsystems/template",
|
||||
Description: "This option enables the Template Subsystem [TEMPLATE]",
|
||||
OptType: config.OptTypeBool,
|
||||
DefaultValue: false,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// register options
|
||||
err = config.Register(&config.Option{
|
||||
Name: "language",
|
||||
Key: "config:template/language",
|
||||
Description: "Sets the language for the template [TEMPLATE]",
|
||||
OptType: config.OptTypeString,
|
||||
ExpertiseLevel: config.ExpertiseLevelUser, // default
|
||||
ReleaseLevel: config.ReleaseLevelStable, // default
|
||||
RequiresRestart: false, // default
|
||||
DefaultValue: "en",
|
||||
ValidationRegex: "^[a-z]{2}$",
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// register event hooks
|
||||
// do this in prep() and not in start(), as we don't want to register again if module is turned off and on again
|
||||
err = module.RegisterEventHook(
|
||||
"template", // event source module name
|
||||
"state update", // event source name
|
||||
"react to state changes", // description of hook function
|
||||
eventHandler, // hook function
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// hint: event hooks and tasks will not be run if module isn't online
|
||||
return nil
|
||||
}
|
||||
|
||||
func start() error {
|
||||
// register tasks
|
||||
module.NewTask("do something", taskFn).Queue()
|
||||
|
||||
// start service worker
|
||||
module.StartServiceWorker("do something", 0, serviceWorker)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func stop() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func serviceWorker(ctx context.Context) error {
|
||||
for {
|
||||
select {
|
||||
case <-time.After(1 * time.Second):
|
||||
err := do()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case <-ctx.Done():
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func taskFn(ctx context.Context, task *modules.Task) error {
|
||||
return do()
|
||||
}
|
||||
|
||||
func eventHandler(ctx context.Context, data interface{}) error {
|
||||
return do()
|
||||
}
|
||||
|
||||
func do() error {
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue