| 890 | } |
| 891 | |
| 892 | void Server::cron() { |
| 893 | uint64_t counter = 0; |
| 894 | while (!stop_) { |
| 895 | // Sleep first |
| 896 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 897 | |
| 898 | // To guarantee accessing DB safely |
| 899 | auto guard = storage->ReadLockGuard(); |
| 900 | if (storage->IsClosing()) continue; |
| 901 | |
| 902 | updateCachedTime(); |
| 903 | counter++; |
| 904 | |
| 905 | if (is_loading_) { |
| 906 | // We need to skip the cron operations since `is_loading_` means the db is restoring, |
| 907 | // and the db pointer will be modified after that. It will panic if we use the db pointer |
| 908 | // before the new db was reopened. |
| 909 | continue; |
| 910 | } |
| 911 | |
| 912 | // check every 20s (use 20s instead of 60s so that cron will execute in critical condition) |
| 913 | if (counter != 0 && counter % 200 == 0) { |
| 914 | auto t = static_cast<time_t>(util::GetTimeStamp()); |
| 915 | std::tm now{}; |
| 916 | localtime_r(&t, &now); |
| 917 | // disable compaction cron when the compaction checker was enabled |
| 918 | if (!config_->compaction_checker_cron.IsEnabled() && config_->compact_cron.IsEnabled() && |
| 919 | config_->compact_cron.IsTimeMatch(&now)) { |
| 920 | Status s = AsyncCompactDB(); |
| 921 | INFO("[server] Schedule to compact the db, result: {}", s.Msg()); |
| 922 | } |
| 923 | if (config_->bgsave_cron.IsEnabled() && config_->bgsave_cron.IsTimeMatch(&now)) { |
| 924 | Status s = AsyncBgSaveDB(); |
| 925 | INFO("[server] Schedule to bgsave the db, result: {}", s.Msg()); |
| 926 | } |
| 927 | if (config_->dbsize_scan_cron.IsEnabled() && config_->dbsize_scan_cron.IsTimeMatch(&now)) { |
| 928 | auto tokens = namespace_.List(); |
| 929 | std::vector<std::string> namespaces; |
| 930 | |
| 931 | // Number of namespaces (custom namespaces + default one) |
| 932 | namespaces.reserve(tokens.size() + 1); |
| 933 | for (auto &token : tokens) { |
| 934 | namespaces.emplace_back(token.second); // namespace |
| 935 | } |
| 936 | |
| 937 | // add default namespace as fallback |
| 938 | namespaces.emplace_back(kDefaultNamespace); |
| 939 | |
| 940 | for (auto &ns : namespaces) { |
| 941 | Status s = AsyncScanDBSize(ns); |
| 942 | INFO("[server] Schedule to recalculate the db size on namespace: {}, result: {}", ns, s.Msg()); |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | // check every 10s |
| 947 | if (counter != 0 && counter % 100 == 0) { |
| 948 | Status s = AsyncPurgeOldBackups(config_->max_backup_to_keep, config_->max_backup_keep_hours); |
| 949 |
no test coverage detected