13static const char *
const TAG =
"scheduler";
15static const uint32_t MAX_LOGICALLY_DELETED_ITEMS = 10;
26 std::function<
void()> func) {
27 const auto now = this->
millis_();
32 if (timeout == SCHEDULER_DONT_RUN)
36 item->component = component;
39 item->next_execution_ = now + timeout;
40 item->callback = std::move(func);
42#ifdef ESPHOME_DEBUG_SCHEDULER
43 ESP_LOGD(TAG,
"set_timeout(name='%s/%s', timeout=%" PRIu32
")", item->get_source(), name.c_str(), timeout);
45 this->
push_(std::move(item));
51 std::function<
void()> func) {
52 const auto now = this->
millis_();
57 if (interval == SCHEDULER_DONT_RUN)
66 item->component = component;
69 item->interval = interval;
70 item->next_execution_ = now + offset;
71 item->callback = std::move(func);
73#ifdef ESPHOME_DEBUG_SCHEDULER
74 ESP_LOGD(TAG,
"set_interval(name='%s/%s', interval=%" PRIu32
", offset=%" PRIu32
")", item->get_source(),
75 name.c_str(), interval, offset);
77 this->
push_(std::move(item));
85 uint8_t retry_countdown;
86 uint32_t current_interval;
89 float backoff_increase_factor;
93static void retry_handler(
const std::shared_ptr<RetryArgs> &args) {
94 RetryResult const retry_result = args->func(--args->retry_countdown);
98 args->scheduler->set_timeout(args->component, args->name, args->current_interval, [args]() { retry_handler(args); });
100 args->current_interval *= args->backoff_increase_factor;
104 uint8_t max_attempts, std::function<
RetryResult(uint8_t)> func,
105 float backoff_increase_factor) {
109 if (initial_wait_time == SCHEDULER_DONT_RUN)
112 ESP_LOGVV(TAG,
"set_retry(name='%s', initial_wait_time=%" PRIu32
", max_attempts=%u, backoff_factor=%0.1f)",
113 name.c_str(), initial_wait_time, max_attempts, backoff_increase_factor);
115 if (backoff_increase_factor < 0.0001) {
117 "set_retry(name='%s'): backoff_factor cannot be close to zero nor negative (%0.1f). Using 1.0 instead",
118 name.c_str(), backoff_increase_factor);
119 backoff_increase_factor = 1;
122 auto args = std::make_shared<RetryArgs>();
123 args->func = std::move(func);
124 args->retry_countdown = max_attempts;
125 args->current_interval = initial_wait_time;
126 args->component = component;
127 args->name =
"retry$" + name;
128 args->backoff_increase_factor = backoff_increase_factor;
129 args->scheduler =
this;
132 this->
set_timeout(component, args->name, 0, [args]() { retry_handler(args); });
141 auto &item = this->
items_[0];
142 const auto now = this->
millis_();
143 if (item->next_execution_ < now)
145 return item->next_execution_ - now;
148 const auto now = this->
millis_();
151#ifdef ESPHOME_DEBUG_SCHEDULER
152 static uint64_t last_print = 0;
154 if (now - last_print > 2000) {
156 std::vector<std::unique_ptr<SchedulerItem>> old_items;
157 ESP_LOGD(TAG,
"Items: count=%u, now=%" PRIu64
" (%u, %" PRIu32
")", this->
items_.size(), now, this->millis_major_,
161 auto item = std::move(this->
items_[0]);
165 ESP_LOGD(TAG,
" %s '%s/%s' interval=%" PRIu32
" next_execution in %" PRIu64
"ms at %" PRIu64,
166 item->get_type_str(), item->get_source(), item->name.c_str(), item->interval,
167 item->next_execution_ - now, item->next_execution_);
169 old_items.push_back(std::move(item));
175 this->
items_ = std::move(old_items);
181 auto items_was = this->
items_.size();
183 if (
to_remove_ > MAX_LOGICALLY_DELETED_ITEMS) {
184 std::vector<std::unique_ptr<SchedulerItem>> valid_items;
187 auto item = std::move(this->
items_[0]);
189 valid_items.push_back(std::move(item));
194 this->
items_ = std::move(valid_items);
199 ESP_LOGW(TAG,
"to_remove_ was %" PRIu32
" now: %" PRIu32
" items where %zu now %zu. Please report this",
209 auto &item = this->
items_[0];
210 if (item->next_execution_ > now) {
215 if (item->component !=
nullptr && item->component->is_failed()) {
222#ifdef ESPHOME_DEBUG_SCHEDULER
223 ESP_LOGV(TAG,
"Running %s '%s/%s' with interval=%" PRIu32
" next_execution=%" PRIu64
" (now=%" PRIu64
")",
224 item->get_type_str(), item->get_source(), item->name.c_str(), item->interval, item->next_execution_,
232 uint32_t now_ms =
millis();
244 auto item = std::move(this->
items_[0]);
259 item->next_execution_ = now + item->interval;
260 this->
push_(std::move(item));
269 for (
auto &it : this->
to_add_) {
274 this->
items_.push_back(std::move(it));
277 this->to_add_.clear();
280 while (!this->
items_.empty()) {
281 auto &item = this->
items_[0];
299 this->
to_add_.push_back(std::move(item));
305 for (
auto &it : this->
items_) {
306 if (it->component == component && it->name == name && it->type ==
type && !it->remove) {
312 for (
auto &it : this->
to_add_) {
313 if (it->component == component && it->name == name && it->type ==
type) {
322 const uint32_t now =
millis();
325 ESP_LOGD(TAG,
"Incrementing scheduler major at %" PRIu64
"ms",
328 this->last_millis_ = now;
329 return now + (
static_cast<uint64_t
>(this->
millis_major_) << 32);
333 const std::unique_ptr<SchedulerItem> &b) {
334 return a->next_execution_ > b->next_execution_;
void set_current_component(Component *component)
Helper class that wraps a mutex with a RAII-style API.
bool cancel_retry(Component *component, const std::string &name)
bool cancel_item_(Component *component, const std::string &name, SchedulerItem::Type type)
void set_retry(Component *component, const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, std::function< RetryResult(uint8_t)> func, float backoff_increase_factor=1.0f)
bool cancel_timeout(Component *component, const std::string &name)
void push_(std::unique_ptr< SchedulerItem > item)
std::vector< std::unique_ptr< SchedulerItem > > to_add_
bool cancel_interval(Component *component, const std::string &name)
void set_timeout(Component *component, const std::string &name, uint32_t timeout, std::function< void()> func)
void set_interval(Component *component, const std::string &name, uint32_t interval, std::function< void()> func)
optional< uint32_t > next_schedule_in()
std::vector< std::unique_ptr< SchedulerItem > > items_
Providing packet encoding functions for exchanging data with a remote host.
std::unique_ptr< T > make_unique(Args &&...args)
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
static bool cmp(const std::unique_ptr< SchedulerItem > &a, const std::unique_ptr< SchedulerItem > &b)