Skip to content

Commit

Permalink
Add sidecar functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
g41797 committed Sep 21, 2023
1 parent 44cf26c commit 2e6211c
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 81 deletions.
4 changes: 2 additions & 2 deletions dumbBlock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (dmb *dumbBlock) run(bc sputnik.BlockCommunicator) {
// redirected OnMsg|OnConnect|etc
select {
case <-dmb.stop:
return
break
}

return
Expand All @@ -57,7 +57,7 @@ func (dmb *dumbBlock) finish(init bool) {

select {
case <-dmb.done: // Wait finish of Run
return
break
}
return
}
Expand Down
54 changes: 0 additions & 54 deletions logger.go

This file was deleted.

2 changes: 1 addition & 1 deletion msgprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (pr *msgProcessor) process() {
for {
msg, ok := pr.q.Get()
if !ok {
return
break
}
pr.fnc(msg)
}
Expand Down
File renamed without changes.
5 changes: 3 additions & 2 deletions config.go → sidecar/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package sputnik
package sidecar

import (
"os"
"path/filepath"
"strings"

"github.com/g41797/gonfig"
"github.com/g41797/sputnik"
)

// ConfigFactory returns implementation of sputnik.ConfFactory based on github.com/tkanos/gonfig
Expand All @@ -14,7 +15,7 @@ import (
// - Env. variable for configuration "example" and key "kname" should be set in environment as "EXAMPLE_KNAME"
// - Value in environment automatically overrides value from the file
// - Temporary used github.com/g41797/gonfig (till merge of PR)
func ConfigFactory(cfPath string) ConfFactory {
func ConfigFactory(cfPath string) sputnik.ConfFactory {
cnf := newConfig(cfPath)
return cnf.unmarshal
}
Expand Down
8 changes: 4 additions & 4 deletions config_test.go → sidecar/config_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package sputnik_test
package sidecar_test

import (
"os"
"strconv"
"testing"

"github.com/g41797/sputnik"
"github.com/g41797/sputnik/sidecar"
)

type TestConf struct {
Expand All @@ -17,7 +17,7 @@ func TestJSON(t *testing.T) {

confFolderPath := "./_conf_test/"

cfact := sputnik.ConfigFactory(confFolderPath)
cfact := sidecar.ConfigFactory(confFolderPath)

expected := defaults()

Expand All @@ -32,7 +32,7 @@ func TestENV(t *testing.T) {

confFolderPath := "./_conf_test/"

cfact := sputnik.ConfigFactory(confFolderPath)
cfact := sidecar.ConfigFactory(confFolderPath)

expected := defaults()
expected.SECONDINT = 12345
Expand Down
8 changes: 5 additions & 3 deletions messageproducer.go → sidecar/messageproducer.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package sputnik
package sidecar

import "github.com/g41797/sputnik"

// MessageProducer provides possibility for negotiation between sputnik based
// software and external broker process
type MessageProducer interface {
// Connect to the broker
Connect(cf ConfFactory) error
Connect(cf sputnik.ConfFactory) error

// Translate message to format of the broker and send it
Produce(msg Msg) error
Produce(msg sputnik.Msg) error

// If connection is alive closes it
Disconnect()
Expand Down
32 changes: 17 additions & 15 deletions runner.go → sidecar/runner.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sputnik
package sidecar

import (
"encoding/json"
Expand All @@ -7,9 +7,11 @@ import (
"os"
"path/filepath"
"time"

"github.com/g41797/sputnik"
)

func Start(cntr ServerConnector) {
func Start(cntr sputnik.ServerConnector) {

var confFolder string
flag.StringVar(&confFolder, "cf", "", "Path of folder with config files")
Expand All @@ -35,12 +37,12 @@ const brokerCheckTimeOut = time.Second

type Runner struct {
// ShootDown
kill ShootDown
kill sputnik.ShootDown
// Signalling channel
done chan struct{}
}

func StartRunner(confFolder string, cntr ServerConnector) (*Runner, error) {
func StartRunner(confFolder string, cntr sputnik.ServerConnector) (*Runner, error) {
info, err := prepare(confFolder, cntr)
if err != nil {
return nil, err
Expand Down Expand Up @@ -83,12 +85,12 @@ func (rnr *Runner) Wait() {
}

type runnerInfo struct {
cfact ConfFactory
cnt ServerConnector
appBlocks []BlockDescriptor
cfact sputnik.ConfFactory
cnt sputnik.ServerConnector
appBlocks []sputnik.BlockDescriptor
}

func prepare(confFolder string, cntr ServerConnector) (*runnerInfo, error) {
func prepare(confFolder string, cntr sputnik.ServerConnector) (*runnerInfo, error) {
info, err := os.Stat(confFolder)
if err != nil {
return nil, err
Expand All @@ -110,10 +112,10 @@ func prepare(confFolder string, cntr ServerConnector) (*runnerInfo, error) {

func (rnr *Runner) Start(ri *runnerInfo) error {

sp, err := NewSputnik(
WithAppBlocks(ri.appBlocks),
WithConfFactory(ri.cfact),
WithConnector(ri.cnt, brokerCheckTimeOut),
sp, err := sputnik.NewSputnik(
sputnik.WithAppBlocks(ri.appBlocks),
sputnik.WithConfFactory(ri.cfact),
sputnik.WithConnector(ri.cnt, brokerCheckTimeOut),
)

if err != nil {
Expand All @@ -128,23 +130,23 @@ func (rnr *Runner) Start(ri *runnerInfo) error {
rnr.kill = kill
rnr.done = make(chan struct{})

go func(l Launch, done chan struct{}) {
go func(l sputnik.Launch, done chan struct{}) {
l()
close(done)
}(launch, rnr.done)

return nil
}

func ReadAppBlocks(confFolder string) ([]BlockDescriptor, error) {
func ReadAppBlocks(confFolder string) ([]sputnik.BlockDescriptor, error) {
fPath := filepath.Join(confFolder, "blocks.json")

blocksRaw, err := os.ReadFile(fPath)
if err != nil {
return nil, err
}

var result []BlockDescriptor
var result []sputnik.BlockDescriptor

json.Unmarshal([]byte(blocksRaw), &result)

Expand Down

0 comments on commit 2e6211c

Please sign in to comment.