lefengyang
2026-08-20 d01a28cb030b2cb0ff1d42273c877a0f728519a6
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
import {RemoteTask} from "./lib/RemoteTask";
import {commitDetailTask} from "./task-fn/executeDetail";
import {wait} from "gs-base";
import {isCache} from "./lib/cacheFetch";
import {executeSearchTask} from "./task-fn/executeSearchTask";
import {executeDetailTask} from "./task-fn/executeDetailTask";
import {executeBidTask} from "./task-fn/executeBidTask";
import {Db, ITask} from "/src-com";
 
 
export async function executeTask(reserve: boolean = false): Promise<any> {
    const task = await RemoteTask.get(reserve);
    const {type}: ITask = task || {};
    if (!type) {
        return {
            task,
            error: 'empty task'
        }
    }
    let cache: any, cached = false;
    try {
        if (type === 'detail') {
            (await isCache()) && (cache = await Db.detailCache.get(task.id));
            cached = !!cache;
            if (cache) {
                if (cache.detail) {
                    cache.result = await commitDetailTask(task.id, [cache.detail]);
                } else if (cache.error) {
                    cache.result = await commitDetailTask(task.id, [{error: cache.error}]);
                }
                return cache;
            }
            cache = await executeDetailTask(task as any);
            await Db.detailCache.set(task.id, cache);
            return cache;
        } else if (type === 'search') {
            return await executeSearchTask(task as any);
        } else if (type === 'bid' || type === 'buyItNow') {
            return await executeBidTask(task as any);
        }
    } catch (e) {
        console.log(e)
        throw e;
    } finally {
        await RemoteTask.end(task);
        if (!cached && type === 'detail' && !/^invalid id:/.test(cache?.error)) {
            console.log('--delay--');
            await wait(3, 6);
        }
    }
}