1 package hudson.triggers; 2 3 import antlr.ANTLRException; 4 import hudson.ExtensionPoint; 5 import hudson.model.AbstractProject; 6 import hudson.model.Action; 7 import hudson.model.Build; 8 import hudson.model.Describable; 9 import hudson.model.FingerprintCleanupThread; 10 import hudson.model.Hudson; 11 import hudson.model.Project; 12 import hudson.model.WorkspaceCleanupThread; 13 import hudson.model.Item; 14 import hudson.scheduler.CronTab; 15 import hudson.scheduler.CronTabList; 16 17 import java.io.InvalidObjectException ; 18 import java.io.ObjectStreamException ; 19 import java.util.Calendar ; 20 import java.util.Collections ; 21 import java.util.GregorianCalendar ; 22 import java.util.Timer ; 23 import java.util.TimerTask ; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 27 36 public abstract class Trigger<J extends Item> implements Describable<Trigger<?>>, ExtensionPoint { 37 38 47 public void start(J project, boolean newInstance) { 48 this.job = project; 49 } 50 51 57 protected void run() {} 58 59 68 public void stop() {} 69 70 74 public Action getProjectAction() { 75 return null; 76 } 77 78 public abstract TriggerDescriptor getDescriptor(); 79 80 81 82 protected final String spec; 83 protected transient CronTabList tabs; 84 protected transient J job; 85 86 91 protected Trigger(String cronTabSpec) throws ANTLRException { 92 this.spec = cronTabSpec; 93 this.tabs = CronTabList.create(cronTabSpec); 94 } 95 96 99 protected Trigger() { 100 this.spec = ""; 101 this.tabs = new CronTabList(Collections.<CronTab>emptyList()); 102 } 103 104 109 public final String getSpec() { 110 return spec; 111 } 112 113 private Object readResolve() throws ObjectStreamException { 114 try { 115 tabs = CronTabList.create(spec); 116 } catch (ANTLRException e) { 117 InvalidObjectException x = new InvalidObjectException (e.getMessage()); 118 x.initCause(e); 119 throw x; 120 } 121 return this; 122 } 123 124 125 128 private static class Cron extends TimerTask { 129 private final Calendar cal = new GregorianCalendar (); 130 131 public void run() { 132 LOGGER.fine("cron checking "+cal.getTime().toLocaleString()); 133 134 try { 135 Hudson inst = Hudson.getInstance(); 136 for (AbstractProject<?,?> p : inst.getAllItems(AbstractProject.class)) { 137 for (Trigger t : p.getTriggers().values()) { 138 LOGGER.fine("cron checking "+p.getName()); 139 if(t.tabs.check(cal)) { 140 LOGGER.fine("cron triggered "+p.getName()); 141 t.run(); 142 } 143 } 144 } 145 } catch (Throwable e) { 146 LOGGER.log(Level.WARNING,"Cron thread throw an exception",e); 147 e.printStackTrace(); 149 } 150 151 cal.add(Calendar.MINUTE,1); 152 } 153 } 154 155 private static final Logger LOGGER = Logger.getLogger(Trigger.class.getName()); 156 157 161 public static final Timer timer = new Timer (); 163 public static void init() { 164 timer.scheduleAtFixedRate(new Cron(), 1000*60, 1000*60); 165 166 long HOUR = 1000*60*60; 168 long DAY = HOUR*24; 169 timer.scheduleAtFixedRate(new FingerprintCleanupThread(),DAY,DAY); 170 timer.scheduleAtFixedRate(new WorkspaceCleanupThread(),DAY+4*HOUR,DAY); 171 } 172 } 173 | Popular Tags |