Skip to content

Commit

Permalink
SEBSERV-613
Browse files Browse the repository at this point in the history
  • Loading branch information
anhefti committed Dec 18, 2024
1 parent b3292a0 commit 2aebfb8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,9 @@ Result<Void> registerInstruction(
/** Used to clean up out-dated instructions on the persistent storage */
void cleanupInstructions();

/** Used to send automated quit instruction to given SEB connection.
*
* @param connectionToken SEB connection token to send quit instruction to
* @param examId exam identifier, if null try to get it from connection*/
void sendQuitInstruction(String connectionToken, Long examId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ public ClientConnectionDataInternal getClientConnection(final String connectionT
}

final ClientConnection clientConnection = getClientConnectionByToken(connectionToken);
// TODO check running exam within cache instead of DB call
if (clientConnection == null || (clientConnection.examId != null && !examDAO.isRunning(clientConnection.examId))) {
return null;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import ch.ethz.seb.sebserver.gbl.model.session.ClientConnection;
import ch.ethz.seb.sebserver.gbl.model.session.ClientInstruction;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
Expand Down Expand Up @@ -272,6 +274,40 @@ public void cleanupInstructions() {
}
}

@Override
public void sendQuitInstruction(final String connectionToken, final Long examId) {
Long _examId = examId;
if (examId == null) {
final Result<ClientConnection> clientConnectionResult = clientConnectionDAO
.byConnectionToken(connectionToken);

if (clientConnectionResult.hasError()) {
log.error(
"Failed to get examId for client connection token: {} error: {}",
connectionToken,
clientConnectionResult.getError().getMessage());
}

_examId = clientConnectionResult.get().examId;
}

if (_examId != null) {

log.info("Send automated quit instruction to SEB for connection token: {}", connectionToken);

// TODO add SEB event log that SEB Server has automatically send quit instruction to SEB

this.registerInstruction(
_examId,
ClientInstruction.InstructionType.SEB_QUIT,
Collections.emptyMap(),
connectionToken,
false,
false
);
}
}

private ClientInstructionRecord getNextInstruction(final String connectionToken) {
// if we still have instruction for given connectionToken, process them first
final long activeTime = DateTime.now(DateTimeZone.UTC).minusMinutes(1).getMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@

package ch.ethz.seb.sebserver.webservice.servicelayer.session.impl;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import ch.ethz.seb.sebserver.gbl.model.session.ClientConnection;
import ch.ethz.seb.sebserver.gbl.model.session.ClientInstruction;
import ch.ethz.seb.sebserver.gbl.util.Result;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.ClientConnectionDAO;
import org.apache.commons.lang3.StringUtils;
import org.ehcache.impl.internal.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
Expand All @@ -41,20 +37,17 @@ public class SEBClientPingBatchService implements SEBClientPingService {

private final ExamSessionCacheService examSessionCacheService;
private final SEBClientInstructionService sebClientInstructionService;
private final ClientConnectionDAO clientConnectionDAO;

private final Set<String> pingKeys = new HashSet<>();
private final Map<String, String> pings = new ConcurrentHashMap<>();
private final Map<String, String> instructions = new ConcurrentHashMap<>();

public SEBClientPingBatchService(
final ExamSessionCacheService examSessionCacheService,
final SEBClientInstructionService sebClientInstructionService,
final ClientConnectionDAO clientConnectionDAO) {
final SEBClientInstructionService sebClientInstructionService) {

this.examSessionCacheService = examSessionCacheService;
this.sebClientInstructionService = sebClientInstructionService;
this.clientConnectionDAO = clientConnectionDAO;
}

@Scheduled(fixedDelayString = "${sebserver.webservice.api.exam.session.ping.batch.interval:500}")
Expand Down Expand Up @@ -121,13 +114,15 @@ private void processPing(
if (connectionData != null) {
if (connectionData.clientConnection.status == ClientConnection.ConnectionStatus.DISABLED) {
// SEBSERV-440 send quit instruction to SEB
sendQuitInstruction(connectionToken, connectionData.clientConnection.examId);
this.sebClientInstructionService.sendQuitInstruction(
connectionToken,
connectionData.clientConnection.examId);
}

connectionData.notifyPing(timestamp);
} else {
log.warn("Failed to get ClientConnectionDataInternal probably due to finished Exam for: {}.", connectionToken);
sendQuitInstruction(connectionToken,null);
this.sebClientInstructionService.sendQuitInstruction(connectionToken,null);
}

if (StringUtils.isNotBlank(instructionConfirm)) {
Expand All @@ -144,37 +139,5 @@ private void processPing(
}
}

private void sendQuitInstruction(final String connectionToken, final Long examId) {

Long _examId = examId;
if (examId == null) {
final Result<ClientConnection> clientConnectionResult = clientConnectionDAO
.byConnectionToken(connectionToken);

if (clientConnectionResult.hasError()) {
log.error(
"Failed to get examId for client connection token: {} error: {}",
connectionToken,
clientConnectionResult.getError().getMessage());
}

_examId = clientConnectionResult.get().examId;
}

if (_examId != null) {

log.info("Send automated quit instruction to SEB for connection token: {}", connectionToken);

// TODO add SEB event log that SEB Server has automatically send quit instruction to SEB

sebClientInstructionService.registerInstruction(
_examId,
ClientInstruction.InstructionType.SEB_QUIT,
Collections.emptyMap(),
connectionToken,
false,
false
);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

package ch.ethz.seb.sebserver.webservice.servicelayer.session.impl;

import java.util.Collections;

import ch.ethz.seb.sebserver.gbl.model.session.ClientConnection;
import ch.ethz.seb.sebserver.gbl.model.session.ClientInstruction;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -34,7 +31,7 @@ public class SEBClientPingBlockingService implements SEBClientPingService {

private final ExamSessionCacheService examSessionCacheService;
private final SEBClientInstructionService sebClientInstructionService;

public SEBClientPingBlockingService(
final ExamSessionCacheService examSessionCacheService,
final SEBClientInstructionService sebClientInstructionService) {
Expand All @@ -60,19 +57,16 @@ public String notifyPing(final String connectionToken, final String instructionC
if (connectionData != null) {
if (connectionData.clientConnection.status == ClientConnection.ConnectionStatus.DISABLED) {
// SEBSERV-440 send quit instruction to SEB
sebClientInstructionService.registerInstruction(
connectionData.clientConnection.examId,
ClientInstruction.InstructionType.SEB_QUIT,
Collections.emptyMap(),
connectionData.clientConnection.connectionToken,
false,
false
);
this.sebClientInstructionService.sendQuitInstruction(
connectionToken,
connectionData.clientConnection.examId);
}

connectionData.notifyPing(Utils.getMillisecondsNow());
} else {
log.error("Failed to get ClientConnectionDataInternal for: {}", connectionToken);
// SEBSERV-613 send quit instruction if exam is not running
log.warn("Failed to get ClientConnectionDataInternal probably due to finished Exam for: {}.", connectionToken);
this.sebClientInstructionService.sendQuitInstruction(connectionToken,null);
}

if (StringUtils.isNotBlank(instructionConfirm)) {
Expand All @@ -81,5 +75,6 @@ public String notifyPing(final String connectionToken, final String instructionC

return this.sebClientInstructionService.getInstructionJSON(connectionToken);
}


}

0 comments on commit 2aebfb8

Please sign in to comment.