2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.
21 package org.sonar.server.computation.taskprocessor;
23 import com.google.common.base.Optional;
24 import org.sonar.api.utils.log.Logger;
25 import org.sonar.api.utils.log.Loggers;
26 import org.sonar.core.util.logs.Profiler;
27 import org.sonar.db.ce.CeActivityDto;
28 import org.sonar.server.computation.log.CeLogging;
29 import org.sonar.server.computation.queue.CeQueue;
30 import org.sonar.server.computation.queue.CeTask;
32 import static java.lang.String.format;
34 public class CeWorkerRunnableImpl implements CeWorkerRunnable {
36 private static final Logger LOG = Loggers.get(CeWorkerRunnableImpl.class);
38 private final CeQueue queue;
39 private final CeLogging ceLogging;
40 private final CeTaskProcessorRepository taskProcessorRepository;
42 public CeWorkerRunnableImpl(CeQueue queue, CeLogging ceLogging, CeTaskProcessorRepository taskProcessorRepository) {
44 this.ceLogging = ceLogging;
45 this.taskProcessorRepository = taskProcessorRepository;
50 Optional<CeTask> ceTask = tryAndFindTaskToExecute();
51 if (!ceTask.isPresent()) {
55 executeTask(ceTask.get());
58 private Optional<CeTask> tryAndFindTaskToExecute() {
61 } catch (Exception e) {
62 LOG.error("Failed to pop the queue of analysis reports", e);
64 return Optional.absent();
67 private void executeTask(CeTask task) {
68 // logging twice: once in sonar.log and once in CE appender
69 Profiler regularProfiler = startProfiler(task);
70 ceLogging.initForTask(task);
71 Profiler ceProfiler = startProfiler(task);
73 CeActivityDto.Status status = CeActivityDto.Status.FAILED;
75 // TODO delegate the message to the related task processor, according to task type
76 Optional<CeTaskProcessor> taskProcessor = taskProcessorRepository.getForCeTask(task);
77 if (taskProcessor.isPresent()) {
78 taskProcessor.get().process(task);
79 status = CeActivityDto.Status.SUCCESS;
81 LOG.error("No CeTaskProcessor is defined for task of type {}. Plugin configuration may have changed", task.getType());
82 status = CeActivityDto.Status.FAILED;
84 queue.remove(task, status);
85 } catch (Throwable e) {
86 LOG.error(format("Failed to execute task %s", task.getUuid()), e);
87 queue.remove(task, status);
89 // logging twice: once in sonar.log and once in CE appender
90 stopProfiler(ceProfiler, task, status);
91 ceLogging.clearForTask();
92 stopProfiler(regularProfiler, task, status);
96 private static Profiler startProfiler(CeTask task) {
97 return Profiler.create(LOG).startInfo("Execute task | project={} | id={}", task.getComponentKey(), task.getUuid());
100 private static void stopProfiler(Profiler profiler, CeTask task, CeActivityDto.Status status) {
101 if (status == CeActivityDto.Status.FAILED) {
102 profiler.stopError("Executed task | project={} | id={}", task.getComponentKey(), task.getUuid());
104 profiler.stopInfo("Executed task | project={} | id={}", task.getComponentKey(), task.getUuid());