Skip to content
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

feat(snapshots): added ability to use cron expressions to schedule snapshots #3149

Merged
merged 3 commits into from Jul 20, 2023

Conversation

jkowalski
Copy link
Contributor

@jkowalski jkowalski commented Jul 16, 2023

We use github.com/hashicorp/cronexpr to parse and evaluate expressions, as documented in https://github.com/hashicorp/cronexpr#implementation

Kapture.2023-07-16.at.11.24.13.mp4

Fixes #671
Related #1723

@codecov
Copy link

codecov bot commented Jul 16, 2023

Codecov Report

Patch coverage: 76.81% and project coverage change: -0.01 ⚠️

Comparison is base (b0ce67b) 75.40% compared to head (1a77e60) 75.40%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3149      +/-   ##
==========================================
- Coverage   75.40%   75.40%   -0.01%     
==========================================
  Files         459      459              
  Lines       36341    36409      +68     
==========================================
+ Hits        27404    27453      +49     
- Misses       7024     7039      +15     
- Partials     1913     1917       +4     
Impacted Files Coverage Δ
cli/command_policy_show.go 61.27% <0.00%> (-1.27%) ⬇️
cli/command_policy_set_scheduling.go 90.82% <81.08%> (-5.07%) ⬇️
snapshot/policy/scheduling_policy.go 83.19% <90.90%> (+1.74%) ⬆️
internal/server/api_policies.go 67.28% <100.00%> (+0.94%) ⬆️

... and 6 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@jkowalski jkowalski force-pushed the cron-schedule branch 5 times, most recently from aa24fe9 to ab057fb Compare July 16, 2023 19:14
…apshots

We use `github.com/hashicorp/cronexpr` to parse and evaluate expressions,
as documented in https://github.com/hashicorp/cronexpr#implementation
Copy link
Collaborator

@julio-lopez julio-lopez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LG. Just a couple of minor comments and questions.

@redgoat650 or @Shrekster should provide input as well.

expChangeCount: 1,
},
} {
t.Log(tc.name)
tc := tc
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why not move the declaration for psf here, inside the closure, so it is a new variable every time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

for _, e := range p.Cron {
ce, err := cronexpr.Parse(stripCronComment(e))
if err != nil {
// ignore invalid crontab entries, nothing we can do at this point
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this at least be logged?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and/or maybe a note pointing the reader to ValidateSchedulingPolicy, where an initial parsing check takes place before the cron strings can be added to the policy

Copy link
Contributor Author

@jkowalski jkowalski Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't want to log here since it happens in a tight loop, so if we ever log anything that's gonna flood the log file, added comment

return nil
}

func stripCronComment(s string) string {
return strings.TrimSpace(strings.Split(s, "#")[0])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: ?

Suggested change
return strings.TrimSpace(strings.Split(s, "#")[0])
return strings.TrimSpace(strings.SplitN(s, "#", 2)[0])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

pol: policy.SchedulingPolicy{
Cron: []string{"0 23 * * *"},
},
previousSnapshotTime: time.Date(2020, time.January, 1, 19, 0, 0, 0, time.Local),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm reading these cases correctly, now is before the previous snapshot, is that right?

Does it make sense to also have cases where now is after the previous snapshot? Both for cases where (a) now is before the time the next snapshot is supposed to be, and (b) now is after when the snapshot was supposed to have been taken.

What is the expected behavior for (b)? Would a new snapshot be taken right away?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed previousSnapshotTime since it was not essential here and confusing.

Copy link
Contributor

@redgoat650 redgoat650 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

for _, e := range p.Cron {
ce, err := cronexpr.Parse(stripCronComment(e))
if err != nil {
// ignore invalid crontab entries, nothing we can do at this point
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and/or maybe a note pointing the reader to ValidateSchedulingPolicy, where an initial parsing check takes place before the cron strings can be added to the policy

@jkowalski jkowalski merged commit cb98abb into kopia:master Jul 20, 2023
27 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Better timed backups
3 participants