10 static const char *
const TAG =
"scheduler";
12 static const uint32_t MAX_LOGICALLY_DELETED_ITEMS = 10;
23 std::function<
void()> func) {
24 const uint32_t now = this->
millis_();
29 if (timeout == SCHEDULER_DONT_RUN)
32 ESP_LOGVV(TAG,
"set_timeout(name='%s', timeout=%" PRIu32
")", name.c_str(), timeout);
34 auto item = make_unique<SchedulerItem>();
35 item->component = component;
38 item->timeout = timeout;
39 item->last_execution = now;
41 item->callback = std::move(func);
43 this->
push_(std::move(item));
49 std::function<
void()> func) {
50 const uint32_t now = this->
millis_();
55 if (interval == SCHEDULER_DONT_RUN)
63 ESP_LOGVV(TAG,
"set_interval(name='%s', interval=%" PRIu32
", offset=%" PRIu32
")", name.c_str(), interval, offset);
65 auto item = make_unique<SchedulerItem>();
66 item->component = component;
69 item->interval = interval;
70 item->last_execution = now - offset - interval;
72 if (item->last_execution > now)
73 item->last_execution_major--;
74 item->callback = std::move(func);
76 this->
push_(std::move(item));
83 std::function<RetryResult(uint8_t)> func;
84 uint8_t retry_countdown;
85 uint32_t current_interval;
88 float backoff_increase_factor;
92 static void retry_handler(
const std::shared_ptr<RetryArgs> &args) {
93 RetryResult const retry_result = args->func(--args->retry_countdown);
97 args->scheduler->set_timeout(args->component, args->name, args->current_interval, [args]() { retry_handler(args); });
99 args->current_interval *= args->backoff_increase_factor;
103 uint8_t max_attempts, std::function<
RetryResult(uint8_t)> func,
104 float backoff_increase_factor) {
108 if (initial_wait_time == SCHEDULER_DONT_RUN)
111 ESP_LOGVV(TAG,
"set_retry(name='%s', initial_wait_time=%" PRIu32
", max_attempts=%u, backoff_factor=%0.1f)",
112 name.c_str(), initial_wait_time, max_attempts, backoff_increase_factor);
114 if (backoff_increase_factor < 0.0001) {
116 "set_retry(name='%s'): backoff_factor cannot be close to zero nor negative (%0.1f). Using 1.0 instead",
117 name.c_str(), backoff_increase_factor);
118 backoff_increase_factor = 1;
121 auto args = std::make_shared<RetryArgs>();
122 args->func = std::move(func);
123 args->retry_countdown = max_attempts;
124 args->current_interval = initial_wait_time;
125 args->component = component;
126 args->name =
"retry$" +
name;
127 args->backoff_increase_factor = backoff_increase_factor;
128 args->scheduler =
this;
131 this->
set_timeout(component, args->name, 0, [args]() { retry_handler(args); });
140 auto &item = this->
items_[0];
141 const uint32_t now = this->
millis_();
142 uint32_t next_time = item->last_execution + item->interval;
145 return next_time - now;
148 const uint32_t now = this->
millis_();
151 #ifdef ESPHOME_DEBUG_SCHEDULER 152 static uint32_t last_print = 0;
154 if (now - last_print > 2000) {
156 std::vector<std::unique_ptr<SchedulerItem>> old_items;
157 ESP_LOGVV(TAG,
"Items: count=%u, now=%" PRIu32, this->
items_.size(), now);
160 auto item = std::move(this->
items_[0]);
164 ESP_LOGVV(TAG,
" %s '%s' interval=%" PRIu32
" last_execution=%" PRIu32
" (%u) next=%" PRIu32
" (%u)",
165 item->get_type_str(), item->name.c_str(), item->interval, item->last_execution,
166 item->last_execution_major, item->next_execution(), item->next_execution_major());
168 old_items.push_back(std::move(item));
170 ESP_LOGVV(TAG,
"\n");
174 this->
items_ = std::move(old_items);
177 #endif // ESPHOME_DEBUG_SCHEDULER 180 auto items_was = this->
items_.size();
182 if (
to_remove_ > MAX_LOGICALLY_DELETED_ITEMS) {
183 std::vector<std::unique_ptr<SchedulerItem>> valid_items;
186 auto item = std::move(this->
items_[0]);
188 valid_items.push_back(std::move(item));
193 this->
items_ = std::move(valid_items);
198 ESP_LOGW(TAG,
"to_remove_ was %" PRIu32
" now: %" PRIu32
" items where %zu now %zu. Please report this",
208 auto &item = this->
items_[0];
209 if ((now - item->last_execution) < item->interval) {
213 uint8_t
major = item->next_execution_major();
218 if (item->component !=
nullptr && item->component->is_failed()) {
224 #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE 225 ESP_LOGVV(TAG,
"Running %s '%s' with interval=%" PRIu32
" last_execution=%" PRIu32
" (now=%" PRIu32
")",
226 item->get_type_str(), item->name.c_str(), item->interval, item->last_execution, now);
242 auto item = std::move(this->
items_[0]);
257 if (item->interval != 0) {
258 const uint32_t before = item->last_execution;
259 const uint32_t amount = (now - item->last_execution) / item->interval;
260 item->last_execution += amount * item->interval;
261 if (item->last_execution < before)
262 item->last_execution_major++;
264 this->
push_(std::move(item));
273 for (
auto &it : this->
to_add_) {
278 this->
items_.push_back(std::move(it));
281 this->to_add_.clear();
284 while (!this->
items_.empty()) {
285 auto &item = this->
items_[0];
303 this->
to_add_.push_back(std::move(item));
309 for (
auto &it : this->
items_) {
310 if (it->component == component && it->name == name && it->type == type && !it->remove) {
316 for (
auto &it : this->
to_add_) {
317 if (it->component == component && it->name == name && it->type == type) {
326 const uint32_t now =
millis();
328 ESP_LOGD(TAG,
"Incrementing scheduler major");
331 this->last_millis_ = now;
336 const std::unique_ptr<SchedulerItem> &b) {
339 uint32_t a_next_exec = a->next_execution();
340 uint8_t a_next_exec_major = a->next_execution_major();
341 uint32_t b_next_exec = b->next_execution();
342 uint8_t b_next_exec_major = b->next_execution_major();
344 if (a_next_exec_major != b_next_exec_major) {
356 uint8_t diff1 = a_next_exec_major - b_next_exec_major;
357 uint8_t diff2 = b_next_exec_major - a_next_exec_major;
358 return diff1 < diff2;
361 return a_next_exec > b_next_exec;
void push_(std::unique_ptr< SchedulerItem > item)
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
bool cancel_timeout(Component *component, const std::string &name)
optional< uint32_t > next_schedule_in()
uint32_t IRAM_ATTR HOT millis()
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)
std::vector< std::unique_ptr< SchedulerItem > > to_add_
bool cancel_retry(Component *component, const std::string &name)
bool cancel_item_(Component *component, const std::string &name, SchedulerItem::Type type)
static bool cmp(const std::unique_ptr< SchedulerItem > &a, const std::unique_ptr< SchedulerItem > &b)
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)
Implementation of SPI Controller mode.
std::vector< std::unique_ptr< SchedulerItem > > items_
Helper class that wraps a mutex with a RAII-style API.
void set_interval(Component *component, const std::string &name, uint32_t interval, std::function< void()> func)