accounting: add call to clear stats

- Make calls more consistent by changing path to kebab case.
- Add stacktrace information to job panics
This commit is contained in:
Aleksandar Jankovic 2019-07-30 13:22:06 +02:00 committed by Nick Craig-Wood
parent 5be968c0ca
commit 6a3e301303
4 changed files with 56 additions and 4 deletions

View File

@ -396,7 +396,7 @@ This tells the go runtime to do a garbage collection run. It isn't
necessary to call this normally, but it can be useful for debugging necessary to call this normally, but it can be useful for debugging
memory problems. memory problems.
### core/group_list: Returns list of stats. ### core/group-list: Returns list of stats.
This returns list of stats groups currently in memory. This returns list of stats groups currently in memory.
@ -481,6 +481,14 @@ Returns the following values:
Values for "transferring", "checking" and "lastError" are only assigned if data is available. Values for "transferring", "checking" and "lastError" are only assigned if data is available.
The value for "eta" is null if an eta cannot be determined. The value for "eta" is null if an eta cannot be determined.
### core/stats-reset: Reset stats.
This clears counters and errors for all stats or specific stats group if group
is provided.
Parameters
- group - name of the stats group (string)
### core/transferred: Returns stats about completed transfers. ### core/transferred: Returns stats about completed transfers.
This returns stats about completed transfers: This returns stats about completed transfers:

View File

@ -51,6 +51,23 @@ func transferredStats(ctx context.Context, in rc.Params) (rc.Params, error) {
return out, nil return out, nil
} }
func resetStats(ctx context.Context, in rc.Params) (rc.Params, error) {
// Check to see if we should filter by group.
group, err := in.GetString("group")
if rc.NotErrParamNotFound(err) {
return rc.Params{}, err
}
if group != "" {
groups.get(group).ResetCounters()
groups.get(group).ResetErrors()
} else {
groups.clear()
}
return rc.Params{}, nil
}
func init() { func init() {
// Init stats container // Init stats container
groups = newStatsGroups() groups = newStatsGroups()
@ -143,7 +160,7 @@ Returns the following values:
}) })
rc.Add(rc.Call{ rc.Add(rc.Call{
Path: "core/group_list", Path: "core/group-list",
Fn: listStats, Fn: listStats,
Title: "Returns list of stats.", Title: "Returns list of stats.",
Help: ` Help: `
@ -159,6 +176,19 @@ Returns the following values:
... ...
] ]
} }
`,
})
rc.Add(rc.Call{
Path: "core/stats-reset",
Fn: resetStats,
Title: "Reset stats.",
Help: `
This clears counters and errors for all stats or specific stats group if group
is provided.
Parameters
- group - name of the stats group (string)
`, `,
}) })
} }
@ -287,3 +317,16 @@ func (sg *statsGroups) sum() *StatsInfo {
} }
return sum return sum
} }
func (sg *statsGroups) clear() {
sg.mu.Lock()
defer sg.mu.Unlock()
for _, stats := range sg.m {
stats.ResetErrors()
stats.ResetCounters()
}
sg.m = make(map[string]*StatsInfo)
sg.order = nil
}

View File

@ -5,6 +5,7 @@ package jobs
import ( import (
"context" "context"
"fmt" "fmt"
"runtime/debug"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -126,7 +127,7 @@ func (job *Job) finish(out rc.Params, err error) {
func (job *Job) run(ctx context.Context, fn rc.Func, in rc.Params) { func (job *Job) run(ctx context.Context, fn rc.Func, in rc.Params) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
job.finish(nil, errors.Errorf("panic received: %v", r)) job.finish(nil, errors.Errorf("panic received: %v \n%s", r, string(debug.Stack())))
} }
}() }()
job.finish(fn(ctx, in)) job.finish(fn(ctx, in))

View File

@ -184,7 +184,7 @@ func TestJobRunPanic(t *testing.T) {
assert.Equal(t, false, job.EndTime.IsZero()) assert.Equal(t, false, job.EndTime.IsZero())
assert.Equal(t, rc.Params{}, job.Output) assert.Equal(t, rc.Params{}, job.Output)
assert.True(t, job.Duration >= floatSleepTime) assert.True(t, job.Duration >= floatSleepTime)
assert.Equal(t, "panic received: boom", job.Error) assert.Contains(t, job.Error, "panic received: boom")
assert.Equal(t, false, job.Success) assert.Equal(t, false, job.Success)
assert.Equal(t, true, job.Finished) assert.Equal(t, true, job.Finished)
job.mu.Unlock() job.mu.Unlock()