|
| 1 | +# Long-Running Tasks |
| 2 | + |
| 3 | +Long-running (i.e., periodic) tasks are periodically scheduled on an interval. |
| 4 | +This can be used for things like monitoring and batching requests. |
| 5 | + |
| 6 | +Below is an example of PollStatsTask, which gets periodically scheduled. |
| 7 | +```cpp |
| 8 | +struct PollStatsTask : public Task, TaskFlags<TF_SRL_SYM> { |
| 9 | + OUT BdevStats stats_; |
| 10 | + |
| 11 | + /** SHM default constructor */ |
| 12 | + HSHM_INLINE_CROSS_FUN |
| 13 | + explicit PollStatsTask(const hipc::CtxAllocator<CHI_ALLOC_T> &alloc) |
| 14 | + : Task(alloc) {} |
| 15 | + |
| 16 | + /** Emplace constructor */ |
| 17 | + HSHM_INLINE_CROSS_FUN |
| 18 | + explicit PollStatsTask(const hipc::CtxAllocator<CHI_ALLOC_T> &alloc, |
| 19 | + const TaskNode &task_node, const PoolId &pool_id, |
| 20 | + const DomainQuery &dom_query, u32 period_ms) |
| 21 | + : Task(alloc) { |
| 22 | + // Initialize task |
| 23 | + task_node_ = task_node; |
| 24 | + pool_ = pool_id; |
| 25 | + method_ = Method::kPollStats; |
| 26 | + if (period_ms) { |
| 27 | + task_flags_.SetBits(TASK_PERIODIC); |
| 28 | + prio_ = TaskPrioOpt::kHighLatency; |
| 29 | + } else { |
| 30 | + task_flags_.SetBits(0); |
| 31 | + prio_ = TaskPrioOpt::kLowLatency; |
| 32 | + } |
| 33 | + dom_query_ = dom_query; |
| 34 | + |
| 35 | + SetPeriodMs(period_ms); |
| 36 | + HILOG(kInfo, "PollStatsTask: period_ms={}", period_ms); |
| 37 | + } |
| 38 | +}; |
| 39 | +``` |
| 40 | +
|
| 41 | +There are two things that make a task scheduled periodically. |
| 42 | +1. ``task_flags_.SetBits(TASK_PERIODIC)``: This will mark the task as periodically scheduled. There is now a |
| 43 | +``TASK_LONG_RUNNING`` flag which is an alias to ``TASK_PERIODIC``. |
| 44 | +1. ``SetPeriodMs(period_ms)``: This sets the minimum amount of time to elapse before rescheduling this task. |
| 45 | +
|
| 46 | +There are several different timing functions: |
| 47 | +1. ``SetPeriodNs`` |
| 48 | +1. ``SetPeriodUs`` |
| 49 | +1. ``SetPeriodMs`` |
| 50 | +1. ``SetPeriodSec`` |
| 51 | +1. ``SetPeriodMin`` |
0 commit comments