• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alkem-io / server / #7335

12 Jun 2024 07:24AM UTC coverage: 13.404%. First build
#7335

Pull #4090

travis-ci

Pull Request #4090: Post ingestion fixed

112 of 4365 branches covered (2.57%)

Branch coverage included in aggregate %.

0 of 4 new or added lines in 2 files covered. (0.0%)

1799 of 9892 relevant lines covered (18.19%)

2.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

11.22
/src/services/task/task.service.ts
1
import { Inject, Injectable, LoggerService } from '@nestjs/common';
5✔
2
import { CACHE_MANAGER } from '@nestjs/cache-manager';
5✔
3
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
5✔
4
import { randomUUID } from 'crypto';
5✔
5
import { Cache } from 'cache-manager';
6
import { TaskError, TaskResult } from '@services/task/types';
7
import { LogContext } from '@common/enums';
5✔
8
import { TaskStatus } from '@domain/task/dto/task.status.enum';
5✔
9
import { Task } from './task.interface';
10
import { TASK_LIST_CACHE_KEY } from './task.list.key';
5✔
11

12
const TTL = 3600;
5✔
13

14
@Injectable()
15
export class TaskService {
5✔
16
  constructor(
17
    @Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
1✔
18
    @Inject(WINSTON_MODULE_NEST_PROVIDER)
19
    private readonly logger: LoggerService
1✔
20
  ) {}
21

22
  public async getTaskList() {
23
    const list =
×
24
      await this.cacheManager.get<Array<string>>(TASK_LIST_CACHE_KEY);
25

26
    if (list) {
27
      return list;
×
28
    }
×
29

30
    this.logger.warn?.(
31
      'Task list not found. Creating a new one...',
×
32
      LogContext.TASKS
33
    );
34
    await this.cacheManager.set<Array<string>>(TASK_LIST_CACHE_KEY, [], {
35
      ttl: TTL,
×
36
    });
37
    return []; // set returns 'ok'
38
  }
×
39

40
  public async getAll(status = TaskStatus.IN_PROGRESS) {
41
    const list = await this.getTaskList();
×
42

×
43
    const resolved = await Promise.all(list.map(taskId => this.get(taskId)));
44

×
45
    let result = resolved.filter((x): x is Task => !!x);
46

×
47
    if (status) {
48
      result = result.filter(x => x.status === status);
×
49
    }
×
50

51
    return result;
52
  }
×
53

54
  public get(id: string) {
55
    return this.cacheManager.get<Task>(id);
56
  }
×
57

58
  public async getOrFail(id: string) {
59
    const task = await this.cacheManager.get<Task>(id);
60

×
61
    if (!task) {
62
      throw new Error(`Task '${id}' not found`);
×
63
    }
×
64

65
    return task;
66
  }
×
67

68
  public async create(itemsCount?: number) {
69
    const now = new Date().getTime();
70
    const task: Task = {
×
71
      id: randomUUID(),
×
72
      created: now,
73
      start: now, // has to change
74
      action: 'auth-reset',
75
      status: TaskStatus.IN_PROGRESS, // may not be accurate atm,
76
      itemsCount,
77
      itemsDone: itemsCount && 0,
78
      results: [],
×
79
      errors: [],
80
    };
81
    await this.cacheManager.set<Task>(task.id, task, {
82
      ttl: TTL,
×
83
    });
84
    await this.addTaskToList(task);
85
    return task;
×
86
  }
×
87

88
  /**
89
   *
90
   * @param id
91
   * @param result
92
   * @param completeItem Increase the itemsDone counter
93
   */
94
  public async updateTaskResults(
95
    id: string,
96
    result: TaskResult,
97
    completeItem = true
98
  ) {
×
99
    const task = await this.getOrFail(id);
100

×
101
    if (task.itemsDone !== undefined && completeItem) {
102
      task.itemsDone += 1;
×
103
    }
×
104

105
    if (task.itemsCount && task.itemsCount === task.itemsDone) {
106
      task.status = TaskStatus.COMPLETED;
×
107
    }
×
108

109
    task.results.unshift(`[${new Date().toISOString()}]::${result}`);
110

×
111
    await this.cacheManager.set(task.id, task, {
112
      ttl: TTL,
×
113
    });
114
  }
115

116
  public async updateTaskErrors(
117
    id: string,
118
    error: TaskError,
119
    completeItem = true
120
  ) {
×
121
    const task = await this.getOrFail(id);
122

×
123
    if (task.itemsDone !== undefined && completeItem) {
124
      task.itemsDone += 1;
×
125
    }
×
126

127
    if (task.itemsCount === task.itemsDone) {
128
      task.status = TaskStatus.COMPLETED;
×
129
    }
×
130

131
    task.errors.unshift(error);
132

×
133
    await this.cacheManager.set(task.id, task, {
134
      ttl: TTL,
×
135
    });
136
  }
137

138
  public async complete(
139
    id: string,
140
    status: TaskStatus.COMPLETED | TaskStatus.ERRORED = TaskStatus.COMPLETED
141
  ) {
×
142
    const task = await this.getOrFail(id);
143

×
144
    task.status = status;
145
    task.end = new Date().getTime();
×
NEW
146

×
147
    await this.cacheManager.set(task.id, task, {
148
      ttl: TTL,
×
149
    });
150
  }
151

152
  public async completeWithError(id: string, error: string) {
153
    const task = await this.getOrFail(id);
154

×
155
    task.errors.unshift(error);
156
    task.status = TaskStatus.ERRORED;
×
157
    task.end = new Date().getTime();
×
158

159
    await this.cacheManager.set(task.id, task, {
160
      ttl: TTL,
161
    });
162
  }
×
163

164
  private async addTaskToList(task: Task) {
165
    const list = await this.getTaskList();
×
166

167
    if (!list) {
×
168
      this.logger.error(
169
        'Could not add task to list. List not found.',
170
        undefined,
×
171
        LogContext.TASKS
172
      );
×
173
      return false;
174
    }
×
175

176
    list.push(task.id);
177

×
178
    return this.cacheManager
179
      .set(TASK_LIST_CACHE_KEY, list, { ttl: TTL })
180
      .then(
181
        () => true,
×
182
        reason => {
183
          this.logger.error(
×
184
            `Could not add task to list. ${reason}`,
185
            reason?.stack,
186
            LogContext.TASKS
×
187
          );
188
          return false;
189
        }
190
      )
191
      .catch(error => {
192
        this.logger.error(
193
          `Could not add task to list. ${error}`,
194
          error?.stack,
195
          LogContext.TASKS
196
        );
197
        return false;
198
      });
199
  }
200
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc