Author: chatellier Date: 2010-01-04 15:49:25 +0000 (Mon, 04 Jan 2010) New Revision: 2868 Added: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/cron/ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/cron/CronService.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/cron/RemoveOldFileTask.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/cron/package-info.java Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/IsisFish.java Log: Define a cron service. One task implemented : remove old files. Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/IsisFish.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/IsisFish.java 2010-01-04 14:22:06 UTC (rev 2867) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/IsisFish.java 2010-01-04 15:49:25 UTC (rev 2868) @@ -51,6 +51,7 @@ import org.nuiton.util.Version; import org.nuiton.widget.SwingUtil; +import fr.ifremer.isisfish.cron.CronService; import fr.ifremer.isisfish.datastore.AnalysePlanStorage; import fr.ifremer.isisfish.datastore.ExportStorage; import fr.ifremer.isisfish.datastore.FormuleStorage; @@ -149,6 +150,10 @@ // action after ui launched config.doAction(IsisConfig.STEP_AFTER_UI); + + // start cron service + CronService cronService = new CronService(); + cronService.start(); } catch (Exception e) { if (log.isErrorEnabled()) { log.error("Global IsisFish exception", e); Added: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/cron/CronService.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/cron/CronService.java (rev 0) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/cron/CronService.java 2010-01-04 15:49:25 UTC (rev 2868) @@ -0,0 +1,99 @@ +/* *##% + * Copyright (C) 2010 Code Lutin, Chatellier Eric + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package fr.ifremer.isisfish.cron; + +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedList; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * Main cron service job. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class CronService extends Thread { + + /** Class logger. */ + private static Log log = LogFactory.getLog(CronService.class); + + /** Registered services. */ + protected Collection<Runnable> services; + + /** + * Init cron service with default available services. + */ + public CronService() { + services = new LinkedList<Runnable>(); + registerDefaultServices(); + } + + /** + * Register default services (always executed if service is launched). + */ + private void registerDefaultServices() { + addService(new RemoveOldFileTask()); + } + + /** + * Add a new task for execution. + * + * @param task task to add + */ + protected void addService(Runnable task) { + services.add(task); + } + + /** + * Run all registered services once and stop. + * + * This behavior may change later to run as a real cron service. + */ + public void run() { + + if (log.isInfoEnabled()) { + log.info("Starting " + services.size() + " registered services"); + } + + Iterator<Runnable> itOnTasks = services.iterator(); + + while (itOnTasks.hasNext()) { + Runnable task = itOnTasks.next(); + Thread taskThread = new Thread(task); + try { + taskThread.start(); + taskThread.join(); // run sequential + } + catch (Exception ex) { + if (log.isDebugEnabled()) { + log.debug("Task " + task.getClass().getSimpleName() + " failed to run", ex); + } + } + } + + // free memory + services.clear(); + } +} Property changes on: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/cron/CronService.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL" Added: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/cron/RemoveOldFileTask.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/cron/RemoveOldFileTask.java (rev 0) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/cron/RemoveOldFileTask.java 2010-01-04 15:49:25 UTC (rev 2868) @@ -0,0 +1,83 @@ +/* *##% + * Copyright (C) 2010 Code Lutin, Chatellier Eric + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package fr.ifremer.isisfish.cron; + +import java.io.File; + +import org.apache.commons.lang.time.DateUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import fr.ifremer.isisfish.IsisFish; + +/** + * This cron task is used to remove old isis files. + * + * Currently remove : + * <ul> + * <li>monitored simulations files older than 1 year</li> + * </ul> + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class RemoveOldFileTask implements Runnable { + + /** Class logger. */ + private static Log log = LogFactory.getLog(RemoveOldFileTask.class); + + /* + * @see java.lang.Runnable#run() + */ + @Override + public void run() { + if (log.isDebugEnabled()) { + log.debug("Remove old simulation files task called"); + } + removeMonitoredSimulations(); + } + + /** + * Remove old *.zip files located in monitoring directory. + */ + protected void removeMonitoredSimulations() { + if (log.isDebugEnabled()) { + log.debug("Removing old monitored simulations files"); + } + + File directory = IsisFish.config.getMonitoringDirectory(); + File[] files = directory.listFiles(); + + // supprime les fichier au premier niveau (pour le moment) + // qui sont plus vieux qu'un an + for (File file : files) { + if (file.isFile()) { + if (file.lastModified() + DateUtils.MILLIS_PER_DAY * 365 < System.currentTimeMillis()) { + if (log.isDebugEnabled()) { + log.debug("Removing file " + file); + } + file.delete(); + } + } + } + } +} Property changes on: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/cron/RemoveOldFileTask.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL" Added: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/cron/package-info.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/cron/package-info.java (rev 0) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/cron/package-info.java 2010-01-04 15:49:25 UTC (rev 2868) @@ -0,0 +1,5 @@ +/** + * Cron service and scripts. + */ +package fr.ifremer.isisfish.cron; + Property changes on: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/cron/package-info.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL"
participants (1)
-
chatellierï¼ users.labs.libre-entreprise.org