8000 feat: store Connor's task logs by nikonov1101 · Pull Request #1390 · sonm-io/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: store Connor's task logs #1390

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

Merged
merged 1 commit into from
Sep 3, 2018
Merged
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
1 change: 1 addition & 0 deletions connor/antifraud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type ProcessorConfig struct {
Format string `yaml:"format" required:""`
TrackInterval time.Duration `yaml:"track_interval" default:"10s"`
TaskWarmupDelay time.Duration `yaml:"warmup_delay" required:"true"`
LogDir string `yaml:"log_dir"`
}

type Config struct {
Expand Down
41 changes: 39 additions & 2 deletions connor/antifraud/log_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package antifraud
import (
"bufio"
"context"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
"time"
Expand All @@ -27,8 +30,9 @@ type commonLogProcessor struct {
hashrateEWMA metrics.EWMA
startTime time.Time

hashrate float64
logParser logParseFunc
hashrate float64
logParser logParseFunc
historyFile *os.File
}

func newLogProcessor(cfg *ProcessorConfig, log *zap.Logger, conn *grpc.ClientConn, deal *sonm.Deal, taskID string, lp logParseFunc) Processor {
Expand Down Expand Up @@ -91,6 +95,31 @@ func (m *commonLogProcessor) Run(ctx context.Context) error {
}
}

func (m *commonLogProcessor) maybeOpenHistoryFile() error {
if len(m.cfg.LogDir) == 0 {
return fmt.Errorf("task logs saving is not configured")
}

fileName := fmt.Sprintf("%s_%s.log", m.deal.GetId().Unwrap().String(), m.taskID)
fullPath := path.Join(m.cfg.LogDir, fileName)

file, err := os.OpenFile(fullPath, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return fmt.Errorf("failed to open log file: %v", err)
}

m.historyFile = file
return nil
}

func (m *commonLogProcessor) maybeSaveLogLine(line string) {
if m.historyFile != nil {
if _, err := m.historyFile.WriteString(line + "\n"); err != nil {
m.log.Warn("cannot write task log", zap.String("file", m.historyFile.Name()), zap.Error(err))
}
}
}

func (m *commonLogProcessor) fetchLogs(ctx context.Context) error {
request := &sonm.TaskLogsRequest{
Type: sonm.TaskLogsRequest_STDOUT,
Expand All @@ -99,6 +128,12 @@ func (m *commonLogProcessor) fetchLogs(ctx context.Context) error {
DealID: m.deal.Id,
}

if err := m.maybeOpenHistoryFile(); err != nil {
m.log.Warn("failed to open log file, task logs wouldn't be saved", zap.Error(err))
} else {
defer m.historyFile.Close()
}

retryTicker := util.NewImmediateTicker(m.cfg.TrackInterval)
defer retryTicker.Stop()

Expand Down Expand Up @@ -141,6 +176,8 @@ func claymoreLogParser(ctx context.Context, m *commonLogProcessor, reader io.Rea
}

line := scanner.Text()
m.maybeSaveLogLine(line)

if strings.Contains(line, "Total Speed: ") {
fields := strings.Fields(line)
if len(fields) != 13 {
Expand Down
0