Skip to content

Commit

Permalink
introduce executor interface
Browse files Browse the repository at this point in the history
  • Loading branch information
kaanaktas committed Sep 19, 2022
1 parent 3259e73 commit 0bf011f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
11 changes: 8 additions & 3 deletions datafilter/datafilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import (
"sync"
)

func Apply(actions []policy.Action, data *string) {
type Executor struct {
Actions []policy.Action
Data *string
}

func (e *Executor) Apply() {
breaker := make(chan string)
in := make(chan Validate)
closeCh := make(chan struct{})

go processor(actions, in, breaker)
go validator(data, in, closeCh, breaker)
go processor(e.Actions, in, breaker)
go validator(e.Data, in, closeCh, breaker)

select {
case v := <-breaker:
Expand Down
6 changes: 5 additions & 1 deletion datafilter/datafilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ func TestExecuteDataFilter(t *testing.T) {
}
}()

Apply(actions, &tt.args.data)
executor := &Executor{
Actions: actions,
Data: &tt.args.data,
}
executor.Apply()
})
}
}
Expand Down
24 changes: 18 additions & 6 deletions executor/executor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package executor

import (
"fmt"
"github.com/kaanaktas/go-slm/cache"
"github.com/kaanaktas/go-slm/config"
"github.com/kaanaktas/go-slm/datafilter"
Expand All @@ -12,6 +13,10 @@ import (

var cacheIn = cache.NewInMemory()

type Executor interface {
Apply()
}

type specification struct {
PolicyRuleSetPath string `envconfig:"policy_rule_set_path"`
CommonRulesPath string `envconfig:"common_policies_path"`
Expand Down Expand Up @@ -56,11 +61,18 @@ func Apply(data, serviceName, direction string) {
}

for _, statement := range policyStatements {
switch statement.Type {
case config.StatementSchedule:
schedule.Apply(statement.Actions)
case config.StatementData:
datafilter.Apply(statement.Actions, &data)
}
statementExecutor := createExecutor(data, statement.Type, statement.Actions)
statementExecutor.Apply()
}
}

func createExecutor(data string, statementType string, actions []policy.Action) Executor {
switch statementType {
case config.StatementSchedule:
return &schedule.Executor{Actions: actions}
case config.StatementData:
return &datafilter.Executor{Actions: actions, Data: &data}
default:
panic(fmt.Sprintf("StatementType: %s doesn't exist", statementType))
}
}
30 changes: 17 additions & 13 deletions schedule/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,22 @@ type schedule struct {
Message string `yaml:"message"`
}

func Load(scheduleStatementPath string) {
if scheduleStatementPath == "" {
panic("GO_SLM_SCHEDULE_STATEMENT_PATH hasn't been set")
}
content := config.MustReadFile(filepath.Join(config.RootDirectory, scheduleStatementPath))
var schedules []schedule
config.MustUnmarshalYaml(scheduleStatementPath, content, &schedules)

cacheIn.Set(key, schedules, cache.NoExpiration)
type Executor struct {
Actions []policy.Action
}

func Apply(actions []policy.Action) {
func (e *Executor) Apply() {
cachedSchedule, ok := cacheIn.Get(key)
if !ok {
panic("schedule doesn't exist in the cache")
}

schedules := cachedSchedule.([]schedule)
sort.Slice(actions, func(i, j int) bool {
return actions[i].Order < actions[j].Order
sort.Slice(e.Actions, func(i, j int) bool {
return e.Actions[i].Order < e.Actions[j].Order
})

for _, action := range actions {
for _, action := range e.Actions {
if action.Active {
for _, sc := range schedules {
if sc.ScheduleName == action.Name {
Expand All @@ -61,6 +54,17 @@ func Apply(actions []policy.Action) {
}
}

func Load(scheduleStatementPath string) {
if scheduleStatementPath == "" {
panic("GO_SLM_SCHEDULE_STATEMENT_PATH hasn't been set")
}
content := config.MustReadFile(filepath.Join(config.RootDirectory, scheduleStatementPath))
var schedules []schedule
config.MustUnmarshalYaml(scheduleStatementPath, content, &schedules)

cacheIn.Set(key, schedules, cache.NoExpiration)
}

func isScheduleMatchWithPolicy(sc schedule) bool {
if isScheduledDayActive(sc.Days) {
return isCurrentTimeInScheduledTime(generateStartTime(sc.Start), time.Duration(sc.Duration))
Expand Down

0 comments on commit 0bf011f

Please sign in to comment.