task.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #![allow(clippy::all)]
  2. #![allow(dead_code)]
  3. use crate::services::row::GridBlockSnapshot;
  4. use crate::services::tasks::queue::TaskHandlerId;
  5. use std::cmp::Ordering;
  6. #[derive(Eq, Debug, Clone, Copy)]
  7. pub enum TaskType {
  8. /// Remove the row if it doesn't satisfy the filter.
  9. Filter,
  10. /// Generate snapshot for grid, unused by now.
  11. Snapshot,
  12. Group,
  13. }
  14. impl PartialEq for TaskType {
  15. fn eq(&self, other: &Self) -> bool {
  16. matches!(
  17. (self, other),
  18. (Self::Filter, Self::Filter) | (Self::Snapshot, Self::Snapshot)
  19. )
  20. }
  21. }
  22. pub type TaskId = u32;
  23. #[derive(Eq, Debug, Clone, Copy)]
  24. pub struct PendingTask {
  25. pub ty: TaskType,
  26. pub id: TaskId,
  27. }
  28. impl PartialEq for PendingTask {
  29. fn eq(&self, other: &Self) -> bool {
  30. self.id.eq(&other.id)
  31. }
  32. }
  33. impl PartialOrd for PendingTask {
  34. fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
  35. Some(self.cmp(other))
  36. }
  37. }
  38. impl Ord for PendingTask {
  39. fn cmp(&self, other: &Self) -> Ordering {
  40. match (self.ty, other.ty) {
  41. // Snapshot
  42. (TaskType::Snapshot, TaskType::Snapshot) => Ordering::Equal,
  43. (TaskType::Snapshot, _) => Ordering::Greater,
  44. (_, TaskType::Snapshot) => Ordering::Less,
  45. // Group
  46. (TaskType::Group, TaskType::Group) => self.id.cmp(&other.id).reverse(),
  47. (TaskType::Group, _) => Ordering::Greater,
  48. (_, TaskType::Group) => Ordering::Greater,
  49. // Filter
  50. (TaskType::Filter, TaskType::Filter) => self.id.cmp(&other.id).reverse(),
  51. }
  52. }
  53. }
  54. pub(crate) struct FilterTaskContext {
  55. pub blocks: Vec<GridBlockSnapshot>,
  56. }
  57. pub(crate) enum TaskContent {
  58. #[allow(dead_code)]
  59. Snapshot,
  60. Group,
  61. Filter(FilterTaskContext),
  62. }
  63. #[derive(Debug, Eq, PartialEq)]
  64. pub(crate) enum TaskStatus {
  65. Pending,
  66. Processing,
  67. Done,
  68. Failure,
  69. Cancel,
  70. }
  71. pub(crate) struct Task {
  72. pub id: TaskId,
  73. pub handler_id: TaskHandlerId,
  74. pub content: Option<TaskContent>,
  75. status: TaskStatus,
  76. pub ret: Option<tokio::sync::oneshot::Sender<TaskResult>>,
  77. pub rx: Option<tokio::sync::oneshot::Receiver<TaskResult>>,
  78. }
  79. pub(crate) struct TaskResult {
  80. pub id: TaskId,
  81. pub(crate) status: TaskStatus,
  82. }
  83. impl std::convert::From<Task> for TaskResult {
  84. fn from(task: Task) -> Self {
  85. TaskResult {
  86. id: task.id,
  87. status: task.status,
  88. }
  89. }
  90. }
  91. impl Task {
  92. pub fn new(handler_id: &str, id: TaskId, content: TaskContent) -> Self {
  93. let (ret, rx) = tokio::sync::oneshot::channel();
  94. Self {
  95. handler_id: handler_id.to_owned(),
  96. id,
  97. content: Some(content),
  98. ret: Some(ret),
  99. rx: Some(rx),
  100. status: TaskStatus::Pending,
  101. }
  102. }
  103. pub fn set_status(&mut self, status: TaskStatus) {
  104. self.status = status;
  105. }
  106. pub fn is_finished(&self) -> bool {
  107. match self.status {
  108. TaskStatus::Done => true,
  109. _ => false,
  110. }
  111. }
  112. }