Skip to content

Commit 85707e8

Browse files
fix(tasks): list tasks with better formatting
1 parent 748e329 commit 85707e8

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

pkg/command/flag/flag.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ func (w Wrapper) interval(p *Duration) {
146146

147147
func (w Wrapper) startDate(p *StartDate) {
148148
w.fs.VarP(p, "start-date", "s", usage["start-date"])
149-
w.MustMarkDeprecated("start-date", "use cron instead")
150149
}
151150

152151
func (w Wrapper) numRetries(p *int, def int) {

pkg/managerclient/model.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package managerclient
44

55
import (
6+
"encoding/json"
67
"fmt"
78
"io"
89
"sort"
@@ -14,7 +15,9 @@ import (
1415
"github.com/pkg/errors"
1516
"github.com/scylladb/go-set/strset"
1617
"github.com/scylladb/scylla-manager/v3/pkg/managerclient/table"
18+
"github.com/scylladb/scylla-manager/v3/pkg/service/scheduler"
1719
"github.com/scylladb/scylla-manager/v3/pkg/util/inexlist"
20+
"github.com/scylladb/scylla-manager/v3/pkg/util/timeutc"
1821
"github.com/scylladb/scylla-manager/v3/pkg/util/version"
1922
"github.com/scylladb/scylla-manager/v3/swagger/gen/scylla-manager/models"
2023
"github.com/scylladb/termtables"
@@ -513,7 +516,18 @@ func (li TaskListItems) Render(w io.Writer) error {
513516

514517
var schedule string
515518
if t.Schedule.Cron != "" {
516-
schedule = t.Schedule.Cron
519+
var cronSpec scheduler.CronSpecification
520+
err := json.Unmarshal([]byte(t.Schedule.Cron), &cronSpec)
521+
if err != nil {
522+
schedule = t.Schedule.Cron
523+
} else {
524+
schedule = cronSpec.Spec
525+
if cronSpec.StartDate.After(timeutc.Now()) {
526+
c := scheduler.MustCron(cronSpec.Spec, cronSpec.StartDate)
527+
schedule += fmt.Sprintf(" with first activation after %s",
528+
c.Next(cronSpec.StartDate).Format("2006-01-02 15:04:05"))
529+
}
530+
}
517531
} else if t.Schedule.Interval != "" {
518532
schedule = t.Schedule.Interval
519533
}

pkg/restapi/task.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ func (h *taskHandler) parseTask(r *http.Request) (*scheduler.Task, error) {
151151
if err := render.DecodeJSON(r.Body, &t); err != nil {
152152
return nil, err
153153
}
154+
if !t.Sched.StartDate.IsZero() && t.Sched.Cron.Spec != "" {
155+
t.Sched.Cron.StartDate = t.Sched.StartDate
156+
}
154157
t.ClusterID = mustClusterIDFromCtx(r)
155158
return &t, nil
156159
}

pkg/service/scheduler/model.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ func (t *TaskType) UnmarshalText(text []byte) error {
7070
// Cron implements a trigger based on cron expression.
7171
// It supports the extended syntax including @monthly, @weekly, @daily, @midnight, @hourly, @every <time.Duration>.
7272
type Cron struct {
73-
cronSpecification
73+
CronSpecification
7474
inner scheduler.Trigger
7575
}
7676

77-
type cronSpecification struct {
77+
// CronSpecification combines specification for cron together with the start dates
78+
// that defines the moment when the cron is being started.
79+
type CronSpecification struct {
7880
Spec string `json:"spec"`
7981
StartDate time.Time `json:"start_date"`
8082
}
@@ -86,7 +88,7 @@ func NewCron(spec string, startDate time.Time) (Cron, error) {
8688
}
8789

8890
return Cron{
89-
cronSpecification: cronSpecification{
91+
CronSpecification: CronSpecification{
9092
Spec: spec,
9193
StartDate: startDate,
9294
},
@@ -120,9 +122,9 @@ func (c Cron) Next(now time.Time) time.Time {
120122
}
121123

122124
func (c Cron) MarshalText() (text []byte, err error) {
123-
bytes, err := json.Marshal(c.cronSpecification)
125+
bytes, err := json.Marshal(c.CronSpecification)
124126
if err != nil {
125-
return nil, errors.Wrapf(err, "cannot json marshal {%v}", c.cronSpecification)
127+
return nil, errors.Wrapf(err, "cannot json marshal {%v}", c.CronSpecification)
126128
}
127129
return bytes, nil
128130
}
@@ -132,11 +134,11 @@ func (c *Cron) UnmarshalText(text []byte) error {
132134
return nil
133135
}
134136

135-
var cronSpec cronSpecification
137+
var cronSpec CronSpecification
136138
err := json.Unmarshal(text, &cronSpec)
137139
if err != nil {
138140
// fallback to the < 3.2.6 approach where cron was not coupled with start date
139-
cronSpec = cronSpecification{
141+
cronSpec = CronSpecification{
140142
Spec: string(text),
141143
}
142144
}

pkg/service/scheduler/model_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,28 @@ func TestCronMarshalUnmarshal(t *testing.T) {
6666
for _, tc := range []struct {
6767
name string
6868
data []byte
69-
expectedSpec cronSpecification
69+
expectedSpec CronSpecification
7070
}{
7171
{
7272
name: "(3.2.6 backward compatibility) unmarshal spec",
7373
data: []byte("@every 15s"),
74-
expectedSpec: cronSpecification{
74+
expectedSpec: CronSpecification{
7575
Spec: "@every 15s",
7676
StartDate: time.Time{},
7777
},
7878
},
7979
{
8080
name: "unmarshal spec full struct zero time",
8181
data: []byte(`{"spec": "@every 15s", "start_date": "0001-01-01T00:00:00Z"}`),
82-
expectedSpec: cronSpecification{
82+
expectedSpec: CronSpecification{
8383
Spec: "@every 15s",
8484
StartDate: time.Time{},
8585
},
8686
},
8787
{
8888
name: "unmarshal spec full struct non-zero time",
8989
data: []byte(`{"spec": "@every 15s", "start_date": "` + nonZeroTimeString + `"}`),
90-
expectedSpec: cronSpecification{
90+
expectedSpec: CronSpecification{
9191
Spec: "@every 15s",
9292
StartDate: nonZeroTime,
9393
},

0 commit comments

Comments
 (0)