mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-12 02:26:26 +05:00
added action type functions and a simple test
This commit is contained in:
@@ -63,7 +63,7 @@ func (s scheduler) loop() {
|
||||
a0 := s.queue[0]
|
||||
now := time.Now()
|
||||
if a0.GetNextStartTime().Equal(now) || a0.GetNextStartTime().Before(now) {
|
||||
a0.Execute()
|
||||
go a0.Execute()
|
||||
s.queue = append(s.queue, a0)
|
||||
s.queue = s.queue[1:]
|
||||
sort.Sort(s.queue)
|
||||
|
||||
@@ -74,6 +74,19 @@ type Action struct {
|
||||
MinuteBucket *MinuteBucket
|
||||
}
|
||||
|
||||
type actionTypeFunc func(a *Action) error
|
||||
|
||||
var (
|
||||
actionTypeFuncMap = map[string]actionTypeFunc{
|
||||
"LOG": logAction,
|
||||
}
|
||||
)
|
||||
|
||||
func logAction(a *Action) (err error) {
|
||||
log.Printf("%v %v %v", a.BalanceId, a.Units, a.MinuteBucket)
|
||||
return
|
||||
}
|
||||
|
||||
// Structure to store actions according to weight
|
||||
type actionsorter []*Action
|
||||
|
||||
@@ -202,12 +215,18 @@ MONTHS:
|
||||
return
|
||||
}
|
||||
|
||||
func (at *ActionTiming) Execute() {
|
||||
func (at *ActionTiming) Execute() (err error) {
|
||||
aac, err := at.getActions()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, a := range aac {
|
||||
log.Print(a)
|
||||
actionFunction, exists := actionTypeFuncMap[a.ActionType]
|
||||
if !exists {
|
||||
log.Printf("Function type %v not available, aborting execution!", a.ActionType)
|
||||
return
|
||||
}
|
||||
err = actionFunction(a)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -161,3 +161,19 @@ func TestActionTimingHourMonthdaysMonths(t *testing.T) {
|
||||
t.Errorf("Expected %v was %v", expected, st)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogFunction(t *testing.T) {
|
||||
a := &Action{
|
||||
ActionType: "LOG",
|
||||
BalanceId: "test",
|
||||
Units: 1.1,
|
||||
MinuteBucket: &MinuteBucket{},
|
||||
}
|
||||
at := &ActionTiming{
|
||||
actions: []*Action{a},
|
||||
}
|
||||
err := at.Execute()
|
||||
if err != nil {
|
||||
t.Errorf("Could not execute LOG action: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,10 +30,11 @@ const (
|
||||
INBOUND = "IN"
|
||||
OUTBOUND = "OUT"
|
||||
// Balance types
|
||||
CREDIT = "MONETARY"
|
||||
SMS = "SMS"
|
||||
TRAFFIC = "INTERNET"
|
||||
MINUTES = "MINUTES"
|
||||
CREDIT = "MONETARY"
|
||||
SMS = "SMS"
|
||||
TRAFFIC = "INTERNET"
|
||||
TRAFFIC_TIME = "INTERNET_TIME"
|
||||
MINUTES = "MINUTES"
|
||||
// Price types
|
||||
PERCENT = "PERCENT"
|
||||
ABSOLUTE = "ABSOLUTE"
|
||||
|
||||
Reference in New Issue
Block a user