-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrotate_date.R
134 lines (115 loc) · 2.05 KB
/
rotate_date.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#' @rdname rotate
#' @export
rotate_date <- function(
file,
age = 1,
size = 1,
max_backups = Inf,
compression = FALSE,
format = "%Y-%m-%d",
dir = dirname(file),
overwrite = FALSE,
create_file = TRUE,
now = Sys.Date(),
dry_run = FALSE,
verbose = dry_run
){
rotate_date_internal(
file = file,
age = age,
format = format,
size = size,
max_backups = max_backups,
compression = compression,
overwrite = overwrite,
dir = dir,
now = now,
dry_run = dry_run,
verbose = verbose,
create_file = create_file,
do_rotate = TRUE
)
}
#' @rdname rotate
#' @export
backup_date <- function(
file,
age = 1,
size = 1,
max_backups = Inf,
compression = FALSE,
format = "%Y-%m-%d",
dir = dirname(file),
overwrite = FALSE,
now = Sys.Date(),
dry_run = FALSE,
verbose = dry_run
){
rotate_date_internal(
file = file,
age = age,
format = format,
size = size,
max_backups = max_backups,
compression = compression,
overwrite = overwrite,
dir = dir,
now = now,
dry_run = dry_run,
verbose = verbose,
do_rotate = FALSE,
create_file = FALSE
)
}
rotate_date_internal <- function(
file,
age,
format,
size,
max_backups,
compression,
overwrite,
create_file,
dir,
now,
do_rotate,
dry_run,
verbose
){
stopifnot(
is_scalar_bool(do_rotate),
is_scalar_bool(dry_run),
is_scalar_bool(verbose),
is_scalar_bool(create_file)
)
assert_pure_BackupQueue(file, dir = dir, warn_only = TRUE)
if (dry_run){
DRY_RUN$activate()
on.exit(DRY_RUN$deactivate())
}
bq <- BackupQueueDate$new(
file,
fmt = format,
dir = dir,
compression = compression
)
# backup
if (bq$should_rotate(size = size, age = age, now = now, verbose = verbose)){
bq$push(
now = now,
overwrite = overwrite
)
} else {
do_rotate <- FALSE
}
# prune
bq$prune(max_backups)
# rotate
if (do_rotate){
file_remove(file)
if (create_file){
file_create(file)
}
}
invisible(file)
}