Fix receiving proper current command args, simplifying retrieval of last log line

This commit is contained in:
Ingo Oppermann
2023-03-01 20:13:49 +01:00
parent 857f5b8182
commit cceb39192a
3 changed files with 30 additions and 29 deletions

View File

@@ -32,6 +32,9 @@ type Parser interface {
// ReportHistory returns an array of previews logs // ReportHistory returns an array of previews logs
ReportHistory() []ReportHistoryEntry ReportHistory() []ReportHistoryEntry
// LastLogline returns the last parsed log line
LastLogline() string
} }
// Config is the config for the Parser implementation // Config is the config for the Parser implementation
@@ -71,6 +74,8 @@ type parser struct {
logHistory *ring.Ring logHistory *ring.Ring
logHistoryLength int logHistoryLength int
lastLogline string
progress struct { progress struct {
ffmpeg ffmpegProgress ffmpeg ffmpegProgress
avstream map[string]ffmpegAVstream avstream map[string]ffmpegAVstream
@@ -622,6 +627,8 @@ func (p *parser) addLog(line string) {
p.lock.log.Lock() p.lock.log.Lock()
defer p.lock.log.Unlock() defer p.lock.log.Unlock()
p.lastLogline = line
p.log.Value = process.Line{ p.log.Value = process.Line{
Timestamp: time.Now(), Timestamp: time.Now(),
Data: line, Data: line,
@@ -646,6 +653,13 @@ func (p *parser) Log() []process.Line {
return log return log
} }
func (p *parser) LastLogline() string {
p.lock.log.RLock()
defer p.lock.log.RUnlock()
return p.lastLogline
}
func (p *parser) ResetStats() { func (p *parser) ResetStats() {
p.lock.progress.Lock() p.lock.progress.Lock()
defer p.lock.progress.Unlock() defer p.lock.progress.Unlock()

View File

@@ -64,26 +64,14 @@ type Config struct {
// Status represents the current status of a process // Status represents the current status of a process
type Status struct { type Status struct {
// State is the current state of the process. See stateType for the known states. State string // State is the current state of the process. See stateType for the known states.
State string States States // States is the cumulative history of states the process had.
Order string // Order is the wanted condition of process, either "start" or "stop"
// States is the cumulative history of states the process had. Duration time.Duration // Duration is the time since the last change of the state
States States Time time.Time // Time is the time of the last change of the state
CPU float64 // Used CPU in percent
// Order is the wanted condition of process, either "start" or "stop" Memory uint64 // Used memory in bytes
Order string CommandArgs []string // Currently running command arguments
// Duration is the time since the last change of the state
Duration time.Duration
// Time is the time of the last change of the state
Time time.Time
// Used CPU in percent
CPU float64
// Used memory in bytes
Memory uint64
} }
// States // States
@@ -205,12 +193,14 @@ var _ Process = &process{}
func New(config Config) (Process, error) { func New(config Config) (Process, error) {
p := &process{ p := &process{
binary: config.Binary, binary: config.Binary,
args: config.Args,
cmd: nil, cmd: nil,
parser: config.Parser, parser: config.Parser,
logger: config.Logger, logger: config.Logger,
} }
p.args = make([]string, len(config.Args))
copy(p.args, config.Args)
// This is a loose check on purpose. If the e.g. the binary // This is a loose check on purpose. If the e.g. the binary
// doesn't exist or it is not executable, it will be // doesn't exist or it is not executable, it will be
// reflected in the resulting state. // reflected in the resulting state.
@@ -414,6 +404,9 @@ func (p *process) Status() Status {
Memory: memory, Memory: memory,
} }
s.CommandArgs = make([]string, len(p.args))
copy(s.CommandArgs, p.args)
return s return s
} }

View File

@@ -1264,8 +1264,8 @@ func (r *restream) GetProcessState(id string) (*app.State, error) {
state.CPU = status.CPU state.CPU = status.CPU
state.Duration = status.Duration.Round(10 * time.Millisecond).Seconds() state.Duration = status.Duration.Round(10 * time.Millisecond).Seconds()
state.Reconnect = -1 state.Reconnect = -1
state.Command = make([]string, len(task.command)) state.Command = status.CommandArgs
copy(state.Command, task.command) state.LastLog = task.parser.LastLogline()
if state.Order == "start" && !task.ffmpeg.IsRunning() && task.config.Reconnect { if state.Order == "start" && !task.ffmpeg.IsRunning() && task.config.Reconnect {
state.Reconnect = float64(task.config.ReconnectDelay) - state.Duration state.Reconnect = float64(task.config.ReconnectDelay) - state.Duration
@@ -1293,12 +1293,6 @@ func (r *restream) GetProcessState(id string) (*app.State, error) {
state.Progress.Output[i].ID = task.process.Config.Output[p.Index].ID state.Progress.Output[i].ID = task.process.Config.Output[p.Index].ID
} }
report := task.parser.Report()
if len(report.Log) != 0 {
state.LastLog = report.Log[len(report.Log)-1].Data
}
return state, nil return state, nil
} }