View Javadoc

1   /**
2    *    Copyright 2012 meltmedia
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *        http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package com.meltmedia.cadmium.core.commands;
17  
18  import javax.inject.Inject;
19  import javax.inject.Singleton;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  import com.meltmedia.cadmium.core.CommandAction;
25  import com.meltmedia.cadmium.core.CommandContext;
26  import com.meltmedia.cadmium.core.ContentWorker;
27  import com.meltmedia.cadmium.core.CoordinatedWorker;
28  import com.meltmedia.cadmium.core.history.HistoryManager;
29  import com.meltmedia.cadmium.core.history.HistoryEntry.EntryType;
30  import com.meltmedia.cadmium.core.lifecycle.LifecycleService;
31  import com.meltmedia.cadmium.core.lifecycle.UpdateState;
32  import com.meltmedia.cadmium.core.messaging.ProtocolMessage;
33  
34  @Singleton
35  public class UpdateFailedCommandAction implements CommandAction {
36    private final Logger log = LoggerFactory.getLogger(getClass());
37    
38    public static final String FAILED_LOG_MESSAGE = "Update failed to run!";
39    
40    @Inject
41    @ContentWorker
42    protected CoordinatedWorker worker;
43    
44    @Inject
45    protected LifecycleService lifecycleService;
46    
47    @Inject
48    protected HistoryManager historyManager;
49  
50    public String getName() { return ProtocolMessage.UPDATE_FAILED; }
51  
52    @Override
53    public boolean execute(CommandContext ctx) throws Exception {
54      if(lifecycleService.getCurrentState() != UpdateState.IDLE) {
55        log.info("update has failed @ {}", ctx.getSource());
56        worker.killUpdate();
57        lifecycleService.updateMyState(UpdateState.IDLE);
58        if(historyManager != null) {
59          String repo = "";
60          if(ctx.getMessage().getProtocolParameters().containsKey("repo")) {
61            repo = ctx.getMessage().getProtocolParameters().get("repo");
62          }
63          String branch = "";
64          if(ctx.getMessage().getProtocolParameters().containsKey("branch")) {
65            branch = ctx.getMessage().getProtocolParameters().get("branch");
66          }
67          String sha = "";
68          if(ctx.getMessage().getProtocolParameters().containsKey("sha")) {
69            sha = ctx.getMessage().getProtocolParameters().get("sha");
70          }
71          String openId = "";
72          if(ctx.getMessage().getProtocolParameters().containsKey("openId")) {
73            openId = ctx.getMessage().getProtocolParameters().get("openId");
74          }
75          historyManager.logEvent(EntryType.CONTENT, repo, branch, sha, openId, "", ctx.getMessage().getProtocolParameters().get("uuid"), FAILED_LOG_MESSAGE, false, false, true, true);
76        }
77      }
78      return true;
79    }
80  
81    @Override
82    public void handleFailure(CommandContext ctx, Exception e) {
83  
84    }
85  
86  }