Skip to content
Snippets Groups Projects
Commit 1f4bc6b1 authored by Grégory Mantelet's avatar Grégory Mantelet
Browse files

[UWS,TAP] Fix the weekly log file rotation.

When enabled, it was generating a file each minute on the day before the
specified day of week.

For instance: if the log rotation frequency was `W 1 0 0` (so, weekly on Sunday
at 00:00). The rotation was performed on Saturday midnight. But, because of a
bad index correction, the rotation kept going on the whole day of Saturday.
Since the rotated file is suffixed by the timestamp with hours and minutes
(no seconds), it actually generated a new log file for each minute of the
saturday. Of course, each time the file contained only one line (or 2 with some
luck)...which is pretty useless.
parent 6fb87a7d
No related branches found
No related tags found
No related merge requests found
...@@ -16,7 +16,7 @@ package uws.service.file; ...@@ -16,7 +16,7 @@ package uws.service.file;
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with UWSLibrary. If not, see <http://www.gnu.org/licenses/>. * along with UWSLibrary. If not, see <http://www.gnu.org/licenses/>.
* *
* Copyright 2014-2015 - UDS/Centre de Données astronomiques de Strasbourg (CDS), * Copyright 2014-2018 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Astronomisches Rechen Institut (ARI) * Astronomisches Rechen Institut (ARI)
*/ */
...@@ -100,9 +100,9 @@ import java.util.regex.MatchResult; ...@@ -100,9 +100,9 @@ import java.util.regex.MatchResult;
* *
* *
* @author Marc Wenger (CDS) * @author Marc Wenger (CDS)
* @author Gr&eacute;gory Mantelet (ARI) * @author Gr&eacute;gory Mantelet (ARI;CDS)
* @version 4.1 (02/2015) * @version 4.4 (07/2018)
* @since 4.1 * @since 4.4
*/ */
public final class EventFrequency { public final class EventFrequency {
...@@ -278,7 +278,8 @@ public final class EventFrequency { ...@@ -278,7 +278,8 @@ public final class EventFrequency {
int d = Integer.parseInt(dayNbStr); int d = Integer.parseInt(dayNbStr);
if (d >= 1 && d <= WEEK_DAYS.length) if (d >= 1 && d <= WEEK_DAYS.length)
return d - 1; return d - 1;
}catch(Exception e){} }catch(Exception e){
}
throw new IllegalStateException("Incorrect day of week (" + dayNbStr + ") ; it should be between 1 and 7 (both included)!"); throw new IllegalStateException("Incorrect day of week (" + dayNbStr + ") ; it should be between 1 and 7 (both included)!");
} }
...@@ -296,7 +297,8 @@ public final class EventFrequency { ...@@ -296,7 +297,8 @@ public final class EventFrequency {
int d = Integer.parseInt(dayStr); int d = Integer.parseInt(dayStr);
if (d >= 1 && d <= 31) if (d >= 1 && d <= 31)
return d; return d;
}catch(Exception e){} }catch(Exception e){
}
throw new IllegalStateException("Incorrect day of month (" + dayStr + ") ; it should be between 1 and 31 (both included)!"); throw new IllegalStateException("Incorrect day of month (" + dayStr + ") ; it should be between 1 and 31 (both included)!");
} }
...@@ -314,7 +316,8 @@ public final class EventFrequency { ...@@ -314,7 +316,8 @@ public final class EventFrequency {
int h = Integer.parseInt(hourStr); int h = Integer.parseInt(hourStr);
if (h >= 0 && h <= 23) if (h >= 0 && h <= 23)
return h; return h;
}catch(Exception e){} }catch(Exception e){
}
throw new IllegalStateException("Incorrect hour number(" + hourStr + ") ; it should be between 0 and 23 (both included)!"); throw new IllegalStateException("Incorrect hour number(" + hourStr + ") ; it should be between 0 and 23 (both included)!");
} }
...@@ -332,7 +335,8 @@ public final class EventFrequency { ...@@ -332,7 +335,8 @@ public final class EventFrequency {
int m = Integer.parseInt(minStr); int m = Integer.parseInt(minStr);
if (m >= 0 && m <= 59) if (m >= 0 && m <= 59)
return m; return m;
}catch(Exception e){} }catch(Exception e){
}
throw new IllegalStateException("Incorrect minute number (" + minStr + ") ; it should be between 0 and 59 (both included)!"); throw new IllegalStateException("Incorrect minute number (" + minStr + ") ; it should be between 0 and 59 (both included)!");
} }
...@@ -414,7 +418,7 @@ public final class EventFrequency { ...@@ -414,7 +418,7 @@ public final class EventFrequency {
// CASE: WEEKLY // CASE: WEEKLY
case 'W': case 'W':
// find the next right day to trigger the rotation // find the next right day to trigger the rotation
int weekday = date.get(Calendar.DAY_OF_WEEK); // sunday=1, ... saturday=7 int weekday = date.get(Calendar.DAY_OF_WEEK) - 1; // sunday=0, ... saturday=6
if (weekday == day){ if (weekday == day){
date.add(Calendar.WEEK_OF_YEAR, 1); date.add(Calendar.WEEK_OF_YEAR, 1);
}else{ }else{
...@@ -478,7 +482,7 @@ public final class EventFrequency { ...@@ -478,7 +482,7 @@ public final class EventFrequency {
str.append(" at ").append(NN.format(hour)).append(':').append(NN.format(min)); str.append(" at ").append(NN.format(hour)).append(':').append(NN.format(min));
break; break;
case 'W': case 'W':
str.append("weekly on ").append(WEEK_DAYS[day % 7]); str.append("weekly on ").append(WEEK_DAYS[day % 6]);
str.append(" at ").append(NN.format(hour)).append(':').append(NN.format(min)); str.append(" at ").append(NN.format(hour)).append(':').append(NN.format(min));
break; break;
case 'M': case 'M':
......
...@@ -8,6 +8,9 @@ import java.io.BufferedReader; ...@@ -8,6 +8,9 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.junit.Test; import org.junit.Test;
...@@ -274,4 +277,43 @@ public class TestLogRotation { ...@@ -274,4 +277,43 @@ public class TestLogRotation {
} }
} }
@Test
public void testNextEvent(){
// Event = weekly on Sunday at midnight
EventFrequency event = new EventFrequency("W 1 0 0");
assertEquals("weekly on Sunday at 00:00", event.toString());
/* CASE: Today = Friday */
// Reference date for the test: Friday 27th July 2018 at noon
GregorianCalendar date = new GregorianCalendar(2018, Calendar.JULY, 27, 12, 0);
assertEquals(Calendar.FRIDAY, date.get(Calendar.DAY_OF_WEEK));
// Get the next event from the given date:
Date nextEvent = event.nextEvent(date.getTime());
GregorianCalendar expectedNextEvent = new GregorianCalendar(2018, Calendar.JULY, 29, 0, 0);
assertEquals(expectedNextEvent.getTime().getTime(), nextEvent.getTime());
/* CASE: Today = Saturday */
// Reference date for the test: Saturday 28th July 2018 at noon
date = new GregorianCalendar(2018, Calendar.JULY, 28, 12, 0);
assertEquals(Calendar.SATURDAY, date.get(Calendar.DAY_OF_WEEK));
// Get the next event from the given date:
nextEvent = event.nextEvent(date.getTime());
assertEquals(expectedNextEvent.getTime().getTime(), nextEvent.getTime());
/* CASE: Today = Sunday */
// Reference date for the test: Sunday 28th July 2018 at noon
date = new GregorianCalendar(2018, Calendar.JULY, 29, 12, 0);
assertEquals(Calendar.SUNDAY, date.get(Calendar.DAY_OF_WEEK));
// Get the next event from the given date:
nextEvent = event.nextEvent(date.getTime());
expectedNextEvent = new GregorianCalendar(2018, Calendar.AUGUST, 5, 0, 0);
assertEquals(expectedNextEvent.getTime().getTime(), nextEvent.getTime());
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment