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.ce.taskprocessor.CeTaskProcessor;
26 import org.sonar.core.util.logs.Profiler;
27 import org.sonar.db.ce.CeActivityDto;
28 import org.sonar.ce.log.CeLogging;
29 import org.sonar.ce.queue.CeQueue;
30 import org.sonar.ce.queue.CeTask;
31 import org.sonar.ce.queue.CeTaskResult;
32 import org.sonar.server.computation.queue.InternalCeQueue;
34 import static java.lang.String.format;
36 public class CeWorkerCallableImpl implements CeWorkerCallable {
38 private static final Logger LOG = Loggers.get(CeWorkerCallableImpl.class);
40 private final InternalCeQueue queue;
41 private final CeLogging ceLogging;
42 private final CeTaskProcessorRepository taskProcessorRepository;
44 public CeWorkerCallableImpl(InternalCeQueue queue, CeLogging ceLogging, CeTaskProcessorRepository taskProcessorRepository) {
46 this.ceLogging = ceLogging;
47 this.taskProcessorRepository = taskProcessorRepository;
51 public Boolean call() throws Exception {
52 Optional<CeTask> ceTask = tryAndFindTaskToExecute();
53 if (!ceTask.isPresent()) {
57 executeTask(ceTask.get());
61 private Optional<CeTask> tryAndFindTaskToExecute() {
64 } catch (Exception e) {
65 LOG.error("Failed to pop the queue of analysis reports", e);
67 return Optional.absent();
70 private void executeTask(CeTask task) {
71 // logging twice: once in sonar.log and once in CE appender
72 Profiler regularProfiler = startProfiler(task);
73 ceLogging.initForTask(task);
74 Profiler ceProfiler = startProfiler(task);
76 CeActivityDto.Status status = CeActivityDto.Status.FAILED;
77 CeTaskResult process = null;
79 // TODO delegate the message to the related task processor, according to task type
80 Optional<CeTaskProcessor> taskProcessor = taskProcessorRepository.getForCeTask(task);
81 if (taskProcessor.isPresent()) {
82 process = taskProcessor.get().process(task);
83 status = CeActivityDto.Status.SUCCESS;
85 LOG.error("No CeTaskProcessor is defined for task of type {}. Plugin configuration may have changed", task.getType());
86 status = CeActivityDto.Status.FAILED;
88 } catch (Throwable e) {
89 LOG.error(format("Failed to execute task %s", task.getUuid()), e);
91 queue.remove(task, status, process);
92 // logging twice: once in sonar.log and once in CE appender
93 stopProfiler(ceProfiler, task, status);
94 ceLogging.clearForTask();
95 stopProfiler(regularProfiler, task, status);
99 private static Profiler startProfiler(CeTask task) {
100 return Profiler.create(LOG).startInfo("Execute task | project={} | id={}", task.getComponentKey(), task.getUuid());
103 private static void stopProfiler(Profiler profiler, CeTask task, CeActivityDto.Status status) {
104 if (status == CeActivityDto.Status.FAILED) {
105 profiler.stopError("Executed task | project={} | id={}", task.getComponentKey(), task.getUuid());
107 profiler.stopInfo("Executed task | project={} | id={}", task.getComponentKey(), task.getUuid());