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