Crate async_tasks_recorder
source ·Expand description
§Introduction
A struct for recording execution status of async tasks with async methods.
Functions:
- Able to host
Future
s and query whether they are not found, successful, failed, or running. - Able to host
Future
s to revoke the succeededFuture
s and make them not found.
Dependency:
- Depend on
tokio
with featurert
, so cannot use other async runtimes. - Depend on scc for async
HashSet
.
Use this crate if:
- Easy to generate an unique
task_id
(not necessarilyString
) for a future (task). - Don’t want tasks with the same
task_id
to succeed more than once. - Want to record and query all succeeded tasks and failed tasks.
- Want to handle every task in the same state (not just focus on one state).
- Need linearizable query.
- Want to revoke a task, and don’t want the revoking to succeed more than once.
A recorder can only use one task_id
type. The type of task_id
should be:
Eq + Hash + Clone + Send + Sync + 'static
- Cheap to clone (sometimes can use
Arc
).
§Usage
Launch a task with a unique task_id
and a Future
by launch.
Query the state of the task with its task_id
by query_task_state or query_task_state_quick.
Revoke a task with its task_id
and a Future
for revoking by revoke_task_block.
§Skills
Remember that you can add anything in the Future
to achieve the functionality you want.
For example:
- Handle your
Result
inFuture
, and then return empty resultResult<(),()>
. - Send a message to a one shot channel at the end of the
Future
to notify upper level that “this task done”. Don’t forget to consider usingtokio::spawn
when the channel may not complete sending immediately. - Set other callback functions.
It’s still efficient to store metadata of tasks at external scc::HashMap
(task_id
-> metadata).
It is recommended to directly look at the source code (about 150 line) if there is any confusion.
§When Shouldn’t Use This Crate
The consumption of all operations in this crate and cloning times
is about two to three times that of the implementation using scc::Hashmap
.
This crate use three HashSet
to make it easy to operate all tasks in the same state,
And two more HashSet
for linearizability of query and supporting revoking operation.
Note that scc
’s containers have less contention in single access when it grows larger.
Therefore, if you don’t need operating every task in the same state,
then just use scc::HashMap
(task_id
-> task_status
) to build a simpler implementation,
which might have less contention and cloning, but more expansive to iterate.
And the scc::HashMap::update_async
could be a powerful tool for atomic operations.
You should also avoid using this crate if you just want to handle every task in only one state.
For example, if you just want to manage the failed tasks,
then you should use scc::HashMap
to record tasks’ states,
and insert the failed tasks into an external Arc<scc::HashSet>
in Future
.
§Theory & Design
§Abstract Model
Here is the three-level structure for thinking about tasks’ status:
- Level 0:
real_not_found
,real_failed
,real_working
,real_success
: Exact status of the tasks in the CPU (seen by God). - Level 1:
failed_tasks
,working_tasks
,success_tasks
: Containers to storetask_id
s (atask_id
can be stored in 0 to 2 containers simultaneously). - Level 2:
NotFound
,Failed
,Working
,Success
: States of the task that could be obtained byquery_task_state
.
§State Transition Diagram
NotFound
---->Working
(first launch)Working
---->Failed
(task failed)Failed
---->Working
(first launch after failed)Working
---->Success
(task success)Success
---->NotFound
(revoke)
If you equivalent NotFound
to Failed
, and ignore revoke
, then:
Failed
<---> Working
----> Success
§Nature
§About Task
- A task is launched by passing a
Future<Output=Result<R, E>>
with uniquetask_id
. - A task is
real_success
when returnOk(R)
, andreal_failed
when returnErr(E)
. - Different future with the same
task_id
is considered the same task. - The same task can only
real_success
once, e.g. a purchase process would never succeed more then once by launching with unique process id astask_id
. - A succeeded task can only be revoked successfully once.
§About Task State
- If a task’s state is
Success
, it must bereal_success
, i.e. $\text{Success}(id) \rightarrow \text{real_success}(id)$. - If a task’s state is
Failed
, it may be in any status, but mostlyreal_failed
. - If a task’s state is
Working
, it may be in any status, but mostlyreal_working
. - If a task’s state is
NotFound
, it may be in any status, but mostlyreal_not_found
.
§About Task State Transition
- Any task’s state can be queried at any time.
- The initial state of the task is
NotFound
. - Task’s state won’t change immediately after
launch
called. But if you query afterlaunch().await
, you will get changed result. - Always, when a task whose state is
Failed
orNotFound
is launched, it will beWorking
at some future moment. - Always, when a task is
Working
, it would eventually beFail
orSuccess
. - Always, when a task is
Success
, it would keepSuccess
until the revoking succeed, and then becomeNotFound
.
§Other
Further propositions and proofs at AsyncTasksRecorder.
Relationship between states and containers at query_task_state.
Use query_task_state_quick for less contention.
Re-exports§
pub use scc;
Structs§
- Arc was used internally, so after
clone
, the sameTaskManager
was used, which means you can shareAsyncTasksRecorder
by clone. - Thread-safe.