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;
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;
76 // TODO delegate the message to the related task processor, according to task type
77 Optional<CeTaskProcessor> taskProcessor = taskProcessorRepository.getForCeTask(task);
78 if (taskProcessor.isPresent()) {
79 taskProcessor.get().process(task);
80 status = CeActivityDto.Status.SUCCESS;
82 LOG.error("No CeTaskProcessor is defined for task of type {}. Plugin configuration may have changed", task.getType());
83 status = CeActivityDto.Status.FAILED;
85 queue.remove(task, status);
86 } catch (Throwable e) {
87 LOG.error(format("Failed to execute task %s", task.getUuid()), e);
88 queue.remove(task, status);
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());