atex 0.3.0

Lib for async local task evaluation(sqlite or in-memory)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use std::collections::HashMap;
use std::mem;
use std::sync::Arc;
use std::time::Duration;

use tokio::sync::mpsc;
use tokio::sync::mpsc::error::TryRecvError;
use tokio::sync::mpsc::{Receiver, Sender};
use tokio::sync::Mutex;
use tokio::task::JoinHandle;

use crate::entities::error::AtexError;
use crate::entities::task::{
    NewPeriodic, NewRepoTask, NewTask, Task, TaskId, TaskStarted, TaskStats, TaskStatus,
};
use crate::proto::{
    DynFut, ILogger, IPool, IPoolFactory, ITaskManager, ITaskRepo, IWrappedTaskExecutor,
};
use crate::utils::now;
use crate::ITaskController;

pub(crate) struct TaskManager<
    P: IPoolFactory<TaskId, JoinHandle<TaskStatus>>,
    R: ITaskRepo,
    L: ILogger,
> {
    data: Arc<TaskManagerData<P, R, L>>,
}

impl<P: IPoolFactory<TaskId, JoinHandle<TaskStatus>>, R: ITaskRepo, L: ILogger> Clone
    for TaskManager<P, R, L>
{
    fn clone(&self) -> Self {
        Self {
            data: self.data.clone(),
        }
    }
}

struct TaskManagerData<P: IPoolFactory<TaskId, JoinHandle<TaskStatus>>, R: ITaskRepo, L: ILogger> {
    executors: HashMap<&'static str, Box<dyn IWrappedTaskExecutor + 'static>>,
    pool_factory: P,
    repo: Arc<Mutex<R>>,
    logger: Arc<L>,
    sleep: Duration,
    last_report: Arc<Mutex<Vec<Option<TaskStarted>>>>,
}

struct MutCtx<P: IPool<TaskId, JoinHandle<TaskStatus>>> {
    sender: Sender<usize>,
    receiver: Receiver<usize>,
    pool: P,
}

unsafe impl<P: IPoolFactory<TaskId, JoinHandle<TaskStatus>>, R: ITaskRepo, L: ILogger> Send
    for TaskManagerData<P, R, L>
{
}

unsafe impl<P: IPoolFactory<TaskId, JoinHandle<TaskStatus>>, R: ITaskRepo, L: ILogger> Sync
    for TaskManagerData<P, R, L>
{
}

impl<P: IPoolFactory<TaskId, JoinHandle<TaskStatus>>, R: ITaskRepo, L: ILogger>
    TaskManager<P, R, L>
{
    pub(crate) fn new(
        executors: HashMap<&'static str, Box<dyn IWrappedTaskExecutor + 'static>>,
        pool_factory: P,
        repo: Arc<Mutex<R>>,
        logger: Arc<L>,
        sleep: Duration,
    ) -> Self {
        let data = TaskManagerData {
            executors,
            pool_factory,
            repo,
            logger,
            sleep,
            last_report: Arc::new(Mutex::new(Vec::new())),
        };
        Self {
            data: Arc::new(data),
        }
    }
}

impl<
        P: IPoolFactory<TaskId, JoinHandle<TaskStatus>> + 'static,
        R: ITaskRepo + 'static,
        L: ILogger + 'static,
    > TaskManager<P, R, L>
{
    async fn a_create_task(self, task: NewTask) -> Result<TaskId, AtexError> {
        let lock = self.gen_lock(&task)?;
        let db_task = NewRepoTask {
            periodic_interval: None,
            executor: task.executor,
            payload: task.payload,
            created: now(),
            updated: now(),
            execute_after: 0,
            lock,
        };
        let mut db = self.data.repo.lock().await;
        db.create(db_task)
    }

    fn gen_lock(&self, task: &NewTask) -> Result<String, AtexError> {
        let ex_name: &str = &task.executor;
        let executor = match self.data.executors.get(&ex_name) {
            None => return Err(AtexError::InvalidExecutor),
            Some(val) => val,
        };
        Ok(executor.lock_key(&task.payload))
    }

    async fn create_periodic(&self, task: NewPeriodic) -> Result<TaskId, AtexError> {
        let lock = task.executor.to_string();
        let db_task = NewRepoTask {
            periodic_interval: Some(task.interval),
            executor: task.executor,
            payload: "".to_string(),
            created: now(),
            updated: now(),
            execute_after: now(),
            lock,
        };
        let mut db = self.data.repo.lock().await;
        if let Some(task) = db.get_by_executor(&db_task.executor)? {
            Ok(task.id)
        } else {
            Ok(db.create(db_task)?)
        }
    }

    async fn a_get(self, id: TaskId) -> Result<Option<Task>, AtexError> {
        let db = self.data.repo.lock().await;
        db.get_by_id(id)
    }

    fn prepare(&self) -> MutCtx<P::Pool> {
        let len = self.data.pool_factory.len();
        let (sender, receiver) = mpsc::channel::<usize>(len);
        let pool = self.data.pool_factory.produce();
        MutCtx {
            sender,
            receiver,
            pool,
        }
    }

    async fn init_periodics(&self) -> Result<(), AtexError> {
        let mut periodics = Vec::new();
        for executor in self.data.executors.values() {
            if let Some(ts) = executor.periodic_interval() {
                periodics.push(NewPeriodic {
                    executor: executor.name(),
                    interval: ts,
                });
            }
        }
        for periodic in periodics {
            self.create_periodic(periodic).await?;
        }
        Ok(())
    }

    async fn a_run(self) -> Result<(), AtexError> {
        self.init_periodics().await?;
        let mut ctx = self.prepare();
        {
            let mut mtx = self.data.last_report.lock().await;
            *mtx = (0..ctx.pool.len()).map(|_| None).collect();
        }
        loop {
            let started = self.loop_iteration(&mut ctx).await?;
            if started == 0 {
                tokio::time::sleep(self.data.sleep).await
            }
        }
    }

    async fn loop_iteration(&self, ctx: &mut MutCtx<P::Pool>) -> Result<usize, AtexError> {
        let mut placed = Vec::new();
        let mut removed = None;
        let result = match ctx.receiver.try_recv() {
            Ok(id) => {
                removed = Some(id);
                self.clear_task_in_cell(ctx, id).await?;
                self.fill_empty_cells(ctx, &mut placed).await?
            }
            Err(TryRecvError::Empty) => self.fill_empty_cells(ctx, &mut placed).await?,
            Err(TryRecvError::Disconnected) => return Err(AtexError::TaskQueueIsBroken),
        };
        if removed.is_none() && placed.is_empty() {
            return Ok(result);
        }
        let mut report = self.data.last_report.lock().await;
        if let Some(i) = removed {
            report[i] = None;
        }
        let started = now();
        for (i, executor) in placed {
            report[i] = Some(TaskStarted { executor, started });
        }
        Ok(result)
    }

    async fn clear_task_in_cell(
        &self,
        ctx: &mut MutCtx<P::Pool>,
        id: usize,
    ) -> Result<(), AtexError> {
        let (task_id, handler) = ctx.pool.get_cell(id)?;
        let status = match handler.await {
            Ok(val) => val,
            Err(err) => {
                self.data.logger.log(&format!("{:?}", err));
                return Err(AtexError::TaskJoinMechanismFailed);
            }
        };
        let mut db = self.data.repo.lock().await;
        let task = match db.get_by_id(task_id)? {
            None => {
                self.data.logger.log("Task has beed removed");
                return Ok(());
            }
            Some(val) => val,
        };
        let log_msg = if let TaskStatus::Error(ref err) = status {
            Some(format!(
                "task (id={}, executor={:?}) failed: {:?}",
                task.id, task.executor, err,
            ))
        } else {
            None
        };
        if let Some(interval) = task.periodic_interval {
            let ts = now() + interval;
            db.set_execute_after(task_id, ts)?;
        } else {
            db.set_status(task_id, status)?
        }
        mem::drop(db);
        if let Some(msg) = log_msg {
            self.data.logger.log(&msg)
        }
        Ok(())
    }

    async fn fill_empty_cells(
        &self,
        ctx: &mut MutCtx<P::Pool>,
        placed: &mut Vec<(usize, &'static str)>,
    ) -> Result<usize, AtexError> {
        let exclude = ctx.pool.get_keys();
        let free_cells = ctx.pool.free_cells();
        if free_cells.is_empty() {
            return Ok(0);
        }
        let db = self.data.repo.lock().await;
        let tasks = db.get_ready(&exclude, free_cells.len())?;
        mem::drop(db);
        let started = tasks.len();
        for (i, task) in tasks.into_iter().enumerate() {
            let cell = free_cells[i];
            let id = task.id;
            let executor = task.executor.clone();
            let handler = match self.start_task(task, ctx.sender.clone(), cell) {
                Ok(val) => val,
                Err(AtexError::ExecutorNotFound) => {
                    let status = TaskStatus::Error("Can not find executor".to_string());
                    self.data.repo.lock().await.set_status(id, status)?;
                    continue;
                }
                Err(err) => return Err(err),
            };
            ctx.pool.put_cell(cell, id, handler)?;
            placed.push((cell, executor));
        }
        Ok(started)
    }

    fn start_task(
        &self,
        task: Task,
        signal: Sender<usize>,
        cell_id: usize,
    ) -> Result<JoinHandle<TaskStatus>, AtexError> {
        let ex_name = task.executor.clone();
        let executor = match self.data.executors.get(&(&ex_name as &str)) {
            Some(val) => val,
            None => {
                self.data
                    .logger
                    .log(&format!("Can not find executor: {}", task.executor));
                return Err(AtexError::ExecutorNotFound);
            }
        };
        let ctrl = Box::new(Self {
            data: self.data.clone(),
        });
        let task_future = executor.execute(ctrl, task);
        let future = async move {
            let status = task_future.await;
            if let Err(err) = signal.send(cell_id).await {
                panic!("executor failed with channel error: {:?}", err)
            }
            status
        };
        Ok(tokio::task::spawn(future))
    }

    async fn a_set_progress(self, id: TaskId, progress: u8) -> Result<(), AtexError> {
        let mut repo = self.data.repo.lock().await;
        repo.set_progress(id, progress)?;
        Ok(())
    }

    async fn a_execute_periodic_now(self, executor: &'static str) -> Result<(), AtexError> {
        let mut repo = self.data.repo.lock().await;
        let task = match repo.get_by_executor(executor)? {
            None => return Err(AtexError::InvalidName),
            Some(val) => val,
        };
        repo.set_execute_after(task.id, now() - 1)?;
        Ok(())
    }

    async fn a_manual_execute_periodic(
        self,
        executor: &'static str,
    ) -> Result<TaskStatus, AtexError> {
        self.init_periodics().await?;
        let opt_task = self.data.repo.lock().await.get_by_executor(executor)?;
        match opt_task {
            None => return Err(AtexError::InvalidExecutor),
            Some(task) => self.manual_exec_task(task).await,
        }
    }

    async fn a_manual_execute_task(self, task_id: TaskId) -> Result<TaskStatus, AtexError> {
        let opt_task = self.data.repo.lock().await.get_by_id(task_id)?;
        match opt_task {
            None => return Err(AtexError::InvalidId),
            Some(task) => self.manual_exec_task(task).await,
        }
    }

    async fn manual_exec_task(&self, task: Task) -> Result<TaskStatus, AtexError> {
        let ex_name = task.executor.clone();
        let executor = match self.data.executors.get(&(&ex_name as &str)) {
            Some(val) => val,
            None => {
                self.data
                    .logger
                    .log(&format!("Can not find executor: {}", task.executor));
                return Err(AtexError::ExecutorNotFound);
            }
        };
        let ctrl = Box::new(Self {
            data: self.data.clone(),
        });
        Ok(executor.execute(ctrl, task).await)
    }

    fn name_to_task_key(&self, name: &str) -> Option<&'static str> {
        let exec = self.data.executors.get(name)?;
        Some(exec.name())
    }

    async fn a_get_stats(self) -> Vec<Option<TaskStats>> {
        let now_time = now();
        let started = self.data.last_report.lock().await;
        let mut result = Vec::with_capacity(started.len());
        for opt_task in started.iter() {
            if let Some(task) = opt_task {
                let stats = TaskStats {
                    executor: task.executor,
                    active_for: now_time - task.started,
                };
                result.push(Some(stats))
            } else {
                result.push(None)
            }
        }
        result
    }
}

impl<
        P: IPoolFactory<TaskId, JoinHandle<TaskStatus>> + 'static,
        R: ITaskRepo + 'static,
        L: ILogger + 'static,
    > ITaskManager for TaskManager<P, R, L>
{
    fn create_task(&self, task: NewTask) -> DynFut<Result<TaskId, AtexError>> {
        Box::pin(self.clone().a_create_task(task))
    }

    fn schedule_periodic_now(&self, name: &str) -> DynFut<Result<(), AtexError>> {
        let key = match self.name_to_task_key(name) {
            Some(val) => val,
            None => return Box::pin(async move { Err(AtexError::InvalidName) }),
        };
        Box::pin(self.clone().a_execute_periodic_now(key))
    }

    fn manual_execute_periodic(&self, name: &str) -> DynFut<Result<TaskStatus, AtexError>> {
        let key = match self.name_to_task_key(name) {
            Some(val) => val,
            None => return Box::pin(async move { Err(AtexError::InvalidName) }),
        };
        Box::pin(self.clone().a_manual_execute_periodic(key))
    }

    fn manual_execute_task(&self, task_id: TaskId) -> DynFut<Result<TaskStatus, AtexError>> {
        Box::pin(self.clone().a_manual_execute_task(task_id))
    }

    fn get(&self, id: TaskId) -> DynFut<Result<Option<Task>, AtexError>> {
        Box::pin(self.clone().a_get(id))
    }

    fn run(&self) -> DynFut<Result<(), AtexError>> {
        Box::pin(self.clone().a_run())
    }

    fn get_stats(&self) -> DynFut<Vec<Option<TaskStats>>> {
        Box::pin(self.clone().a_get_stats())
    }
}

impl<
        P: IPoolFactory<TaskId, JoinHandle<TaskStatus>> + 'static,
        R: ITaskRepo + 'static,
        L: ILogger + 'static,
    > ITaskController for TaskManager<P, R, L>
{
    fn set_progress(&self, id: TaskId, progress: u8) -> DynFut<Result<(), AtexError>> {
        Box::pin(self.clone().a_set_progress(id, progress))
    }

    fn create_task(&self, task: NewTask) -> DynFut<Result<TaskId, AtexError>> {
        Box::pin(self.clone().a_create_task(task))
    }
}