<commit_before>package eu.scape_project.watch.scheduling.quartz;

import java.util.HashMap;
import java.util.Map;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eu.scape_project.watch.domain.SourceAdaptorEvent;
import eu.scape_project.watch.domain.SourceAdaptorEventType;
import eu.scape_project.watch.interfaces.AdaptorPluginInterface;
import eu.scape_project.watch.utils.exceptions.PluginException;

public class QuartzExecutionListener implements JobListener {

  private static final Logger LOG = LoggerFactory.getLogger(QuartzExecutionListener.class);

  private int REPEAT = 5;

  private QuartzScheduler scheduler;

  private QuartzListenerManager listenerManager;

  private String name = "QuartzExecutionListener";

  private Map<AdaptorPluginInterface, Integer> failed;

  public QuartzExecutionListener() {
    failed = new HashMap<AdaptorPluginInterface, Integer>();
  }

  public QuartzExecutionListener(QuartzScheduler sc) {
    scheduler = sc;
  }

  public void setScheduler(QuartzScheduler sc) {
    scheduler = sc;
  }

  public void setListenerManager(QuartzListenerManager lm) {
    listenerManager = lm;
  }

  @Override
  public String getName() {
    return name;
  }

  @Override
  public void jobToBeExecuted(JobExecutionContext context) {
    QuartzAdaptorJob job = (QuartzAdaptorJob) context.getJobInstance();
    job.setScheduler(scheduler);
    job.setlManager(listenerManager);
  }

  @Override
  public void jobExecutionVetoed(JobExecutionContext context) {

  }

  @Override
  public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
    QuartzAdaptorJob job = (QuartzAdaptorJob) context.getJobInstance();
    AdaptorPluginInterface adaptor = job.getAdaptorPlugin();
    Boolean result = (Boolean) context.getResult();
    if (result != null && result.booleanValue()) {
      failed.remove(adaptor);
      LOG.info(adaptor.getName() + " was successfully executed");
      SourceAdaptorEvent event = new SourceAdaptorEvent();
      event.setType(SourceAdaptorEventType.EXECUTED);
      event.setSuccessful(true);
      event.setMessage(adaptor.getName() + " was successfully executed");
      scheduler.notifyEvent(adaptor, event);
    } else {
      PluginException e = (PluginException) context.get("exception");
      LOG.warn(adaptor.getName() + " was not successfully executed. An exception happened: " + e);
      SourceAdaptorEvent event = new SourceAdaptorEvent();
      event.setType(SourceAdaptorEventType.EXECUTED);
      event.setSuccessful(false);
      event.setMessage(adaptor.getName() + " was not successfully executed");
      event.setReason("An exception happened: " + e);
      scheduler.notifyEvent(adaptor, event);
      int num;
      if (failed.containsKey(adaptor)) {
        Integer i = failed.get(adaptor);
        i = i + 1;
        num = i.intValue();
        failed.put(adaptor, i);
      } else {
        failed.put(adaptor, Integer.valueOf(1));
        num = 1;
      }
      if (num > REPEAT) {
        LOG.warn("Unscheduling adaptor: " + adaptor.getName());
        SourceAdaptorEvent event2 = new SourceAdaptorEvent();
        event2.setReason("Adaptor failed 5 times in a row");
        scheduler.stop(adaptor, event2);
        failed.remove(adaptor);<commit_msg>exception stack trace is now sent to listeners fixes #126<commit_after>package eu.scape_project.watch.scheduling.quartz;

import java.util.HashMap;
import java.util.Map;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eu.scape_project.watch.domain.SourceAdaptorEvent;
import eu.scape_project.watch.domain.SourceAdaptorEventType;
import eu.scape_project.watch.interfaces.AdaptorPluginInterface;
import eu.scape_project.watch.utils.exceptions.PluginException;

public class QuartzExecutionListener implements JobListener {

  private static final Logger LOG = LoggerFactory.getLogger(QuartzExecutionListener.class);

  private int REPEAT = 5;

  private QuartzScheduler scheduler;

  private QuartzListenerManager listenerManager;

  private String name = "QuartzExecutionListener";

  private Map<AdaptorPluginInterface, Integer> failed;

  public QuartzExecutionListener() {
    failed = new HashMap<AdaptorPluginInterface, Integer>();
  }

  public QuartzExecutionListener(QuartzScheduler sc) {
    scheduler = sc;
  }

  public void setScheduler(QuartzScheduler sc) {
    scheduler = sc;
  }

  public void setListenerManager(QuartzListenerManager lm) {
    listenerManager = lm;
  }

  @Override
  public String getName() {
    return name;
  }

  @Override
  public void jobToBeExecuted(JobExecutionContext context) {
    QuartzAdaptorJob job = (QuartzAdaptorJob) context.getJobInstance();
    job.setScheduler(scheduler);
    job.setlManager(listenerManager);
  }

  @Override
  public void jobExecutionVetoed(JobExecutionContext context) {

  }

  @Override
  public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
    QuartzAdaptorJob job = (QuartzAdaptorJob) context.getJobInstance();
    AdaptorPluginInterface adaptor = job.getAdaptorPlugin();
    Boolean result = (Boolean) context.getResult();
    if (result != null && result.booleanValue()) {
      failed.remove(adaptor);
      LOG.info(adaptor.getName() + " was successfully executed");
      SourceAdaptorEvent event = new SourceAdaptorEvent();
      event.setType(SourceAdaptorEventType.EXECUTED);
      event.setSuccessful(true);
      event.setMessage(adaptor.getName() + " was successfully executed");
      scheduler.notifyEvent(adaptor, event);
    } else {
      PluginException e = (PluginException) context.get("exception");
      LOG.warn(adaptor.getName() + " was not successfully executed. An exception happened: " + e.getStackTrace());
      SourceAdaptorEvent event = new SourceAdaptorEvent();
      event.setType(SourceAdaptorEventType.EXECUTED);
      event.setSuccessful(false);
      event.setMessage(adaptor.getName() + " was not successfully executed");
      event.setReason("An exception happened: " + e.getStackTrace());
      scheduler.notifyEvent(adaptor, event);
      int num;
      if (failed.containsKey(adaptor)) {
        Integer i = failed.get(adaptor);
        i = i + 1;
        num = i.intValue();
        failed.put(adaptor, i);
      } else {
        failed.put(adaptor, Integer.valueOf(1));
        num = 1;
      }
      if (num > REPEAT) {
        LOG.warn("Unscheduling adaptor: " + adaptor.getName());
        SourceAdaptorEvent event2 = new SourceAdaptorEvent();
        event2.setReason("Adaptor failed 5 times in a row");
        scheduler.stop(adaptor, event2);
        failed.remove(adaptor);
{'fraction_non_alphanumeric': 0.09909778812572759, 'fraction_numerical': 0.003346915017462165, 'mean_word_length': 4.076070901033973, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7155003', 'n_tokens_mistral': 2086, 'n_tokens_neox': 2004, 'n_words': 422}