9 static const char *
const TAG =
"scheduler";
11 static const uint32_t MAX_LOGICALLY_DELETED_ITEMS = 10;
17 std::function<
void()> func) {
18 const uint32_t now = this->
millis_();
23 if (timeout == SCHEDULER_DONT_RUN)
26 ESP_LOGVV(TAG,
"set_timeout(name='%s', timeout=%u)", name.c_str(), timeout);
28 auto item = make_unique<SchedulerItem>();
29 item->component = component;
32 item->timeout = timeout;
33 item->last_execution = now;
35 item->callback = std::move(func);
37 this->
push_(std::move(item));
43 std::function<
void()> func) {
44 const uint32_t now = this->
millis_();
49 if (interval == SCHEDULER_DONT_RUN)
57 ESP_LOGVV(TAG,
"set_interval(name='%s', interval=%u, offset=%u)", name.c_str(), interval, offset);
59 auto item = make_unique<SchedulerItem>();
60 item->component = component;
63 item->interval = interval;
64 item->last_execution = now - offset - interval;
66 if (item->last_execution > now)
67 item->last_execution_major--;
68 item->callback = std::move(func);
70 this->
push_(std::move(item));
77 std::function<RetryResult()> func;
78 uint8_t retry_countdown;
79 uint32_t current_interval;
82 float backoff_increase_factor;
86 static void retry_handler(
const std::shared_ptr<RetryArgs> &args) {
90 args->current_interval *= args->backoff_increase_factor;
91 args->scheduler->set_timeout(args->component, args->name, args->current_interval, [args]() { retry_handler(args); });
95 uint8_t max_attempts, std::function<
RetryResult()> func,
float backoff_increase_factor) {
99 if (initial_wait_time == SCHEDULER_DONT_RUN)
102 ESP_LOGVV(TAG,
"set_retry(name='%s', initial_wait_time=%u, max_attempts=%u, backoff_factor=%0.1f)", name.c_str(),
103 initial_wait_time, max_attempts, backoff_increase_factor);
105 auto args = std::make_shared<RetryArgs>();
106 args->func = std::move(func);
107 args->retry_countdown = max_attempts;
108 args->current_interval = initial_wait_time;
109 args->component = component;
110 args->name =
"retry$" +
name;
111 args->backoff_increase_factor = backoff_increase_factor;
112 args->scheduler =
this;
114 this->
set_timeout(component, args->name, initial_wait_time, [args]() { retry_handler(args); });
123 auto &item = this->
items_[0];
124 const uint32_t now = this->
millis_();
125 uint32_t next_time = item->last_execution + item->interval;
128 return next_time - now;
131 const uint32_t now = this->
millis_();
134 #ifdef ESPHOME_DEBUG_SCHEDULER 135 static uint32_t last_print = 0;
137 if (now - last_print > 2000) {
139 std::vector<std::unique_ptr<SchedulerItem>> old_items;
140 ESP_LOGVV(TAG,
"Items: count=%u, now=%u", this->
items_.size(), now);
142 auto item = std::move(this->
items_[0]);
143 ESP_LOGVV(TAG,
" %s '%s' interval=%u last_execution=%u (%u) next=%u (%u)", item->get_type_str(),
144 item->name.c_str(), item->interval, item->last_execution, item->last_execution_major,
145 item->next_execution(), item->next_execution_major());
148 old_items.push_back(std::move(item));
150 ESP_LOGVV(TAG,
"\n");
151 this->
items_ = std::move(old_items);
153 #endif // ESPHOME_DEBUG_SCHEDULER 156 auto items_was =
items_.size();
158 if (
to_remove_ > MAX_LOGICALLY_DELETED_ITEMS) {
159 std::vector<std::unique_ptr<SchedulerItem>> valid_items;
161 auto item = std::move(this->
items_[0]);
163 valid_items.push_back(std::move(item));
165 this->
items_ = std::move(valid_items);
169 ESP_LOGW(TAG,
"to_remove_ was %u now: %u items where %zu now %zu. Please report this", to_remove_was,
to_remove_,
170 items_was,
items_.size());
179 auto &item = this->
items_[0];
180 if ((now - item->last_execution) < item->interval) {
184 uint8_t major = item->next_execution_major();
189 if (item->component !=
nullptr && item->component->is_failed()) {
194 #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE 195 ESP_LOGVV(TAG,
"Running %s '%s' with interval=%u last_execution=%u (now=%u)", item->get_type_str(),
196 item->name.c_str(), item->interval, item->last_execution, now);
210 auto item = std::move(this->
items_[0]);
223 if (item->interval != 0) {
224 const uint32_t before = item->last_execution;
225 const uint32_t amount = (now - item->last_execution) / item->interval;
226 item->last_execution += amount * item->interval;
227 if (item->last_execution < before)
228 item->last_execution_major++;
230 this->
push_(std::move(item));
238 for (
auto &it : this->
to_add_) {
243 this->
items_.push_back(std::move(it));
246 this->to_add_.clear();
249 while (!this->
items_.empty()) {
250 auto &item = this->
items_[0];
265 for (
auto &it : this->
items_) {
266 if (it->component == component && it->name == name && it->type == type && !it->remove) {
272 for (
auto &it : this->
to_add_) {
273 if (it->component == component && it->name == name && it->type == type) {
282 const uint32_t now =
millis();
284 ESP_LOGD(TAG,
"Incrementing scheduler major");
287 this->last_millis_ = now;
292 const std::unique_ptr<SchedulerItem> &b) {
295 uint32_t a_next_exec = a->next_execution();
296 uint8_t a_next_exec_major = a->next_execution_major();
297 uint32_t b_next_exec = b->next_execution();
298 uint8_t b_next_exec_major = b->next_execution_major();
300 if (a_next_exec_major != b_next_exec_major) {
312 uint8_t diff1 = a_next_exec_major - b_next_exec_major;
313 uint8_t diff2 = b_next_exec_major - a_next_exec_major;
314 return diff1 < diff2;
317 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()
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)
std::vector< std::unique_ptr< SchedulerItem > > items_
void set_retry(Component *component, const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, std::function< RetryResult()> func, float backoff_increase_factor=1.0f)
void set_interval(Component *component, const std::string &name, uint32_t interval, std::function< void()> func)