Apply scheduler_timeout correctly (#6618)

This commit is contained in:
Teddy Reed 2020-09-03 23:56:16 -04:00 committed by GitHub
parent d0b4e327a2
commit cb428e105e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 12 deletions

View File

@ -299,7 +299,7 @@ which does not define an interval.
`--schedule_timeout=0`
Limit the schedule, 0 for no limit. Optionally limit the `osqueryd`'s life by adding a schedule limit in seconds. This should only be used for testing.
Limit the schedule, 0 for no limit. Optionally limit the `osqueryd`'s life by adding a schedule limit in seconds as a duration. This should only be used for testing.
`--disable_tables=table_name1,table_name2`

View File

@ -32,7 +32,10 @@
namespace osquery {
FLAG(uint64, schedule_timeout, 0, "Limit the schedule, 0 for no limit");
FLAG(uint64,
schedule_timeout,
0,
"Limit the schedule to a duration in seconds, 0 for no limit");
FLAG(uint64, schedule_max_drift, 60, "Max time drift in seconds");
@ -209,6 +212,9 @@ void SchedulerRunner::maybeFlushLogs(uint64_t time_step) {
void SchedulerRunner::start() {
// Start the counter at the second.
auto i = osquery::getUnixTime();
// Timeout is the number of seconds from starting.
timeout_ += (timeout_ == 0) ? 0 : i;
for (; (timeout_ == 0) || (i <= timeout_); ++i) {
auto start_time_point = std::chrono::steady_clock::now();
Config::get().scheduledQueries(([&i](const std::string& name,
@ -239,6 +245,12 @@ void SchedulerRunner::start() {
break;
}
}
// Scheduler ended.
if (!interrupted()) {
LOG(INFO) << "The scheduler ended after " << timeout_ << " seconds";
requestShutdown();
}
}
std::chrono::milliseconds SchedulerRunner::getCurrentTimeDrift() const

View File

@ -59,7 +59,7 @@ class SchedulerRunner : public InternalRunnable {
const std::chrono::milliseconds interval_;
/// Maximum number of steps.
const unsigned long int timeout_;
unsigned long int timeout_;
/// Accumulated for some time time drift to compensate.
/// It will be either reduced during compensation process or

View File

@ -170,7 +170,7 @@ TEST_F(SchedulerTests, test_scheduler) {
Config::get().update({{"data", config}});
// Run the scheduler for 1 second with a second interval.
SchedulerRunner runner(static_cast<unsigned long int>(now + 1), 1);
SchedulerRunner runner(static_cast<unsigned long int>(1), 1);
runner.start();
// If a query was executed the cache step will have been advanced.
@ -205,7 +205,7 @@ TEST_F(SchedulerTests, test_scheduler_zero_drift) {
// Run the scheduler for 1 second with a second interval.
SchedulerRunner runner(
static_cast<unsigned long int>(now), size_t{1}, std::chrono::seconds{10});
static_cast<unsigned long int>(1), size_t{1}, std::chrono::seconds{10});
runner.start();
EXPECT_EQ(runner.getCurrentTimeDrift(), std::chrono::milliseconds::zero());
@ -242,10 +242,9 @@ TEST_F(SchedulerTests, test_scheduler_drift_accumulation) {
})config";
Config::get().update({{"data", config}});
// Run the scheduler for 1 second with a second interval.
SchedulerRunner runner(static_cast<unsigned long int>(now + 3),
size_t{0},
std::chrono::seconds{10});
// Run the scheduler for 3 seconds with no interval.
SchedulerRunner runner(
static_cast<unsigned long int>(3), size_t{0}, std::chrono::seconds{10});
runner.start();
EXPECT_GE(runner.getCurrentTimeDrift(), std::chrono::milliseconds{1});
@ -262,7 +261,7 @@ TEST_F(SchedulerTests, test_scheduler_reload) {
auto backup_reload = FLAGS_schedule_reload;
// Start the scheduler;
auto expire = static_cast<unsigned long int>(getUnixTime() + 1);
auto expire = static_cast<unsigned long int>(1);
FLAGS_schedule_reload = 1;
SchedulerRunner runner(expire, 1);
FLAGS_schedule_reload = backup_reload;

View File

@ -116,9 +116,8 @@ TEST_F(TLSConfigTests, test_runner_and_scheduler) {
Config::get().load();
// Start a scheduler runner for 3 seconds.
auto t = static_cast<unsigned long int>(getUnixTime());
ASSERT_TRUE(
Dispatcher::addService(std::make_shared<SchedulerRunner>(t + 1, 1)).ok());
Dispatcher::addService(std::make_shared<SchedulerRunner>(1, 1)).ok());
// Reload our instance config.
ASSERT_TRUE(Config::get().load().ok());