<commit_before>/*
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2018 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.quartz;

import com.axelor.app.AppSettings;
import com.axelor.db.Query;
import com.axelor.i18n.I18n;
import com.axelor.meta.db.MetaSchedule;
import com.axelor.meta.db.MetaScheduleParam;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The {@link JobRunner} controls the scheduler.<br>
 * <br>
 * It configures the {@link Scheduler} from the job configuration provided from the database. It
 * also provides some public methods to start/restart/stop the scheduler.
 */
@Singleton
public class JobRunner {

  private static Logger log = LoggerFactory.getLogger(JobRunner.class);
  private static final String CONFIG_QUARTZ_ENABLE = "quartz.enable";

  private Scheduler scheduler;

  private int total;

  @Inject
  public JobRunner(Scheduler scheduler) {
    this.scheduler = scheduler;
  }

  public boolean isEnabled() {
    return AppSettings.get().getBoolean(CONFIG_QUARTZ_ENABLE, false);
  }

  private void configure() {
    if (total > 0) {
      return;
    }
    total = 0;
    log.info("Configuring scheduled jobs...");
    for (MetaSchedule meta : Query.of(MetaSchedule.class).fetch()) {
      configure(meta);
    }
    log.info("Configured total jobs: {}", total);
  }

  private void configure(MetaSchedule meta) {

    if (meta == null || meta.getActive() != Boolean.TRUE) {
      return;
    }

    final String name = meta.getName();
    final String cron = meta.getCron();
    final String jobClass = meta.getJob();

    log.info("Configuring job: {}, {}", name, cron);
    Class<?> klass;
    try {<commit_msg>Fix idle in transaction when using quartz<commit_after>/*
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2018 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.quartz;

import com.axelor.app.AppSettings;
import com.axelor.db.JPA;
import com.axelor.i18n.I18n;
import com.axelor.meta.db.MetaSchedule;
import com.axelor.meta.db.MetaScheduleParam;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The {@link JobRunner} controls the scheduler.<br>
 * <br>
 * It configures the {@link Scheduler} from the job configuration provided from the database. It
 * also provides some public methods to start/restart/stop the scheduler.
 */
@Singleton
public class JobRunner {

  private static Logger log = LoggerFactory.getLogger(JobRunner.class);
  private static final String CONFIG_QUARTZ_ENABLE = "quartz.enable";
  private static final String META_SCHEDULE_QUERY =
      "SELECT DISTINCT self FROM MetaSchedule self LEFT JOIN FETCH self.params";

  private Scheduler scheduler;

  private int total;

  @Inject
  public JobRunner(Scheduler scheduler) {
    this.scheduler = scheduler;
  }

  public boolean isEnabled() {
    return AppSettings.get().getBoolean(CONFIG_QUARTZ_ENABLE, false);
  }

  private void configure() {
    if (total > 0) {
      return;
    }
    total = 0;
    log.info("Configuring scheduled jobs...");

    JPA.em()
        .createQuery(META_SCHEDULE_QUERY, MetaSchedule.class)
        .getResultList()
        .stream()
        .forEach(this::configure);

    log.info("Configured total jobs: {}", total);
  }

  private void configure(MetaSchedule meta) {

    if (meta == null || meta.getActive() != Boolean.TRUE) {
      return;
    }

    final String name = meta.getName();
    final String cron = meta.getCron();
    final String jobClass = meta.getJob();

    log.info("Configuring job: {}, {}", name, cron);
    Class<?> klass;
    try {
{'fraction_non_alphanumeric': 0.1030058651026393, 'fraction_numerical': 0.006231671554252199, 'mean_word_length': 3.807929515418502, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2889249', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1608, 'n_words': 524}