@Override public long getTimeMillis() { return timeSource.currentTimeMillis(); } }
@Override public void setFinished(ScheduledTask task) { lastFinishCache.put(task, timeSource.currentTimeMillis()); }
/** * @return string representing the offset of the given time zone from GMT */ public String getDisplayOffset(@Nullable TimeZone timeZone) { if (timeZone == null) return ""; int offsetSec = timeZone.getOffset(timeSource.currentTimeMillis()) / 1000; int offsetHours = offsetSec / 3600; int offsetMins = (offsetSec % 3600) / 60; String str = StringUtils.leftPad(String.valueOf(Math.abs(offsetHours)), 2, '0') + ":" + StringUtils.leftPad(String.valueOf(Math.abs(offsetMins)), 2, '0'); String sign = offsetHours >= 0 ? "+" : "-"; return "GMT" + sign + str; }
@Override public boolean setRunning(ScheduledTask task, boolean running) { log.trace("{}: mark running={}", task, running); if (running) { task.setCurrentStartTimestamp(timeSource.currentTimeMillis()); Long prev = runningTasks.putIfAbsent(task, task.getCurrentStartTimestamp()); return prev != null; } else { Long startTime = runningTasks.get(task); if (Objects.equals(task.getCurrentStartTimestamp(), startTime)) { runningTasks.remove(task); } return false; } }
protected boolean isRunning(ScheduledTask task) { Long startTime = runningTasks.get(task); if (startTime != null) { boolean timedOut; if (task.getTimeout() != null && task.getTimeout() != 0) { timedOut = (startTime + task.getTimeout() * 1000) < timeSource.currentTimeMillis(); } else { timedOut = (startTime + 1000 * 60 * 60 * 3) < timeSource.currentTimeMillis(); } if (timedOut) { runningTasks.remove(task); } else { return true; } } return false; }
@Override public void runOnce(ScheduledTask task) { UserSession userSession; try { userSession = getUserSession(task); } catch (LoginException e) { throw new RuntimeException("Unable to obtain user session", e); } Integer priority = getServerPriority(task, serverInfo.getServerId()); if (priority == null) { throw new RunTaskOnceException(task.toString()); } lastStartCache.put(task, timeSource.currentTimeMillis()); runner.runTaskOnce(task, timeSource.currentTimeMillis(), userSession); } }
@Override public void propagate(UUID id) { UserSessionInfo usi = getSessionInfo(id); if (usi != null) { long now = timeSource.currentTimeMillis(); usi.lastUsedTs = now; usi.lastSentTs = now; putSessionInfo(id, usi); clusterManager.send(usi); } }
@Override public void postMessage(List<UUID> sessionIds, String message) { long time = timeSource.currentTimeMillis(); for (UUID sessionId : sessionIds) { UserSession userSession = userSessions.get(sessionId); if (userSession != null && !userSession.isSystem()) { userSession.setAttribute(MESSAGE_ATTR_PREFIX + time, message); userSessions.propagate(sessionId); } } }
@Override public void processEviction() { if (!AppContext.isStarted()) return; log.trace("Processing eviction"); long now = timeSource.currentTimeMillis(); getSessionInfoStream() .filter(info -> !info.session.isSystem() && now > (info.lastUsedTs + toMillis(expirationTimeout))) .forEach(usi -> { log.debug("Removing session due to timeout: {}", usi); userSessionLog.updateSessionLogRecord(usi.getSession(), SessionAction.EXPIRATION); removeSessionInfo(usi.session.getId()); usi.lastUsedTs = 0; clusterManager.send(usi); }); }
@Override public void add(UserSession session) { UserSessionInfo usi = new UserSessionInfo(session, timeSource.currentTimeMillis()); putSessionInfo(session.getId(), usi); if (!session.isSystem()) { if (serverConfig.getSyncNewUserSessionReplication()) clusterManager.sendSync(usi); else clusterManager.send(usi); } }
@Nullable protected UserSession internalGet(UUID id, boolean touch, boolean propagate) { if (!AppContext.isStarted()) return NO_USER_SESSION; UserSessionInfo usi = getSessionInfo(id); if (usi != null) { if (touch) { long now = timeSource.currentTimeMillis(); if (now > (usi.lastUsedTs + toMillis(touchTimeout))) { usi.lastUsedTs = now; putSessionInfo(id, usi); } if (propagate && !usi.session.isSystem()) { if (now > (usi.lastSentTs + toMillis(sendTimeout))) { usi.lastSentTs = now; clusterManager.send(usi); } } } return usi.session; } return null; }
@Override public void processScheduledTasks(boolean onlyIfActive) { if (onlyIfActive && !isActive()) return; log.debug("Processing scheduled tasks"); if (schedulingStartTime == 0) schedulingStartTime = timeSource.currentTimeMillis(); authentication.begin(); try { StopWatch sw = new Slf4JStopWatch("Scheduling.processTasks"); Coordinator.Context context = coordinator.begin(); try { for (ScheduledTask task : context.getTasks()) { processTask(task); } } finally { coordinator.end(context); } sw.stop(); } finally { authentication.end(); } }
long now = timeSource.currentTimeMillis(); String me = serverInfo.getServerId();