File size: 2,864 Bytes
ba27085
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * 图片处理队列
 * 确保图片严格按照顺序处理,避免异步操作导致的顺序混乱
 */
class ImageProcessingQueue {
  constructor() {
    this.queue = []
    this.processing = false
    this.results = new Map() // 存储处理结果,key为索引,value为结果
  }

  /**
   * 添加图片处理任务到队列
   * @param {number} index - 图片索引
   * @param {Function} processor - 处理函数
   * @returns {Promise} 处理结果的Promise
   */
  async addTask(index, processor) {
    return new Promise((resolve, reject) => {
      const task = {
        index,
        processor,
        resolve,
        reject,
        addTime: Date.now()
      }
      
      this.queue.push(task)
      console.log(`[图片队列] 任务${index}已添加到队列,队列长度: ${this.queue.length}`)
      
      // 如果队列没有在处理,开始处理
      if (!this.processing) {
        this.processQueue()
      }
    })
  }

  /**
   * 处理队列中的任务
   */
  async processQueue() {
    if (this.processing || this.queue.length === 0) {
      return
    }

    this.processing = true
    console.log(`[图片队列] 开始处理队列,共${this.queue.length}个任务`)

    while (this.queue.length > 0) {
      const task = this.queue.shift()
      const startTime = Date.now()
      
      console.log(`[图片队列] 开始处理任务${task.index},等待时间: ${startTime - task.addTime}ms`)

      try {
        const result = await task.processor()
        const endTime = Date.now()
        const processingTime = endTime - startTime
        
        console.log(`[图片队列] 任务${task.index}处理完成,耗时: ${processingTime}ms`)
        
        // 存储结果
        this.results.set(task.index, {
          result,
          processingTime,
          completedAt: endTime
        })
        
        task.resolve(result)
      } catch (error) {
        console.error(`[图片队列] 任务${task.index}处理失败: ${error.message}`)
        task.reject(error)
      }
    }

    this.processing = false
    console.log(`[图片队列] 队列处理完成`)
  }

  /**
   * 获取所有结果,按索引顺序排列
   * @returns {Array} 按顺序排列的结果数组
   */
  getOrderedResults() {
    const sortedKeys = Array.from(this.results.keys()).sort((a, b) => a - b)
    return sortedKeys.map(key => ({
      index: key,
      ...this.results.get(key)
    }))
  }

  /**
   * 清空队列和结果
   */
  clear() {
    this.queue = []
    this.results.clear()
    this.processing = false
    console.log(`[图片队列] 队列已清空`)
  }

  /**
   * 获取队列状态
   */
  getStatus() {
    return {
      queueLength: this.queue.length,
      processing: this.processing,
      resultsCount: this.results.size
    }
  }
}

module.exports = ImageProcessingQueue