set-file-ttl.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233
  1. // Give every stored file an expiry.
  2. //
  3. // A file's TTL could only be set at upload before 2.8, so everything written
  4. // before that is immortal - and FileRecord carries no expiry field, so a file
  5. // that already has one cannot be told from a file that does not. The only
  6. // available move is to set one on all of them.
  7. //
  8. // 90 days: these are working data - source photos, generated images, downloads -
  9. // all of it either regenerable or already published elsewhere. Long enough that
  10. // nothing in flight is lost, short enough that the store does not grow for ever.
  11. #include <smartbotic/database/client.hpp>
  12. #include <cstdio>
  13. #include <set>
  14. using namespace smartbotic::database;
  15. int main(int argc, char** argv) {
  16. const uint32_t ttl = (argc > 1) ? (uint32_t)atoi(argv[1]) : 90u*24*3600;
  17. Client::Config cfg; cfg.address="192.168.2.150:9004"; cfg.project="smartbotic-automation";
  18. Client c(cfg);
  19. if (!c.connect()) return 1;
  20. std::set<std::string> seen; int set_ok=0, failed=0;
  21. for (uint32_t off=0; off<5000; off+=100) {
  22. auto page = c.listFiles("", "", 100, off);
  23. if (page.files.empty()) break;
  24. for (auto& f : page.files) {
  25. if (!seen.insert(f.id).second) continue;
  26. if (c.setFileTtl(f.id, ttl)) set_ok++; else failed++;
  27. }
  28. if (page.files.size() < 100) break;
  29. }
  30. printf("files seen %zu, expiry set on %d, failed %d (ttl %u s = %.0f days)\n",
  31. seen.size(), set_ok, failed, ttl, ttl/86400.0);
  32. return failed ? 1 : 0;
  33. }