View Javadoc

1   package org.csc.phynixx.watchdog;
2   
3   /*
4    * #%L
5    * phynixx-watchdog
6    * %%
7    * Copyright (C) 2014 csc
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  
24  import org.csc.phynixx.common.logger.IPhynixxLogger;
25  import org.csc.phynixx.common.logger.PhynixxLogManager;
26  import org.csc.phynixx.watchdog.log.CheckConditionFailedLog;
27  import org.csc.phynixx.watchdog.log.ConditionViolatedLog;
28  
29  /**
30   * Checks if the watchdig is alive.
31   * If not it is restarted ...
32   */
33  
34  class RestartCondition extends TimeoutCondition implements IWatchedCondition {
35  
36      private IPhynixxLogger log = PhynixxLogManager.getLogger(this.getClass());
37  
38      private WatchdogReference watchdogReference = null;
39  
40      RestartCondition(long checkInterval, Watchdog wd) {
41          super(checkInterval);
42          this.watchdogReference = new WatchdogReference(wd);
43      }
44  
45      /**
46       * Not synchronized as to be meant for the watch dog exclusively
47       * <p/>
48       * Do not call it not synchronized
49       */
50      public boolean checkCondition() {
51  
52          // assure that the checkinterval is elapsed .....
53          if (super.checkCondition()) {
54              return true;
55          }
56  
57          if (this.watchdogReference.isStale()) {
58              return false;
59          }
60  
61          Watchdog wd = this.watchdogReference.getWatchdog();
62  
63          //log.info("Checking "+this+"\n watched WD is alive="+watchedWatchdog.isAlive()+" is killed="+watchedWatchdog.isKilled()+" WD-Thead="+this.watchedWatchdog.getThreadHandle());
64          if (!wd.isAlive()) {
65              if (log.isInfoEnabled()) {
66                  String logString = "RestartCondition :: Watchdog " + wd.getThreadHandle() + " is not alive ";
67                  log.info(new CheckConditionFailedLog(this, logString).toString());
68              }
69              return false;
70          }
71          return true;
72      }
73  
74  
75      public String toString() {
76          return " RestartCondition referenced Watchdog " + watchdogReference.getId() + " and check interval=" + this.getTimeout();
77      }
78  
79      /**
80       * Not synchronized as to be meant for the watch dog exclusively
81       * <p/>
82       * Do not call it unsynchronized
83       */
84      public void conditionViolated() {
85          if (this.watchdogReference.isStale()) {
86              throw new IllegalStateException("Watchdog is stale and does not exist any longer");
87          }
88  
89          Watchdog wd = this.watchdogReference.getWatchdog();
90  
91          wd.restart();
92          if (log.isInfoEnabled()) {
93              log.info(new ConditionViolatedLog(this, "Watchdog " + wd.getId() + " is restarted by Condition  " + this.toString()).toString());
94          }
95  
96      }
97  
98  
99      public boolean isUseless() {
100         return this.watchdogReference!=null || this.watchdogReference.isStale();
101     }
102 
103 
104 }