Skip to content

Commit

Permalink
Migrate keys and remove database cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Tananaev committed Dec 29, 2020
1 parent 4746877 commit c0dee04
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 42 deletions.
8 changes: 0 additions & 8 deletions setup/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@
SELECT * FROM tc_events WHERE deviceId = :deviceId AND serverTime BETWEEN :from AND :to ORDER BY serverTime
</entry>

<entry key='database.deletePositions'>
DELETE FROM tc_positions WHERE serverTime &lt; :serverTime AND id NOT IN (SELECT positionId FROM tc_devices WHERE positionId IS NOT NULL)
</entry>

<entry key='database.deleteEvents'>
DELETE FROM tc_events WHERE serverTime &lt; :serverTime
</entry>

<entry key='database.selectStatistics'>
SELECT * FROM tc_statistics WHERE captureTime BETWEEN :from AND :to ORDER BY captureTime
</entry>
Expand Down
14 changes: 0 additions & 14 deletions src/main/java/org/traccar/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,6 @@ private static void scheduleHealthCheck() {
}
}

private static void scheduleDatabaseCleanup() {
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
Context.getDataManager().clearHistory();
} catch (SQLException error) {
LOGGER.warn("Clear history error", error);
}
}
}, 0, CLEAN_PERIOD);
}

public static void run(String configFile) {
try {
Context.init(configFile);
Expand All @@ -147,7 +134,6 @@ public static void run(String configFile) {
Context.getScheduleManager().start();

scheduleHealthCheck();
scheduleDatabaseCleanup();

Thread.setDefaultUncaughtExceptionHandler((t, e) -> LOGGER.error("Thread exception", e));

Expand Down
24 changes: 23 additions & 1 deletion src/main/java/org/traccar/config/Keys.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,28 @@ public final class Keys {
"database.changelog",
Collections.singletonList(KeyType.GLOBAL));

/**
* Automatically generate SQL database queries when possible.
*/
public static final ConfigKey<Boolean> DATABASE_GENERATE_QUERIES = new ConfigKey<>(
"database.generateQueries",
Collections.singletonList(KeyType.GLOBAL));

/**
* Database connection pool size. Default value is defined by the HikariCP library.
*/
public static final ConfigKey<Integer> DATABASE_MAX_POOL_SIZE = new ConfigKey<>(
"database.maxPoolSize",
Collections.singletonList(KeyType.GLOBAL));

/**
* SQL query to check connection status. Default value is 'SELECT 1'. For Oracle database you can use
* 'SELECT 1 FROM DUAL'.
*/
public static final ConfigKey<String> DATABASE_CHECK_CONNECTION = new ConfigKey<>(
"database.checkConnection",
Collections.singletonList(KeyType.GLOBAL));
Collections.singletonList(KeyType.GLOBAL),
"SELECT 1");

/**
* Store original HEX or string data as "raw" attribute in the corresponding position.
Expand Down Expand Up @@ -320,6 +335,13 @@ public final class Keys {
Collections.singletonList(KeyType.GLOBAL),
60000L);

/**
* Authentication sessions timeout in seconds. By default no timeout.
*/
public static final ConfigKey<Integer> WEB_SESSION_TIMEOUT = new ConfigKey<>(
"web.sessionTimeout",
Collections.singletonList(KeyType.GLOBAL));

/**
* Enable positions forwarding to other web server.
*/
Expand Down
21 changes: 3 additions & 18 deletions src/main/java/org/traccar/database/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,15 @@ private void initDatabase() throws Exception {
hikariConfig.setJdbcUrl(config.getString(Keys.DATABASE_URL));
hikariConfig.setUsername(config.getString(Keys.DATABASE_USER));
hikariConfig.setPassword(config.getString(Keys.DATABASE_PASSWORD));
hikariConfig.setConnectionInitSql(config.getString(Keys.DATABASE_CHECK_CONNECTION, "SELECT 1"));
hikariConfig.setConnectionInitSql(config.getString(Keys.DATABASE_CHECK_CONNECTION));
hikariConfig.setIdleTimeout(600000);

int maxPoolSize = config.getInteger("database.maxPoolSize");

int maxPoolSize = config.getInteger(Keys.DATABASE_MAX_POOL_SIZE);
if (maxPoolSize != 0) {
hikariConfig.setMaximumPoolSize(maxPoolSize);
}

generateQueries = config.getBoolean("database.generateQueries");
generateQueries = config.getBoolean(Keys.DATABASE_GENERATE_QUERIES);

dataSource = new HikariDataSource(hikariConfig);
}
Expand Down Expand Up @@ -342,20 +341,6 @@ public Collection<Position> getLatestPositions() throws SQLException {
.executeQuery(Position.class);
}

public void clearHistory() throws SQLException {
long historyDays = config.getInteger("database.historyDays");
if (historyDays != 0) {
Date timeLimit = new Date(System.currentTimeMillis() - historyDays * 24 * 3600 * 1000);
LOGGER.info("Clearing history earlier than " + DateUtil.formatDate(timeLimit, false));
QueryBuilder.create(dataSource, getQuery("database.deletePositions"))
.setDate("serverTime", timeLimit)
.executeUpdate();
QueryBuilder.create(dataSource, getQuery("database.deleteEvents"))
.setDate("serverTime", timeLimit)
.executeUpdate();
}
}

public Server getServer() throws SQLException {
return QueryBuilder.create(dataSource, getQuery(ACTION_SELECT_ALL, Server.class))
.executeQuerySingle(Server.class);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/traccar/web/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private void initApi(Config config, ServletContextHandler servletHandler) {
}

private void initSessionConfig(Config config, ServletContextHandler servletHandler) {
int sessionTimeout = config.getInteger("web.sessionTimeout");
int sessionTimeout = config.getInteger(Keys.WEB_SESSION_TIMEOUT);
if (sessionTimeout > 0) {
servletHandler.getSessionHandler().setMaxInactiveInterval(sessionTimeout);
}
Expand Down

0 comments on commit c0dee04

Please sign in to comment.