mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
refeactored history
This commit is contained in:
@@ -22,7 +22,7 @@ func start() {
|
||||
if *masterAddr != "" {
|
||||
store, err := NewProxyStore(*masterAddr)
|
||||
} else {
|
||||
store, err := NewHistoryStore(*dataFile)
|
||||
store, err := NewFileStore(*dataFile)
|
||||
}
|
||||
rpc.RegisterName("Store", store)
|
||||
rpc.HandleHTTP()
|
||||
|
||||
72
history/file_store.go
Normal file
72
history/file_store.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package history
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type FileStore struct {
|
||||
sync.RWMutex
|
||||
filename string
|
||||
records records
|
||||
}
|
||||
|
||||
func NewFileStore(filename string) (*FileStore, error) {
|
||||
// looking for git
|
||||
_, err := exec.LookPath("git")
|
||||
if err != nil {
|
||||
return nil, errors.New("Please install git: " + err.Error())
|
||||
}
|
||||
s := &FileStore{filename: filename}
|
||||
return s, s.load()
|
||||
}
|
||||
|
||||
func (s *FileStore) Record(key string, obj interface{}) error {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
s.records = s.records.SetOrAdd(key, obj)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *FileStore) commit() error {
|
||||
out, err := exec.Command("git", "commit", "-a", "-m", "'historic commit'").Output()
|
||||
if err != nil {
|
||||
return errors.New(string(out) + " " + err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *FileStore) load() error {
|
||||
f, err := os.Open(s.filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
d := json.NewDecoder(f)
|
||||
|
||||
if err := d.Decode(&s.records); err != nil {
|
||||
return err
|
||||
}
|
||||
s.records.Sort()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *FileStore) save(filename string) error {
|
||||
f, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b := bufio.NewWriter(f)
|
||||
e := json.NewEncoder(b)
|
||||
defer f.Close()
|
||||
defer b.Flush()
|
||||
s.records.Sort()
|
||||
if err := e.Encode(s.records); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.commit()
|
||||
}
|
||||
1
history/mock_stor.go
Normal file
1
history/mock_stor.go
Normal file
@@ -0,0 +1 @@
|
||||
package history
|
||||
24
history/proxy_store.go
Normal file
24
history/proxy_store.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package history
|
||||
|
||||
import (
|
||||
"net/rpc"
|
||||
)
|
||||
|
||||
type ProxyStore struct {
|
||||
client *rpc.Client
|
||||
}
|
||||
|
||||
func NewProxyStore(addr string) (*ProxyStore, error) {
|
||||
client, err := rpc.DialHTTP("tcp", addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ProxyStore{client: client}, nil
|
||||
}
|
||||
|
||||
func (ps *ProxyStore) Record(key string, obj interface{}) error {
|
||||
if err := ps.client.Call("Store.Record", key, obj); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,26 +1,13 @@
|
||||
package history
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/rpc"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sort"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Store interface {
|
||||
Record(key string, obj interface{}) error
|
||||
}
|
||||
|
||||
type HistoryStore struct {
|
||||
sync.RWMutex
|
||||
filename string
|
||||
records records
|
||||
}
|
||||
|
||||
type record struct {
|
||||
Key string
|
||||
Object interface{}
|
||||
@@ -58,78 +45,3 @@ func (rs records) SetOrAdd(key string, obj interface{}) records {
|
||||
}
|
||||
return rs
|
||||
}
|
||||
|
||||
func NewHistoryStore(filename string) (*HistoryStore, error) {
|
||||
// looking for git
|
||||
_, err := exec.LookPath("git")
|
||||
if err != nil {
|
||||
return nil, errors.New("Please install git: " + err.Error())
|
||||
}
|
||||
s := &HistoryStore{filename: filename}
|
||||
return s, s.load()
|
||||
}
|
||||
|
||||
func (s *HistoryStore) Record(key string, obj interface{}) error {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
s.records = s.records.SetOrAdd(key, obj)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *HistoryStore) commit() error {
|
||||
out, err := exec.Command("git", "commit", "-a", "-m", "'historic commit'").Output()
|
||||
if err != nil {
|
||||
return errors.New(string(out) + " " + err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *HistoryStore) load() error {
|
||||
f, err := os.Open(s.filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
d := json.NewDecoder(f)
|
||||
|
||||
if err := d.Decode(&s.records); err != nil {
|
||||
return err
|
||||
}
|
||||
s.records.Sort()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *HistoryStore) save(filename string) error {
|
||||
f, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b := bufio.NewWriter(f)
|
||||
e := json.NewEncoder(b)
|
||||
defer f.Close()
|
||||
defer b.Flush()
|
||||
s.records.Sort()
|
||||
if err := e.Encode(s.records); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.commit()
|
||||
}
|
||||
|
||||
type ProxyStore struct {
|
||||
client *rpc.Client
|
||||
}
|
||||
|
||||
func NewProxyStore(addr string) (*ProxyStore, error) {
|
||||
client, err := rpc.DialHTTP("tcp", addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ProxyStore{client: client}, nil
|
||||
}
|
||||
|
||||
func (ps *ProxyStore) Record(key string, obj interface{}) error {
|
||||
if err := ps.client.Call("Store.Record", key, obj); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user