<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 java.io.File
import java.io.OutputStream
import java.io.PrintStream
import java.net.URI
import java.util.UUID
import org.openmole.core.batch.control._
import org.openmole.core.batch.storage.{ StorageService, Storage }
import org.openmole.core.batch.environment.SerializedJob
import org.openmole.misc.exception.InternalProcessingError
import org.openmole.core.batch.environment.Runtime
import org.openmole.misc.tools.service.Logger
import org.openmole.misc.workspace.ConfigurationLocation
import org.openmole.misc.workspace.Workspace
import org.openmole.core.batch.control.AccessToken
import org.openmole.misc.tools.io.FileUtil._
import org.openmole.plugin.environment.gridscale.GridScaleJobService
import fr.iscpif.gridscale.glite.{ WMSJobService, WMSJobDescription }
import scala.collection.JavaConversions._
import scala.io.Source
import org.openmole.misc.tools.service.Duration._
import StatusFiles._
import scalax.io.Resource

object GliteJobService extends Logger

trait GliteJobService extends GridScaleJobService with JobServiceQualityControl with LimitedAccess with AvailabitityQuality with JobScript { js ⇒

  val jobService: WMSJobService
  def environment: GliteEnvironment

  def authentication = environment.authentication

  lazy val id = jobService.url.toString
  def hysteresis = Workspace.preferenceAsInt(GliteEnvironment.QualityHysteresis)

  var delegated = false

  def delegated[T](f: ⇒ T): T = synchronized {
    if (!delegated) {
      jobService.delegate(authentication)
      delegated = true
    }
    f
  }

  override protected def _purge(j: J) = quality { delegated { super._purge(j) } }

  override protected def _cancel(j: J) = quality { delegated { super._cancel(j) } }

  override protected def _state(j: J) = quality { delegated { super._state(j) } }

  override protected def _submit(serializedJob: SerializedJob) = quality {
    delegated {
      import serializedJob._
      import GliteJobService.Log._

      val script = Workspace.newFile("script", ".sh")
      try {
        val outputFilePath = storage.child(path, Storage.uniqName("job", ".out"))
        val _runningPath = storage.child(path, runningFile)
        val _finishedPath = storage.child(path, finishedFile)

        val scriptContent = generateScript(serializedJob, outputFilePath, Some(_runningPath), Some(_finishedPath))

        Resource.fromFile(script).write(scriptContent)

        val jobDescription = buildJobDescription(script)

        logger.fine(s"""Submitting job: ${jobDescription.toJDL} with script $scriptContent" """)

        val jid = jobService.submit(jobDescription)(authentication)

        logger.fine(s"""Job successfully submitted with job service ${jobService.url} and with id ${jid.id}""")

        new GliteJob {
          val jobService = js
          val storage = serializedJob.storage
          val finishedPath = _finishedPath
          val runningPath = _runningPath
          val id = jid
          val resultPath = outputFilePath
        }
      }
      finally script.delete
    }
  }

  protected def buildJobDescription(script: File) =
    new WMSJobDescription {<commit_msg>Compilation fix.<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 java.io.File
import java.io.OutputStream
import java.io.PrintStream
import java.net.URI
import java.util.UUID
import org.openmole.core.batch.control._
import org.openmole.core.batch.storage.{ StorageService, Storage }
import org.openmole.core.batch.environment.SerializedJob
import org.openmole.misc.exception.InternalProcessingError
import org.openmole.core.batch.environment.Runtime
import org.openmole.misc.tools.service.Logger
import org.openmole.misc.workspace.ConfigurationLocation
import org.openmole.misc.workspace.Workspace
import org.openmole.core.batch.control.AccessToken
import org.openmole.misc.tools.io.FileUtil._
import org.openmole.plugin.environment.gridscale.GridScaleJobService
import fr.iscpif.gridscale.glite.{ WMSJobService, WMSJobDescription }
import scala.collection.JavaConversions._
import scala.io.Source
import org.openmole.misc.tools.service.Duration._
import StatusFiles._
import scalax.io.Resource

object GliteJobService extends Logger

trait GliteJobService extends GridScaleJobService with JobServiceQualityControl with LimitedAccess with AvailabitityQuality with JobScript { js ⇒

  val jobService: WMSJobService
  def environment: GliteEnvironment

  def authentication = environment.authentication

  lazy val id = jobService.url.toString
  def hysteresis = Workspace.preferenceAsInt(GliteEnvironment.QualityHysteresis)

  var delegated = false

  def delegated[T](f: ⇒ T): T = synchronized {
    if (!delegated) {
      jobService.delegate(authentication)
      delegated = true
    }
    f
  }

  override protected def _purge(j: J) = quality { delegated { super._purge(j) } }

  override protected def _cancel(j: J) = quality { delegated { super._cancel(j) } }

  override protected def _state(j: J) = quality { delegated { super._state(j) } }

  override protected def _submit(serializedJob: SerializedJob) = quality {
    delegated {
      import serializedJob._
      import GliteJobService.Log._

      val script = Workspace.newFile("script", ".sh")
      try {
        val outputFilePath = storage.child(path, Storage.uniqName("job", ".out"))
        val _runningPath = storage.child(path, runningFile)
        val _finishedPath = storage.child(path, finishedFile)

        val scriptContent = generateScript(serializedJob, outputFilePath, Some(_runningPath), Some(_finishedPath))

        Resource.fromFile(script).write(scriptContent)

        val jobDescription = buildJobDescription(script)

        logger.fine(s"""Submitting job: ${jobDescription.toJDL} with script $scriptContent" """)

        val jid = jobService.submit(jobDescription)(authentication)

        logger.fine(s"""Job successfully submitted with job service ${jobService.url} and with id ${jid.id}""")

        new GliteJob {
          val jobService = js
          val storage = serializedJob.storage
          val finishedPath = _finishedPath
          val runningPath = _runningPath
          val id = jid
          val resultPath = outputFilePath
          val description = jobDescription
        }
      }
      finally script.delete
    }
  }

  protected def buildJobDescription(script: File) =
    new WMSJobDescription {
{'fraction_non_alphanumeric': 0.08346094946401225, 'fraction_numerical': 0.001276161306789178, 'mean_word_length': 3.8526315789473684, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1122994', 'n_tokens_mistral': 2210, 'n_tokens_neox': 2149, 'n_words': 638}