use of org.codelibs.core.timer.TimeoutTask in project fess by codelibs.
the class GenerateThumbnailJob method execute.
@Override
public String execute() {
final StringBuilder resultBuf = new StringBuilder();
if (sessionId == null) {
// create session id
sessionId = RandomStringUtils.randomAlphabetic(15);
}
resultBuf.append("Session Id: ").append(sessionId).append("\n");
if (jobExecutor != null) {
jobExecutor.addShutdownListener(() -> ComponentUtil.getProcessHelper().destroyProcess(sessionId));
}
final TimeoutTask timeoutTask = createTimeoutTask();
try {
executeThumbnailGenerator();
} catch (final Exception e) {
logger.warn("Failed to generate thumbnails.", e);
resultBuf.append(e.getMessage()).append("\n");
} finally {
if (timeoutTask != null && !timeoutTask.isCanceled()) {
timeoutTask.cancel();
}
}
return resultBuf.toString();
}
use of org.codelibs.core.timer.TimeoutTask in project fess by codelibs.
the class ScriptExecutorJob method process.
protected void process(final LaJobRuntime runtime) {
if (!runtime.getParameterMap().containsKey(Constants.SCHEDULED_JOB)) {
logger.warn("{} is empty.", Constants.SCHEDULED_JOB);
return;
}
runtime.stopIfNeeds();
final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
final JobManager jobManager = ComponentUtil.getJobManager();
final ScheduledJob scheduledJob = (ScheduledJob) runtime.getParameterMap().get(Constants.SCHEDULED_JOB);
final String id = scheduledJob.getId();
final String target = scheduledJob.getTarget();
if (!ComponentUtil.getFessConfig().isSchedulerTarget(target)) {
logger.info("Ignore Job {}:{} because of not target: {}", scheduledJob.getName(), id, scheduledJob.getTarget());
return;
}
final JobHelper jobHelper = ComponentUtil.getJobHelper();
if (!jobHelper.isAvailable(id)) {
logger.info("Job {} is unavailable. Unregistering this job.", id);
jobHelper.unregister(scheduledJob);
return;
}
final JobLog jobLog = new JobLog(scheduledJob);
final String scriptType = scheduledJob.getScriptType();
final String script = scheduledJob.getScriptData();
final JobExecutor jobExecutor = ComponentUtil.getJobExecutor(scriptType);
if (jobExecutor == null) {
throw new ScheduledJobException("No jobExecutor: " + scriptType);
}
if (!jobManager.findJobByUniqueOf(LaJobUnique.of(id)).isPresent()) {
if (logger.isDebugEnabled()) {
logger.debug("Job {} is running.", id);
}
return;
}
TimeoutTask task = null;
try {
if (scheduledJob.isLoggingEnabled()) {
jobHelper.store(jobLog);
task = jobHelper.startMonitorTask(jobLog);
}
if (logger.isDebugEnabled()) {
logger.debug("Starting Job {}. scriptType: {}, script: {}", id, scriptType, script);
} else if (scheduledJob.isLoggingEnabled() && logger.isInfoEnabled()) {
logger.info("Starting Job {}.", id);
}
final Object ret = jobExecutor.execute(Constants.DEFAULT_SCRIPT, script);
if (ret == null) {
if (scheduledJob.isLoggingEnabled() && logger.isInfoEnabled()) {
logger.info("Finished Job {}.", id);
}
} else {
if (scheduledJob.isLoggingEnabled() && logger.isInfoEnabled()) {
logger.info("Finished Job {}. The return value is:\n{}", id, ret);
}
jobLog.setScriptResult(ret.toString());
}
jobLog.setJobStatus(Constants.OK);
} catch (final Throwable t) {
logger.warn("Failed to execute {}: {}", id, script, t);
jobLog.setJobStatus(Constants.FAIL);
jobLog.setScriptResult(systemHelper.abbreviateLongText(t.getLocalizedMessage()));
} finally {
if (task != null) {
try {
task.stop();
} catch (final Exception e) {
logger.warn("Failed to stop {}", jobLog, e);
}
}
jobLog.setEndTime(ComponentUtil.getSystemHelper().getCurrentTimeAsLong());
if (logger.isDebugEnabled()) {
logger.debug("jobLog: {}", jobLog);
}
if (scheduledJob.isLoggingEnabled()) {
jobHelper.store(jobLog);
}
}
}
use of org.codelibs.core.timer.TimeoutTask in project fess by codelibs.
the class Crawler method main.
public static void main(final String[] args) {
final Options options = new Options();
final CmdLineParser parser = new CmdLineParser(options);
try {
parser.parseArgument(args);
} catch (final CmdLineException e) {
System.err.println(e.getMessage());
System.err.println("java " + Crawler.class.getCanonicalName() + " [options...] arguments...");
parser.printUsage(System.err);
return;
}
if (logger.isDebugEnabled()) {
try {
ManagementFactory.getRuntimeMXBean().getInputArguments().stream().forEach(s -> logger.debug("Parameter: {}", s));
System.getProperties().entrySet().stream().forEach(e -> logger.debug("Property: {}={}", e.getKey(), e.getValue()));
System.getenv().entrySet().forEach(e -> logger.debug("Env: {}={}", e.getKey(), e.getValue()));
logger.debug("Option: {}", options);
} catch (final Exception e) {
// ignore
}
}
initializeProbes();
final String httpAddress = System.getProperty(Constants.FESS_ES_HTTP_ADDRESS);
if (StringUtil.isNotBlank(httpAddress)) {
System.setProperty(FesenClient.HTTP_ADDRESS, httpAddress);
}
TimeoutTask systemMonitorTask = null;
Thread commandThread = null;
int exitCode;
try {
running.set(true);
SingletonLaContainerFactory.setConfigPath("app.xml");
SingletonLaContainerFactory.setExternalContext(new GenericExternalContext());
SingletonLaContainerFactory.setExternalContextComponentDefRegister(new GenericExternalContextComponentDefRegister());
SingletonLaContainerFactory.init();
final Thread shutdownCallback = new Thread("ShutdownHook") {
@Override
public void run() {
destroyContainer();
}
};
Runtime.getRuntime().addShutdownHook(shutdownCallback);
commandThread = new Thread(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
String command;
while (true) {
try {
while (!reader.ready()) {
ThreadUtil.sleep(1000L);
}
command = reader.readLine().trim();
if (logger.isDebugEnabled()) {
logger.debug("Process command: {}", command);
}
if (Constants.CRAWLER_PROCESS_COMMAND_THREAD_DUMP.equals(command)) {
ThreadDumpUtil.printThreadDump();
} else {
logger.warn("Unknown process command: {}", command);
}
if (Thread.interrupted()) {
return;
}
} catch (final InterruptedRuntimeException e) {
return;
}
}
} catch (final IOException e) {
logger.debug("I/O exception.", e);
}
}, "ProcessCommand");
commandThread.start();
systemMonitorTask = TimeoutManager.getInstance().addTimeoutTarget(new SystemMonitorTarget(), ComponentUtil.getFessConfig().getCrawlerSystemMonitorIntervalAsInteger(), true);
exitCode = process(options);
} catch (final ContainerNotAvailableException e) {
if (logger.isDebugEnabled()) {
logger.debug("Crawler is stopped.", e);
} else if (logger.isInfoEnabled()) {
logger.info("Crawler is stopped.");
}
exitCode = Constants.EXIT_FAIL;
} catch (final Throwable t) {
logger.error("Crawler does not work correctly.", t);
exitCode = Constants.EXIT_FAIL;
} finally {
if (commandThread != null && commandThread.isAlive()) {
commandThread.interrupt();
}
if (systemMonitorTask != null) {
systemMonitorTask.cancel();
}
destroyContainer();
}
if (exitCode != Constants.EXIT_OK) {
System.exit(exitCode);
}
}
Aggregations