Joblet

Defines the attributes for creating a virtual machine. An instance of this class is passed to resource.createInstance(), resource.createTemplate(), resource.clone().

Description

The job writer invokes the schedule() function in the job subclass to define when, where and which resource the joblet is executed.

Each joblet instance has the job instance (jobinstance.*,job.*,jobargs.*,user.*), Resource (resource.*) and joblet (joblet.*) fact sets available using the base GridObjectInfo fact functions.

Interaction with the job and the client is accomplished using event methods on the joblet class. If a ParameterSpace has been defined for this joblet, the self.getParamterSpace() method retrieves the joblet's JobletParameterSpace.

Fields

Methods

Summary:

cancel()

Cancels this joblet if it is waiting or running. Has no effect if joblet is finished.

cancel(String reason)

Cancel this joblet if it is running. Has no effect if joblet is finished.

Parameters: reason - String to store in the job log.

fail()

Fail a running joblet to force failover to another resource. If joblet has reached retry limit, then joblet ends in failed state. Has no effect if joblet is finished.

fail(String reason)

Fail a running joblet to force failover to another resource. If joblet has reached retry limit, then joblet ends in failed state. Has no effect if joblet is finished.

Parameters: reason - String to store in the job log.

getcwd()

Retrieves the path of the current working directory in which this joblet is running.

Returns: Path of joblet’s current working directory.

getParameterSpace()

Retrieves the JobletParameterSpace defined for this joblet instance.

Returns: JobletParameterSpace for this joblet. None if no ParameterSpace is defined.

isValidUser(String username)

Test if a supplied username is valid on a resource. If true is returned, then the username can run operations as that user on the resource. This includes running execs (assuming any required credentials are supplied) and running file operations on the resource. If enhanced exec is not installed on the resource, this returns False.

Parameters: username - Name of user to test.

Returns: true if user is valid on this resource, False if not.

joblet_cancelled_event()

The joblet_failed_event is fired when joblet execution has failed either by code, runtime errors or by the self.fail() function. The job writer can optionally implement this function to handle special postprocessing such as cleanup of a running process.

Examples: Sends a signal to a running process before joblet is ended:

 class MyJoblet(Joblet): 
      def joblet_started_event(self): 
          self.exec = Exec() 
          self.exec.setCommand("sleep 100")
          self.exec.execute()

      def joblet_failed_event(self):
          self.exec.signal("SIGABRT")
joblet_pre_cancel_event()

The joblet_before_cancel_event is fired when Joblet execution is about to be interrupted. The job writer can optionally implement this function to handle special preprocessing before cancellation such as sending a signal to a running process.

Example: Sends a signal to a running process before joblet is cancelled:

 class MyJoblet(Joblet):
      def joblet_started_event(self):
          self.exec = Exec()
          self.exec.setCommand("sleep 100")
          self.exec.execute()
      def joblet_pre_cancel_event(self):
          self.exec.signal("SIGABRT")
joblet_started_event()

joblet_started_event is fired when Joblet execution is started on a resource. The job writer is required to implement this function.

retry()

Retry this joblet elsewhere if it is running. Has no effect if joblet is finished.

retry(String reason)

Retry this joblet if it is running. Has no effect if joblet is finished.

Parameters: reason - string to store in job log.

sendClientEvent(String name, PyDictionary paramst)

Send an event to a client. The sends an event directly to a client application listening for events using the Toolkit.

Parameters: name - Name of event. params - Dictionary of event parameters

sendEvent(String name, PyDictionary params)

Send an event to the job that scheduled this joblet.

Parameters: name - Name of event. params - Dictionary of event parameters.

Examples: Sends an event with the parameters dictionary containing two elements:

      self.sendEvent("custom_event", { "arg1":1,"arg2":"foo" } )
sendEventString jobID, String name, PyDictionary params)

Sends an event to the supplied job instance. Use this method for sending events to a job different from the job that scheduled this joblet..

Returns: jobID - ID of job to send event to. name - Name of event params - Dictionary of event parameters

terminate()

Terminate a joblet that is running. This joblet will end execution upon exit of the last running joblet event. This method is necessary to end the joblet execution if the fact joblet.autoterminate is true.

Inherited from class class com.gridion.jdl.GridObjectInfo:

deleteFact, factExists, getFact, getFactLastModified, getFactNames, refresh, setArrayFact, setBooleanArrayFact, setDateArrayFact, setDateFact, setFact, setIntegerArrayFact, setRealArrayFact, setStringArrayFact, setTimeArrayFact, setTimeFact

See GridObjectInfo.

Examples

Example of a joblet that runs a command line and forces a joblet failure if the command line returns an exit code greater than zero

      class MyJoblet(Joblet):
          def joblet_started_event(self):
              e = Exec()
              e.setCommand("ls -la")
              rslt = e.execute()
              if rslt > 0:
                 self.fail("ls command failed")

In this example, the joblet may be retried on another resource depending on the value of the fact job.joblet.maxfailures and how many times the joblet has already been retried.

See Also