Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedding API #309

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 2 additions & 41 deletions beehive.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
"fmt"
"net/url"
"os"
"os/signal"
"syscall"

"github.com/mattn/go-colorable"
log "github.com/sirupsen/logrus"
Expand All @@ -39,6 +37,7 @@ import (
"github.com/muesli/beehive/cfg"
_ "github.com/muesli/beehive/filters"
_ "github.com/muesli/beehive/filters/template"
"github.com/muesli/beehive/reactor"

"github.com/muesli/beehive/bees"
)
Expand Down Expand Up @@ -126,45 +125,7 @@ func main() {
}
}

// Load actions from config
bees.SetActions(config.Actions)
// Load chains from config
bees.SetChains(config.Chains)
// Initialize bees
bees.StartBees(config.Bees)

// Wait for signals
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGKILL)

for s := range ch {
log.Println("Got signal:", s)

abort := false
switch s {
case syscall.SIGHUP:
err := config.Load()
if err != nil {
log.Panicf("Error loading config from %s: %v", config.URL(), err)
}
bees.StopBees()
bees.SetActions(config.Actions)
bees.SetChains(config.Chains)
bees.StartBees(config.Bees)

case syscall.SIGTERM:
fallthrough
case syscall.SIGKILL:
fallthrough
case os.Interrupt:
abort = true
break
}

if abort {
break
}
}
reactor.Run(config)

// Save actions & chains to config
log.Printf("Saving config to %s", config.URL())
Expand Down
12 changes: 11 additions & 1 deletion bees/hellobee/hellobee.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package hellobee

import (
"github.com/muesli/beehive/bees"
log "github.com/sirupsen/logrus"
)

// HelloBee is an example for a Bee skeleton, designed to help you get started
Expand All @@ -47,7 +48,16 @@ func (mod *HelloBee) Run(eventChan chan bees.Event) {

// Action triggers the action passed to it.
func (mod *HelloBee) Action(action bees.Action) []bees.Placeholder {
return []bees.Placeholder{}
outs := []bees.Placeholder{}

switch action.Name {
case "say_hello":
log.Info("Hello!")
default:
log.Debug("un")
}

return outs
}

// ReloadOptions parses the config options and initializes the Bee.
Expand Down
13 changes: 13 additions & 0 deletions bees/hellobee/hellobeefactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ func (factory *HelloBeeFactory) Description() string {
return "A 'Hello World' module for beehive"
}

// Actions describes the available actions provided by this Bee.
func (factory *HelloBeeFactory) Actions() []bees.ActionDescriptor {
actions := []bees.ActionDescriptor{
{
Namespace: factory.Name(),
Name: "say_hello",
Description: "Say Hello!",
Options: []bees.PlaceholderDescriptor{},
},
}
return actions
}

func init() {
f := HelloBeeFactory{}
bees.RegisterFactory(&f)
Expand Down
2 changes: 2 additions & 0 deletions bees/timebee/timebee.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"time"

"github.com/muesli/beehive/bees"
log "github.com/sirupsen/logrus"
)

// TimeBee is a Bee that can fire events at a specific time.
Expand Down Expand Up @@ -80,6 +81,7 @@ func (mod *TimeBee) timer() {
return
}

log.Debug("timebee: triggering timer")
mod.lastEvent = mod.curTime
event := bees.Event{
Bee: mod.Name(),
Expand Down
83 changes: 83 additions & 0 deletions examples/embedding/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

import (
"github.com/muesli/beehive/bees"
_ "github.com/muesli/beehive/bees/hellobee"
_ "github.com/muesli/beehive/bees/timebee"
"github.com/muesli/beehive/cfg"
"github.com/muesli/beehive/reactor"
)

func main() {
config, err := cfg.New("test.cfg")
// We could also use the memory backend
// config, err := cfg.New("mem://")
if err != nil {
panic(err)
}

helloBee := newHelloBee()
config.Bees = []bees.BeeConfig{
newTimeBee(),
helloBee,
}

// Create the Action and add it to the config
action := bees.Action{}
action.ID = "123"
action.Bee = helloBee.Name
action.Name = "say_hello"
action.Options = bees.Placeholders{}
config.Actions = []bees.Action{action}

// Create the event t
event := bees.Event{}
event.Name = "time"
event.Bee = "timer"

// Create the chain and add it to the config
chain := bees.Chain{}
chain.Name = "foochain"
chain.Description = "this is a test chain that will say hello every second"
chain.Actions = []string{action.ID} // Action ID we create above
chain.Event = &event
chain.Filters = []string{}
config.Chains = []bees.Chain{chain}

// Debugging level, prints debug messages from bees
reactor.SetLogLevel(5)
reactor.Run(config)

// Optional, saves the config to disk (if we didn't use the mem backend)
config.Save()
}

// Create a new bee that says hello
func newHelloBee() bees.BeeConfig {
options := bees.BeeOptions{}
bc, err := bees.NewBeeConfig("hello", "hellobee", "test", options)
if err != nil {
panic(err)
}

return bc
}

// Create a new bee that triggers events every second
func newTimeBee() bees.BeeConfig {
options := bees.BeeOptions{
bees.BeeOption{Name: "second", Value: "-1"},
bees.BeeOption{Name: "minute", Value: "-1"},
bees.BeeOption{Name: "hour", Value: "-1"},
bees.BeeOption{Name: "day_of_week", Value: "-1"},
bees.BeeOption{Name: "day_of_month", Value: "-1"},
bees.BeeOption{Name: "month", Value: "-1"},
bees.BeeOption{Name: "year", Value: "-1"},
}
bc, err := bees.NewBeeConfig("timer", "timebee", "test", options)
if err != nil {
panic(err)
}

return bc
}
63 changes: 63 additions & 0 deletions reactor/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package reactor

import (
"os"
"os/signal"
"syscall"

log "github.com/sirupsen/logrus"

"github.com/muesli/beehive/bees"
"github.com/muesli/beehive/cfg"
)

// SetLogLevel sets the reactor log level.
//
// From 0 (quiet) to 6 (trace).
func SetLogLevel(level int) {
log.SetLevel(log.Level(level))
}

// Reactor loops and handles signals.
func Run(config *cfg.Config) {
// Load actions from config
bees.SetActions(config.Actions)
// Load chains from config
bees.SetChains(config.Chains)
// Initialize bees
bees.StartBees(config.Bees)

// Wait for signals
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGKILL)

log.Debug("Starting Beehive's reactor")
for s := range ch {
log.Println("Got signal:", s)

abort := false
switch s {
case syscall.SIGHUP:
err := config.Load()
if err != nil {
log.Panicf("Error loading config from %s: %v", config.URL(), err)
}
bees.StopBees()
bees.SetActions(config.Actions)
bees.SetChains(config.Chains)
bees.StartBees(config.Bees)

case syscall.SIGTERM:
fallthrough
case syscall.SIGKILL:
fallthrough
case os.Interrupt:
abort = true
break
}

if abort {
break
}
}
}