Skip to content

Commit

Permalink
Server: Improved task service log entries
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 committed Oct 24, 2021
1 parent 643bddf commit bc5a853
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/server/src/services/TaskService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,20 @@ export default class TaskService extends BaseService {
};
}

private taskById(id: TaskId): Task {
if (!this.tasks_[id]) throw new Error(`No such task: ${id}`);
return this.tasks_[id];
}

private taskDisplayString(id: TaskId): string {
const task = this.taskById(id);
return `#${task.id} (${task.description})`;
}

public async runTask(id: TaskId, runType: RunType) {
const displayString = this.taskDisplayString(id);
const state = this.taskState(id);
if (state.running) throw new Error(`Task is already running: ${id}`);
if (state.running) throw new Error(`Already running: ${displayString}`);

const startTime = Date.now();

Expand All @@ -92,10 +103,10 @@ export default class TaskService extends BaseService {
await this.models.event().create(EventType.TaskStarted, id.toString());

try {
logger.info(`Running "${id}" (${runTypeToString(runType)})...`);
logger.info(`Running ${displayString} (${runTypeToString(runType)})...`);
await this.tasks_[id].run(this.models);
} catch (error) {
logger.error(`On task "${id}"`, error);
logger.error(`On ${displayString}`, error);
}

this.taskStates_[id] = {
Expand All @@ -105,14 +116,14 @@ export default class TaskService extends BaseService {

await this.models.event().create(EventType.TaskCompleted, id.toString());

logger.info(`Completed "${id}" in ${Date.now() - startTime}ms`);
logger.info(`Completed ${this.taskDisplayString(id)} in ${Date.now() - startTime}ms`);
}

public async runInBackground() {
for (const [taskId, task] of Object.entries(this.tasks_)) {
if (!task.schedule) continue;

logger.info(`Scheduling task #${taskId} (${task.description}): ${task.schedule}`);
logger.info(`Scheduling ${this.taskDisplayString(task.id)}: ${task.schedule}`);

cron.schedule(task.schedule, async () => {
await this.runTask(Number(taskId), RunType.Scheduled);
Expand Down

0 comments on commit bc5a853

Please sign in to comment.