3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.taskprocessor;
22 import com.google.common.base.Optional;
23 import org.sonar.api.utils.log.Logger;
24 import org.sonar.api.utils.log.Loggers;
25 import org.sonar.core.util.logs.Profiler;
26 import org.sonar.db.ce.CeActivityDto;
27 import org.sonar.server.computation.log.CeLogging;
28 import org.sonar.server.computation.queue.CeQueue;
29 import org.sonar.server.computation.queue.CeTask;
30 import org.sonar.server.computation.queue.CeTaskResult;
32 import static java.lang.String.format;
34 public class CeWorkerCallableImpl implements CeWorkerCallable {
36 private static final Logger LOG = Loggers.get(CeWorkerCallableImpl.class);
38 private final CeQueue queue;
39 private final CeLogging ceLogging;
40 private final CeTaskProcessorRepository taskProcessorRepository;
42 public CeWorkerCallableImpl(CeQueue queue, CeLogging ceLogging, CeTaskProcessorRepository taskProcessorRepository) {
44 this.ceLogging = ceLogging;
45 this.taskProcessorRepository = taskProcessorRepository;
49 public Boolean call() throws Exception {
50 Optional<CeTask> ceTask = tryAndFindTaskToExecute();
51 if (!ceTask.isPresent()) {
55 executeTask(ceTask.get());
59 private Optional<CeTask> tryAndFindTaskToExecute() {
62 } catch (Exception e) {
63 LOG.error("Failed to pop the queue of analysis reports", e);
65 return Optional.absent();
68 private void executeTask(CeTask task) {
69 // logging twice: once in sonar.log and once in CE appender
70 Profiler regularProfiler = startProfiler(task);
71 ceLogging.initForTask(task);
72 Profiler ceProfiler = startProfiler(task);
74 CeActivityDto.Status status = CeActivityDto.Status.FAILED;
75 CeTaskResult process = null;
77 // TODO delegate the message to the related task processor, according to task type
78 Optional<CeTaskProcessor> taskProcessor = taskProcessorRepository.getForCeTask(task);
79 if (taskProcessor.isPresent()) {
80 process = taskProcessor.get().process(task);
81 status = CeActivityDto.Status.SUCCESS;
83 LOG.error("No CeTaskProcessor is defined for task of type {}. Plugin configuration may have changed", task.getType());
84 status = CeActivityDto.Status.FAILED;
86 } catch (Throwable e) {
87 LOG.error(format("Failed to execute task %s", task.getUuid()), e);
89 queue.remove(task, status, process);
90 // logging twice: once in sonar.log and once in CE appender
91 stopProfiler(ceProfiler, task, status);
92 ceLogging.clearForTask();
93 stopProfiler(regularProfiler, task, status);
97 private static Profiler startProfiler(CeTask task) {
98 return Profiler.create(LOG).startInfo("Execute task | project={} | id={}", task.getComponentKey(), task.getUuid());
101 private static void stopProfiler(Profiler profiler, CeTask task, CeActivityDto.Status status) {
102 if (status == CeActivityDto.Status.FAILED) {
103 profiler.stopError("Executed task | project={} | id={}", task.getComponentKey(), task.getUuid());
105 profiler.stopInfo("Executed task | project={} | id={}", task.getComponentKey(), task.getUuid());