Add a timeout of 2 seconds for each online check to make sure the online check doesn't get blocked somehow
This commit is contained in:
parent
a008fff084
commit
04eb5a7ad1
|
@ -45,7 +45,7 @@
|
||||||
<logger name="ctbrec.LoggingInterceptor" level="INFO"/>
|
<logger name="ctbrec.LoggingInterceptor" level="INFO"/>
|
||||||
<logger name="ctbrec.io.CookieJarImpl" level="INFO"/>
|
<logger name="ctbrec.io.CookieJarImpl" level="INFO"/>
|
||||||
<logger name="ctbrec.recorder.FFmpeg" level="DEBUG"/>
|
<logger name="ctbrec.recorder.FFmpeg" level="DEBUG"/>
|
||||||
<logger name="ctbrec.recorder.OnlineMonitor" level="INFO"/>
|
<logger name="ctbrec.recorder.OnlineMonitor" level="DEBUG"/>
|
||||||
<logger name="ctbrec.recorder.RecordingFileMonitor" level="TRACE"/>
|
<logger name="ctbrec.recorder.RecordingFileMonitor" level="TRACE"/>
|
||||||
<logger name="ctbrec.recorder.download.dash.DashDownload" level="DEBUG"/>
|
<logger name="ctbrec.recorder.download.dash.DashDownload" level="DEBUG"/>
|
||||||
<logger name="ctbrec.recorder.server.HlsServlet" level="INFO"/>
|
<logger name="ctbrec.recorder.server.HlsServlet" level="INFO"/>
|
||||||
|
|
|
@ -1,44 +1,38 @@
|
||||||
package ctbrec.recorder;
|
package ctbrec.recorder;
|
||||||
|
|
||||||
import static ctbrec.Model.State.*;
|
|
||||||
|
|
||||||
import java.io.InterruptedIOException;
|
|
||||||
import java.net.SocketTimeoutException;
|
|
||||||
import java.time.Duration;
|
|
||||||
import java.time.Instant;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import ctbrec.Config;
|
import ctbrec.Config;
|
||||||
import ctbrec.Model;
|
import ctbrec.Model;
|
||||||
import ctbrec.event.EventBusHolder;
|
import ctbrec.event.EventBusHolder;
|
||||||
import ctbrec.event.ModelIsOnlineEvent;
|
import ctbrec.event.ModelIsOnlineEvent;
|
||||||
import ctbrec.event.ModelStateChangedEvent;
|
import ctbrec.event.ModelStateChangedEvent;
|
||||||
import ctbrec.io.HttpException;
|
import ctbrec.io.HttpException;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.InterruptedIOException;
|
||||||
|
import java.net.SocketTimeoutException;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
|
import static ctbrec.Model.State.UNKNOWN;
|
||||||
|
|
||||||
public class OnlineMonitor extends Thread {
|
public class OnlineMonitor extends Thread {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(OnlineMonitor.class);
|
private static final Logger LOG = LoggerFactory.getLogger(OnlineMonitor.class);
|
||||||
private static final boolean IGNORE_CACHE = true;
|
private static final boolean IGNORE_CACHE = true;
|
||||||
|
private static final long TIMEOUT_IN_MILLIS = 2000;
|
||||||
|
|
||||||
private volatile boolean running = false;
|
private volatile boolean running = false;
|
||||||
private Recorder recorder;
|
private final Recorder recorder;
|
||||||
|
|
||||||
private Map<Model, Model.State> states = new HashMap<>();
|
private final Map<Model, Model.State> states = new HashMap<>();
|
||||||
|
|
||||||
private Map<String, ExecutorService> executors = new HashMap<>();
|
private final Map<String, ExecutorService> executors = new HashMap<>();
|
||||||
private Config config;
|
private final Config config;
|
||||||
|
|
||||||
public OnlineMonitor(Recorder recorder, Config config) {
|
public OnlineMonitor(Recorder recorder, Config config) {
|
||||||
this.recorder = recorder;
|
this.recorder = recorder;
|
||||||
|
@ -68,19 +62,14 @@ public class OnlineMonitor extends Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeDeletedModels(List<Model> models) {
|
private void removeDeletedModels(List<Model> models) {
|
||||||
for (Iterator<Model> iterator = states.keySet().iterator(); iterator.hasNext();) {
|
states.keySet().removeIf(model -> !models.contains(model));
|
||||||
Model model = iterator.next();
|
|
||||||
if(!models.contains(model)) {
|
|
||||||
iterator.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateModels(List<Model> models) {
|
private void updateModels(List<Model> models) {
|
||||||
// sort models by priority
|
// sort models by priority
|
||||||
Collections.sort(models, (a, b) -> b.getPriority() - a.getPriority());
|
models.sort((a, b) -> b.getPriority() - a.getPriority());
|
||||||
// submit online check jobs to the executor for the model's site
|
// submit online check jobs to the executor for the model's site
|
||||||
List<Future<?>> futures = new LinkedList<>();
|
List<ModelAwareFuture> futures = new LinkedList<>();
|
||||||
for (Model model : models) {
|
for (Model model : models) {
|
||||||
boolean skipCheckForSuspended = config.getSettings().onlineCheckSkipsPausedModels && model.isSuspended();
|
boolean skipCheckForSuspended = config.getSettings().onlineCheckSkipsPausedModels && model.isSuspended();
|
||||||
boolean skipCheckForMarkedAsLater = model.isMarkedForLaterRecording();
|
boolean skipCheckForMarkedAsLater = model.isMarkedForLaterRecording();
|
||||||
|
@ -91,18 +80,21 @@ public class OnlineMonitor extends Thread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// wait for all jobs to finish
|
// wait for all jobs to finish
|
||||||
for (Future<?> future : futures) {
|
for (ModelAwareFuture future : futures) {
|
||||||
try {
|
try {
|
||||||
future.get();
|
future.get(TIMEOUT_IN_MILLIS, TimeUnit.MILLISECONDS);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
LOG.debug("Online check interrupted for model {}", future.getModel(), e);
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
} catch (ExecutionException e) {
|
} catch (ExecutionException e) {
|
||||||
LOG.info("Error while checking online state", e);
|
LOG.info("Error while checking online state for model {}", future.getModel(), e);
|
||||||
|
} catch (TimeoutException e) {
|
||||||
|
LOG.debug("Online check didn't finish after {}ms for model {}", TIMEOUT_IN_MILLIS, future.getModel());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Future<?> updateModel(Model model) {
|
private ModelAwareFuture updateModel(Model model) {
|
||||||
final String siteName = model.getSite().getName();
|
final String siteName = model.getSite().getName();
|
||||||
ExecutorService executor = executors.computeIfAbsent(siteName, name -> Executors.newSingleThreadExecutor(r -> {
|
ExecutorService executor = executors.computeIfAbsent(siteName, name -> Executors.newSingleThreadExecutor(r -> {
|
||||||
Thread t = new Thread(r);
|
Thread t = new Thread(r);
|
||||||
|
@ -112,14 +104,14 @@ public class OnlineMonitor extends Thread {
|
||||||
return t;
|
return t;
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return executor.submit(() -> {
|
var future = executor.submit(() -> {
|
||||||
try {
|
try {
|
||||||
if (model.isOnline(IGNORE_CACHE)) {
|
if (model.isOnline(IGNORE_CACHE)) {
|
||||||
EventBusHolder.BUS.post(new ModelIsOnlineEvent(model));
|
EventBusHolder.BUS.post(new ModelIsOnlineEvent(model));
|
||||||
model.setLastSeen(Instant.now());
|
model.setLastSeen(Instant.now());
|
||||||
}
|
}
|
||||||
Model.State state = model.getOnlineState(false);
|
Model.State state = model.getOnlineState(false);
|
||||||
LOG.trace("Model online state: {} {}", model.getName(), state);
|
LOG.debug("Model online state: {} {}", model.getName(), state);
|
||||||
Model.State oldState = states.getOrDefault(model, UNKNOWN);
|
Model.State oldState = states.getOrDefault(model, UNKNOWN);
|
||||||
states.put(model, state);
|
states.put(model, state);
|
||||||
if (state != oldState) {
|
if (state != oldState) {
|
||||||
|
@ -137,7 +129,9 @@ public class OnlineMonitor extends Thread {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOG.error("Couldn't check if model {} is online", model.getName(), e);
|
LOG.error("Couldn't check if model {} is online", model.getName(), e);
|
||||||
}
|
}
|
||||||
|
return model;
|
||||||
});
|
});
|
||||||
|
return new ModelAwareFuture(model, future);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void suspendUntilNextIteration(List<Model> models, Duration timeCheckTook) {
|
private void suspendUntilNextIteration(List<Model> models, Duration timeCheckTook) {
|
||||||
|
@ -164,4 +158,36 @@ public class OnlineMonitor extends Thread {
|
||||||
}
|
}
|
||||||
interrupt();
|
interrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private record ModelAwareFuture(Model model, Future<Model> future) implements Future<Model> {
|
||||||
|
|
||||||
|
public Model getModel() {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean cancel(boolean mayInterruptIfRunning) {
|
||||||
|
return future.cancel(mayInterruptIfRunning);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCancelled() {
|
||||||
|
return future.isCancelled();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isDone() {
|
||||||
|
return future.isDone();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Model get() throws InterruptedException, ExecutionException {
|
||||||
|
return future.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Model get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
|
||||||
|
return future.get(timeout, unit);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -20,6 +20,7 @@ import java.net.URLDecoder;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
@ -672,7 +673,9 @@ public class MyFreeCamsClient {
|
||||||
LOG.trace("Sending USERNAMELOOKUP for {}", q);
|
LOG.trace("Sending USERNAMELOOKUP for {}", q);
|
||||||
Object monitor = new Object();
|
Object monitor = new Object();
|
||||||
int msgId = messageId++;
|
int msgId = messageId++;
|
||||||
|
AtomicBoolean searchDone = new AtomicBoolean(false);
|
||||||
responseHandlers.put(msgId, msg -> {
|
responseHandlers.put(msgId, msg -> {
|
||||||
|
try {
|
||||||
LOG.trace("Search result: {}", msg);
|
LOG.trace("Search result: {}", msg);
|
||||||
if (StringUtil.isNotBlank(msg.getMessage()) && !Objects.equals(msg.getMessage(), q)) {
|
if (StringUtil.isNotBlank(msg.getMessage()) && !Objects.equals(msg.getMessage(), q)) {
|
||||||
JSONObject json = new JSONObject(msg.getMessage());
|
JSONObject json = new JSONObject(msg.getMessage());
|
||||||
|
@ -695,13 +698,21 @@ public class MyFreeCamsClient {
|
||||||
model.setPreview(previewUrl);
|
model.setPreview(previewUrl);
|
||||||
result.add(model);
|
result.add(model);
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
searchDone.setPlain(true);
|
||||||
synchronized (monitor) {
|
synchronized (monitor) {
|
||||||
monitor.notifyAll();
|
monitor.notifyAll();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
ws.send("10 " + sessionId + " 0 " + msgId + " 0 " + q + "\n");
|
ws.send("10 " + sessionId + " 0 " + msgId + " 0 " + q + "\n");
|
||||||
|
int waitInMillis = 1000;
|
||||||
|
int iterations = 0;
|
||||||
|
while (iterations < 5 && !searchDone.get()) {
|
||||||
synchronized (monitor) {
|
synchronized (monitor) {
|
||||||
monitor.wait();
|
monitor.wait(waitInMillis);
|
||||||
|
iterations++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (MyFreeCamsModel model : models.asMap().values()) {
|
for (MyFreeCamsModel model : models.asMap().values()) {
|
||||||
|
|
Loading…
Reference in New Issue