<commit_before>/*
 * Copyright (C) 2010 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 org.openmole.plugin.environment.glite

import org.openmole.misc.exception.InternalProcessingError
import org.openmole.misc.workspace.Workspace
import org.openmole.plugin.environment.jsaga.JSAGAJob
import java.io.File
import org.openmole.core.batch.environment.ShouldBeKilledException
import org.openmole.core.model.execution.ExecutionState._
import fr.in2p3.jsaga.adaptor.job.SubState
import org.openmole.core.model.execution.ExecutionState
import scala.io.Source

class GliteJob(
    val jobId: String,
    resultPath: String,
    jobService: GliteJobService,
    proxyExpired: Long) extends JSAGAJob(resultPath, jobService) {

  var lastChaked = System.currentTimeMillis

  override def updatedState: ExecutionState = {

    val (state, subState) = super.updatedStateAndSubState

    if (!state.isFinal && proxyExpired < System.currentTimeMillis) throw new InternalProcessingError("Proxy for this job has expired.")

    if (state == SUBMITTED) {
      val maxNbReady = Workspace.preferenceAsInt(GliteEnvironment.JobShakingMaxReady)

      def nbReady = jobService.environment.jobRegistry.allExecutionJobs.count(_.state == READY)

      if (nbReady < maxNbReady) {
        lastChaked = System.currentTimeMillis

        val jobShakingAverageTime = Workspace.preferenceAsDurationInMs(GliteEnvironment.JobShakingHalfLife)
        val nbInterval = ((System.currentTimeMillis - lastChaked.toDouble) / jobShakingAverageTime)
        val probability = 1 - math.pow(0.5, nbInterval)

        if (Workspace.rng.nextDouble < probability) throw new ShouldBeKilledException("Killed in shaking process")
      }
    }

    state
  }

}<commit_msg>Fix job shaking.<commit_after>/*
 * Copyright (C) 2010 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * 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 org.openmole.plugin.environment.glite

import org.openmole.misc.exception.InternalProcessingError
import org.openmole.misc.workspace.Workspace
import org.openmole.plugin.environment.jsaga.JSAGAJob
import java.io.File
import org.openmole.core.batch.environment.ShouldBeKilledException
import org.openmole.core.model.execution.ExecutionState._
import fr.in2p3.jsaga.adaptor.job.SubState
import org.openmole.core.model.execution.ExecutionState
import scala.io.Source

class GliteJob(
    val jobId: String,
    resultPath: String,
    jobService: GliteJobService,
    proxyExpired: Long) extends JSAGAJob(resultPath, jobService) {

  var lastShaked = System.currentTimeMillis

  override def updatedState: ExecutionState = {

    val (state, subState) = super.updatedStateAndSubState

    if (!state.isFinal && proxyExpired < System.currentTimeMillis) throw new InternalProcessingError("Proxy for this job has expired.")

    if (state == SUBMITTED) {
      val maxNbReady = Workspace.preferenceAsInt(GliteEnvironment.JobShakingMaxReady)

      def nbReady = jobService.environment.jobRegistry.allExecutionJobs.count(_.state == READY)

      if (nbReady < maxNbReady) {

        val jobShakingAverageTime = Workspace.preferenceAsDurationInMs(GliteEnvironment.JobShakingHalfLife)
        val nbInterval = ((System.currentTimeMillis - lastShaked.toDouble) / jobShakingAverageTime)
        val probability = 1 - math.pow(0.5, nbInterval)

        lastShaked = System.currentTimeMillis
        
        if (Workspace.rng.nextDouble < probability) throw new ShouldBeKilledException("Killed in shaking process")
      }
    }

    state
  }

}
{'fraction_non_alphanumeric': 0.07859249894648125, 'fraction_numerical': 0.004214075010535188, 'mean_word_length': 4.098818474758325, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, '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': '4050953', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1341, 'n_words': 406}