im
Clone
https://git.tahinli.com/Tahinli/im.git
0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits
| 1 | //! Scheduled backups: when a run is due, the set is written, older sets are pruned, and the outcome is |
| 2 | //! recorded and sent to the dead-man URL. |
| 3 | //! |
| 4 | //! The worker runs only when `[backup] dir` is set. A run is due when none was attempted yet, or when the |
| 5 | //! last attempt is at least `interval_hours` old. The attempt time is recorded before the run, so a failing |
| 6 | //! run is retried at the interval and not every minute. The same [`run_and_record`] serves `im backup now`. |
| 7 | //! |
| 8 | //! The admin health page asks for a run now through [`BackupTrigger`]. At most one run is pending or running: |
| 9 | //! a request made meanwhile starts nothing. `im backup now` is still the way to back up while the server is |
| 10 | //! stopped, because it cannot run beside the server's data directory lock. |
| 11 | //! |
| 12 | //! The dead-man URL gets `<url>/start` before a run, `<url>` after a good run and `<url>/fail` after a bad |
| 13 | //! one. A ping that fails is logged and never fails the backup. |
| 14 | |
| 15 | use ; |
| 16 | use Duration; |
| 17 | |
| 18 | use backup; |
| 19 | use Config; |
| 20 | use Store; |
| 21 | use ; |
| 22 | use Notify; |
| 23 | use JoinHandle; |
| 24 | |
| 25 | use cratehttp_client; |
| 26 | use cratestopped; |
| 27 | use crateAppState; |
| 28 | |
| 29 | /// How often the worker checks whether a run is due. |
| 30 | const TICK: Duration = from_secs; |
| 31 | |
| 32 | /// Asks the backup worker for a run now. A run is pending or running at most once: a request made while one is |
| 33 | /// pending or running starts nothing. |
| 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 | /// Settings key: when a run was last attempted (a stamp). |
| 70 | pub const LAST_ATTEMPT: &str = "backup.last_attempt"; |
| 71 | /// Settings key: when a run last succeeded (a stamp). |
| 72 | pub const LAST_SUCCESS: &str = "backup.last_success"; |
| 73 | /// Settings key: the reason the last run failed, when it did. Never a path to a secret. |
| 74 | pub const LAST_ERROR: &str = "backup.last_error"; |
| 75 | |
| 76 | /// Starts the worker when backups are configured. Returns `None` when they are off. The task ends when |
| 77 | /// the stop flag is set. |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | /// True when no run was attempted yet, or the last attempt is at least `interval_hours` old. |
| 101 | async |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | /// Writes one set, prunes the old ones and records the outcome. Returns the set's directory on success. |
| 114 | /// |
| 115 | /// The outcome is recorded in `settings`, so the health page can show it. A failure is returned to the |
| 116 | /// caller as well, so `im backup now` can exit with status 1. |
| 117 | pub async |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | /// Writes the set and removes the sets beyond `keep`. |
| 158 | async |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | /// Sends one dead-man ping. Failures are logged; they never fail the backup. The URL holds the healthcheck |
| 173 | /// id, a secret, so a transport error is logged without it. |
| 174 | async |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 |