in
Clone
https://git.tahinli.com/Tahinli/in.git
0 watching0 forksRustAGPL-3.0528.7 KiB9215377Tahinli27 days ago37 commits
| 1 | //! The scheduled backup worker: writes a backup set (snapshot, blob manifest and its MAC, see `in-core` `backup.rs`) |
| 2 | //! when one is due, keeps the newest `keep` sets, and pings the dead-man URL after each good run. |
| 3 | //! |
| 4 | //! A run is due when no attempt is recorded yet, or the last attempt is at least `interval_hours` old (see |
| 5 | //! [`is_due`]). The worker checks every [`TICK`]. The attempt time is written before the run starts, and it lives |
| 6 | //! in the database, so a restart does not reset the interval, and a crash-looping server cannot write a set on |
| 7 | //! every start. A failing run is retried at the interval, not every tick. |
| 8 | //! |
| 9 | //! The admin page can ask for a run now through [`BackupTrigger`]. At most one run is pending or running: a request |
| 10 | //! made meanwhile starts nothing, and a scheduled run that is already going counts as running too. |
| 11 | //! |
| 12 | //! A failed run is logged and the next one is tried at the next interval; a failed ping is logged and nothing |
| 13 | //! else. The ping URL carries a secret, so it is never logged. The worker is not started without a `[backup]` |
| 14 | //! table. |
| 15 | //! |
| 16 | //! After every run, scheduled or manual, the outcome is recorded in the `settings` table (see [`record_outcome`]) |
| 17 | //! so the admin page can show the last success, the last attempt and the error of a failed run. The recorded |
| 18 | //! error is short text, never the ping URL. |
| 19 | |
| 20 | use ; |
| 21 | use Duration; |
| 22 | |
| 23 | use backup; |
| 24 | use Backup; |
| 25 | use Error; |
| 26 | use Store; |
| 27 | use ; |
| 28 | use ; |
| 29 | |
| 30 | use crateAppState; |
| 31 | |
| 32 | /// How long the dead-man ping may take. |
| 33 | pub const PING_TIMEOUT: Duration = from_secs; |
| 34 | |
| 35 | /// How often the worker checks whether a run is due. A minute is short enough that a run starts close to its time, |
| 36 | /// and a check reads one setting, so it costs nothing. |
| 37 | const TICK: Duration = from_secs; |
| 38 | |
| 39 | /// Settings key: when a run was last attempted (a stamp). Written before every run, so a run that is cut short |
| 40 | /// still counts as an attempt. |
| 41 | pub const LAST_ATTEMPT_AT: &str = "backup.last_attempt_at"; |
| 42 | /// Settings key: when a run last succeeded (a stamp). |
| 43 | pub const LAST_SUCCESS_AT: &str = "backup.last_success_at"; |
| 44 | /// Settings key: the short error of the latest run. Present only while the latest run failed. |
| 45 | pub const LAST_ERROR: &str = "backup.last_error"; |
| 46 | /// The longest error text that is stored, in characters. Long errors are cut so the admin page stays short. |
| 47 | const ERROR_CHARS: usize = 200; |
| 48 | /// The shortest time without a successful backup before the admin page warns: two days, in seconds. The |
| 49 | /// limit is never shorter than two backup intervals (see [`is_stale`]). |
| 50 | pub const STALE_AFTER: Duration = from_secs; |
| 51 | |
| 52 | /// Asks the backup worker for a run now. At most one run is pending or running: a request made while one is |
| 53 | /// pending or running starts nothing. |
| 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 | /// Runs one backup now and returns the new set's directory. |
| 90 | async |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | /// The short text of a failed run: the first line of its error, without control characters, cut to |
| 107 | /// [`ERROR_CHARS`] characters. |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | /// Stores the attempt time before a run starts. A store failure is logged and never stops the run. |
| 118 | async |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | /// Stores the outcome of one run: on success the success time and no error; on failure the short error. A store |
| 125 | /// failure is logged and never fails the run. |
| 126 | async |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | /// True when a run is due: none was attempted, or the last attempt is at least `interval_hours` old. The stamps |
| 144 | /// have a fixed width, so comparing them as text compares their times. |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | /// True when there is no successful backup within the limit: [`STALE_AFTER`] or two backup intervals, |
| 153 | /// whichever is longer, so a weekly schedule is not always overdue. The stamps have a fixed width, so |
| 154 | /// comparing them as text compares their times. |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | /// Pings the dead-man URL once. Only failures are logged, and the URL itself never is. |
| 162 | async |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | /// Runs backups until the stop flag is set. Every [`TICK`] it checks whether a run is due (see [`is_due`]); a |
| 180 | /// request from the admin page starts a run at once. |
| 181 | pub async |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | /// True when a scheduled run is due, by the attempt recorded in the database. A store failure is logged and |
| 211 | /// counts as not due, so the next tick tries again. |
| 212 | async |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 |