Search in sources :

Example 1 with AppenderProvider

use of org.ow2.proactive.scheduler.common.util.logforwarder.AppenderProvider in project scheduling by ow2-proactive.

the class SchedulerStateRestLiveLogsTest method verifyListenAndGetAppender.

private Appender verifyListenAndGetAppender(String jobId) throws NotConnectedException, UnknownJobException, PermissionException, LogForwardingException {
    ArgumentCaptor<AppenderProvider> appenderProviderArgumentCaptor = ArgumentCaptor.forClass(AppenderProvider.class);
    verify(scheduler).listenJobLogs(eq(jobId), appenderProviderArgumentCaptor.capture());
    AppenderProvider appenderProvider = appenderProviderArgumentCaptor.getValue();
    return appenderProvider.getAppender();
}
Also used : AppenderProvider(org.ow2.proactive.scheduler.common.util.logforwarder.AppenderProvider)

Example 2 with AppenderProvider

use of org.ow2.proactive.scheduler.common.util.logforwarder.AppenderProvider in project scheduling by ow2-proactive.

the class TaskLogger method activateLogs.

public void activateLogs(AppenderProvider logSink) {
    logger.info("Activating logs for task " + this.taskId + " (" + taskId.getReadableName() + ")");
    if (this.loggersActivated.get()) {
        logger.info("Logs for task " + this.taskId + " are already activated");
        return;
    }
    this.loggersActivated.set(true);
    // create appender
    Appender appender;
    try {
        appender = logSink.getAppender();
    } catch (LogForwardingException e) {
        logger.error("Cannot create log appender.", e);
        return;
    }
    // fill appender
    if (!this.loggersFinalized.get()) {
        taskLogAppender.addAppender(appender);
    } else {
        logger.info("Logs for task " + this.taskId + " are closed. Flushing buffer...");
        // Everything is closed: reopen and close...
        for (LoggingEvent e : taskLogAppender.getStorage()) {
            appender.doAppend(e);
        }
        appender.close();
        this.loggersActivated.set(false);
        return;
    }
    logger.info("Activated logs for task " + this.taskId);
}
Also used : Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) LoggingEvent(org.apache.log4j.spi.LoggingEvent) LogForwardingException(org.ow2.proactive.scheduler.common.util.logforwarder.LogForwardingException)

Example 3 with AppenderProvider

use of org.ow2.proactive.scheduler.common.util.logforwarder.AppenderProvider in project scheduling by ow2-proactive.

the class TaskLoggerTest method getStoredLogs.

@Test
public void getStoredLogs() throws Exception {
    taskLogger = new TaskLogger(TaskIdImpl.createTaskId(new JobIdImpl(1000, "job"), "task", 42L), "myhost");
    final StringWriter stringAppender = new StringWriter();
    AppenderProvider stringAppenderProvider = new AppenderProvider() {

        @Override
        public Appender getAppender() throws LogForwardingException {
            return new WriterAppender(new PatternLayout("%m%n"), stringAppender);
        }
    };
    taskLogger.getStoredLogs(stringAppenderProvider);
    assertEquals("", stringAppender.toString());
    taskLogger.getOutputSink().println("hello");
    taskLogger.getStoredLogs(stringAppenderProvider);
    assertEquals(String.format("hello%n"), stringAppender.toString());
}
Also used : StringWriter(java.io.StringWriter) AppenderProvider(org.ow2.proactive.scheduler.common.util.logforwarder.AppenderProvider) PatternLayout(org.apache.log4j.PatternLayout) JobIdImpl(org.ow2.proactive.scheduler.job.JobIdImpl) WriterAppender(org.apache.log4j.WriterAppender) Test(org.junit.Test)

Example 4 with AppenderProvider

use of org.ow2.proactive.scheduler.common.util.logforwarder.AppenderProvider in project scheduling by ow2-proactive.

the class TaskLoggerTest method logStreaming.

@Test
public void logStreaming() throws Exception {
    taskLogger = new TaskLogger(TaskIdImpl.createTaskId(new JobIdImpl(1000, "job"), "task", 42L), "myhost");
    final StringWriter stringAppender = new StringWriter();
    AppenderProvider stringAppenderProvider = new AppenderProvider() {

        @Override
        public Appender getAppender() throws LogForwardingException {
            return new WriterAppender(new PatternLayout("%m%n"), stringAppender);
        }
    };
    taskLogger.activateLogs(stringAppenderProvider);
    assertEquals("", stringAppender.toString());
    taskLogger.getOutputSink().println("hello");
    // async appender, it gets buffered
    assertEquals("", stringAppender.toString());
    for (int i = 0; i < 1000; i++) {
        taskLogger.getOutputSink().println("hello");
    }
    assertNotEquals("", stringAppender.toString());
}
Also used : StringWriter(java.io.StringWriter) AppenderProvider(org.ow2.proactive.scheduler.common.util.logforwarder.AppenderProvider) PatternLayout(org.apache.log4j.PatternLayout) JobIdImpl(org.ow2.proactive.scheduler.job.JobIdImpl) WriterAppender(org.apache.log4j.WriterAppender) Test(org.junit.Test)

Example 5 with AppenderProvider

use of org.ow2.proactive.scheduler.common.util.logforwarder.AppenderProvider in project scheduling by ow2-proactive.

the class EnabledListenJobLogsSupport method listenJobLogs.

@Override
public synchronized void listenJobLogs(JobId jobId, AppenderProvider appenderProvider) throws UnknownJobException {
    jlogger.info(jobId, "listening logs");
    // create the appender to the remote listener
    Appender clientAppender = null;
    try {
        clientAppender = appenderProvider.getAppender();
    } catch (LogForwardingException e) {
        jlogger.error(jobId, "cannot create an appender", e);
        throw new InternalException("Cannot create an appender for job " + jobId, e);
    }
    boolean logIsAlreadyInitialized = jobsToBeLogged.contains(jobId);
    initJobLogging(jobId, clientAppender);
    JobResult result = dbManager.loadJobResult(jobId);
    if (result == null) {
        throw new UnknownJobException(jobId);
    }
    // for finished tasks, add logs events "manually"
    Collection<TaskResult> allRes = result.getAllResults().values();
    for (TaskResult tr : allRes) {
        this.flushTaskLogs(tr, clientAppender, jobId);
    }
    for (RunningTaskData taskData : liveJobs.getRunningTasks(jobId)) {
        jlogger.debug(jobId, "Handling log initialization for task " + taskData.getTask().getName());
        try {
            TaskLauncher taskLauncher = taskData.getLauncher();
            if (logIsAlreadyInitialized) {
                jlogger.debug(jobId, "Call getStoredLogs");
                taskLauncher.getStoredLogs(appenderProvider);
            } else {
                jlogger.debug(jobId, "Call activateLogs");
                taskLauncher.activateLogs(lfs.getAppenderProvider());
            }
        } catch (Exception e) {
            tlogger.error(taskData.getTask().getId(), "cannot create an appender provider", e);
        }
    }
    if (!result.getJobInfo().getStatus().isJobAlive()) {
        jlogger.info(jobId, "cleaning loggers for already finished job");
        cleanLoggers(jobId);
    }
}
Also used : Appender(org.apache.log4j.Appender) JobResult(org.ow2.proactive.scheduler.common.job.JobResult) UnknownJobException(org.ow2.proactive.scheduler.common.exception.UnknownJobException) TaskLauncher(org.ow2.proactive.scheduler.task.TaskLauncher) TaskResult(org.ow2.proactive.scheduler.common.task.TaskResult) LogForwardingException(org.ow2.proactive.scheduler.common.util.logforwarder.LogForwardingException) LogForwardingException(org.ow2.proactive.scheduler.common.util.logforwarder.LogForwardingException) UnknownJobException(org.ow2.proactive.scheduler.common.exception.UnknownJobException) InternalException(org.ow2.proactive.scheduler.common.exception.InternalException) InternalException(org.ow2.proactive.scheduler.common.exception.InternalException)

Aggregations

Appender (org.apache.log4j.Appender)3 AppenderProvider (org.ow2.proactive.scheduler.common.util.logforwarder.AppenderProvider)3 LogForwardingException (org.ow2.proactive.scheduler.common.util.logforwarder.LogForwardingException)3 StringWriter (java.io.StringWriter)2 FileAppender (org.apache.log4j.FileAppender)2 PatternLayout (org.apache.log4j.PatternLayout)2 WriterAppender (org.apache.log4j.WriterAppender)2 Test (org.junit.Test)2 JobIdImpl (org.ow2.proactive.scheduler.job.JobIdImpl)2 LoggingEvent (org.apache.log4j.spi.LoggingEvent)1 InternalException (org.ow2.proactive.scheduler.common.exception.InternalException)1 UnknownJobException (org.ow2.proactive.scheduler.common.exception.UnknownJobException)1 JobResult (org.ow2.proactive.scheduler.common.job.JobResult)1 TaskResult (org.ow2.proactive.scheduler.common.task.TaskResult)1 TaskLauncher (org.ow2.proactive.scheduler.task.TaskLauncher)1