11 static const char *
const TAG =
"scheduler";
13 static const uint32_t MAX_LOGICALLY_DELETED_ITEMS = 10;
24 std::function<
void()> func) {
25 const auto now = this->
millis_();
30 if (timeout == SCHEDULER_DONT_RUN)
33 auto item = make_unique<SchedulerItem>();
34 item->component = component;
37 item->next_execution_ = now + timeout;
38 item->callback = std::move(func);
40 #ifdef ESPHOME_DEBUG_SCHEDULER 41 ESP_LOGD(TAG,
"set_timeout(name='%s/%s', timeout=%" PRIu32
")", item->get_source(), name.c_str(), timeout);
43 this->
push_(std::move(item));
49 std::function<
void()> func) {
50 const auto now = this->
millis_();
55 if (interval == SCHEDULER_DONT_RUN)
63 auto item = make_unique<SchedulerItem>();
64 item->component = component;
67 item->interval = interval;
68 item->next_execution_ = now + offset;
69 item->callback = std::move(func);
71 #ifdef ESPHOME_DEBUG_SCHEDULER 72 ESP_LOGD(TAG,
"set_interval(name='%s/%s', interval=%" PRIu32
", offset=%" PRIu32
")", item->get_source(),
73 name.c_str(), interval, offset);
75 this->
push_(std::move(item));
82 std::function<RetryResult(uint8_t)> func;
83 uint8_t retry_countdown;
84 uint32_t current_interval;
87 float backoff_increase_factor;
91 static void retry_handler(
const std::shared_ptr<RetryArgs> &args) {
92 RetryResult const retry_result = args->func(--args->retry_countdown);
96 args->scheduler->set_timeout(args->component, args->name, args->current_interval, [args]() { retry_handler(args); });
98 args->current_interval *= args->backoff_increase_factor;
102 uint8_t max_attempts, std::function<
RetryResult(uint8_t)> func,
103 float backoff_increase_factor) {
107 if (initial_wait_time == SCHEDULER_DONT_RUN)
110 ESP_LOGVV(TAG,
"set_retry(name='%s', initial_wait_time=%" PRIu32
", max_attempts=%u, backoff_factor=%0.1f)",
111 name.c_str(), initial_wait_time, max_attempts, backoff_increase_factor);
113 if (backoff_increase_factor < 0.0001) {
115 "set_retry(name='%s'): backoff_factor cannot be close to zero nor negative (%0.1f). Using 1.0 instead",
116 name.c_str(), backoff_increase_factor);
117 backoff_increase_factor = 1;
120 auto args = std::make_shared<RetryArgs>();
121 args->func = std::move(func);
122 args->retry_countdown = max_attempts;
123 args->current_interval = initial_wait_time;
124 args->component = component;
125 args->name =
"retry$" +
name;
126 args->backoff_increase_factor = backoff_increase_factor;
127 args->scheduler =
this;
130 this->
set_timeout(component, args->name, 0, [args]() { retry_handler(args); });
139 auto &item = this->
items_[0];
140 const auto now = this->
millis_();
141 if (item->next_execution_ < now)
143 return item->next_execution_ - now;
146 const auto now = this->
millis_();
149 #ifdef ESPHOME_DEBUG_SCHEDULER 150 static uint64_t last_print = 0;
152 if (now - last_print > 2000) {
154 std::vector<std::unique_ptr<SchedulerItem>> old_items;
155 ESP_LOGD(TAG,
"Items: count=%u, now=%" PRIu64
" (%u, %" PRIu32
")", this->
items_.size(), now, this->
millis_major_,
159 auto item = std::move(this->
items_[0]);
163 ESP_LOGD(TAG,
" %s '%s/%s' interval=%" PRIu32
" next_execution in %" PRIu64
"ms at %" PRIu64,
164 item->get_type_str(), item->get_source(), item->name.c_str(), item->interval,
165 item->next_execution_ - now, item->next_execution_);
167 old_items.push_back(std::move(item));
173 this->
items_ = std::move(old_items);
176 #endif // ESPHOME_DEBUG_SCHEDULER 179 auto items_was = this->
items_.size();
181 if (
to_remove_ > MAX_LOGICALLY_DELETED_ITEMS) {
182 std::vector<std::unique_ptr<SchedulerItem>> valid_items;
185 auto item = std::move(this->
items_[0]);
187 valid_items.push_back(std::move(item));
192 this->
items_ = std::move(valid_items);
197 ESP_LOGW(TAG,
"to_remove_ was %" PRIu32
" now: %" PRIu32
" items where %zu now %zu. Please report this",
207 auto &item = this->
items_[0];
208 if (item->next_execution_ > now) {
213 if (item->component !=
nullptr && item->component->is_failed()) {
219 #ifdef ESPHOME_DEBUG_SCHEDULER 220 ESP_LOGV(TAG,
"Running %s '%s/%s' with interval=%" PRIu32
" next_execution=%" PRIu64
" (now=%" PRIu64
")",
221 item->get_type_str(), item->get_source(), item->name.c_str(), item->interval, item->next_execution_,
238 auto item = std::move(this->
items_[0]);
253 item->next_execution_ = now + item->interval;
254 this->
push_(std::move(item));
263 for (
auto &it : this->
to_add_) {
268 this->
items_.push_back(std::move(it));
271 this->to_add_.clear();
274 while (!this->
items_.empty()) {
275 auto &item = this->
items_[0];
293 this->
to_add_.push_back(std::move(item));
299 for (
auto &it : this->
items_) {
300 if (it->component == component && it->name == name && it->type == type && !it->remove) {
306 for (
auto &it : this->
to_add_) {
307 if (it->component == component && it->name == name && it->type == type) {
316 const uint32_t now =
millis();
319 ESP_LOGD(TAG,
"Incrementing scheduler major at %" PRIu64
"ms",
322 this->last_millis_ = now;
323 return now + (
static_cast<uint64_t
>(this->
millis_major_) << 32);
327 const std::unique_ptr<SchedulerItem> &b) {
328 return a->next_execution_ > b->next_execution_;
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)