iz
Clone
https://git.tahinli.com/Tahinli/iz.git
0 watching0 forksRustAGPL-3.0567.3 KiB3950aceTahinli27 days ago34 commits
| 1 | //! The sweeper worker: housekeeping that is not urgent. It runs once at start and then every hour until the |
| 2 | //! stop flag is set. Each run deletes expired sessions and expired logout-token records, retries the |
| 3 | //! attachment copies still owed (the deleter in `attachments.rs` does this too, every few seconds; this pass |
| 4 | //! is the backstop), deletes activity and mail-ledger rows older than `RETENTION_DAYS`, deletes audit rows |
| 5 | //! older than `AUDIT_RETENTION_DAYS`, and removes staged uploads left in `data_dir/tmp` by a crash. |
| 6 | //! |
| 7 | //! It never deletes live data: pending mail, pending deletions that are not yet due, and sessions that have |
| 8 | //! not expired are left alone. A failed step is logged and the next step still runs. |
| 9 | |
| 10 | use Future; |
| 11 | use Path; |
| 12 | use ; |
| 13 | |
| 14 | use TMP_DIR; |
| 15 | use AUDIT_RETENTION_DAYS; |
| 16 | use RETENTION_DAYS; |
| 17 | use ; |
| 18 | use watch; |
| 19 | |
| 20 | use crateattachments; |
| 21 | use crateAppState; |
| 22 | |
| 23 | /// How often the sweeper runs after its first pass. Reason: none of this is urgent. |
| 24 | const SWEEP_EVERY: Duration = from_secs; |
| 25 | /// Rows deleted per statement. Reason: keeps each write short so the writer is not held for long. |
| 26 | const BATCH: i64 = 500; |
| 27 | /// A staged upload older than this is an orphan. Reason: no upload stays in staging for a day. |
| 28 | const TEMP_MAX_AGE: Duration = from_secs; |
| 29 | |
| 30 | /// Starts the sweeper. It runs at once, then every [`SWEEP_EVERY`], until `stop` is set. |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | /// One full run. Each step is independent: a failure is logged and the run goes on. |
| 45 | async |
| 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 | /// Logs a step's count when it is not zero, and logs a failed step. Returns nothing: a sweep step has no |
| 83 | /// result the caller needs. |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | /// Runs a batched delete until a batch comes back short, a batch fails, or the step runs out of rows. Logs |
| 93 | /// the total when it is not zero. |
| 94 | async |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | /// Removes regular files in `data_dir/tmp` that were last changed more than [`TEMP_MAX_AGE`] ago. A missing |
| 118 | /// staging directory means there is nothing to remove. Links and directories are left alone. |
| 119 | async |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 |