Job

The Job class represents a running job instance. This class defines functions for interacting with the server including handling notification of job state transitions, child job submission, managing joblets and for receiving and sending events from resources and from clients. A job writer defines a subclass of the Job class and uses the methods available on the Job class for scheduling joblets and event processing.

Examples

The following example job schedules a single joblet to run on one resource:

 class Simple(Job):
      def job_started_event(self):
          self.schedule(SimpleJoblet)

 class SimpleJoblet(Joblet):
      def joblet_started_event(self):
          print "Hello from Joblet"

For this example, the class Simple is instantiated on the server when a job is run either by client tools or by the job scheduler. When a job transitions to the started state, the method job_started_event is invoked. Here the job_started_event invokes the base class method schedule() to create a single joblet and schedule the joblet to run on a resource. The SimpleJoblet class is instantiated and run on a resource.

Job Events

Each job has a set of events that are invoked at the state transistions of a job. For example, on the starting state of a job, the job_started_event is always invoked. The following events that might be associated with jobs:

Job State Transition Events

Following is a list of job events that are invoked upon job state transitions:

      job_started_event
      job_completed_event
      job_cancelled_event
      job_failed_event
      job_paused_event
      job_resumed_event

Child Job State Transition Events

The following lists job events that are invoked upon child job state transitions:

      child_job_started_event
      child_job_completed_event
      child_job_cancelled_event
      child_job_failed_event

Job State Transition Provisioner Events

The following lists provisioner events that are invoked upon provisioner state transitions:

      provisioner_completed_event
      provisioner_cancelled_event
      provisioner_failed_event

Joblet State Transition Events

The following lists joblet events that are invoked upon joblet state transitions:

      joblet_started_event
      joblet_completed_event
      joblet_failed_event
      joblet_cancelled_event
      joblet_retry_event

NOTE:In each of the events listed above, only job_started_event event is required; the remainder are optional.

Handling Custom Job Events

A job writer can also handle and invoke custom events within a job. Events can come from clients, other jobs, and from joblets.

Example of defining an event handler named mycustom_event in a job:

 class Simple(Job):
      def job_started_event(self):
          ...

      def mycustom_event(self,params):
          dir = params["directory_to_list"]
          self.schedule(MyJoblet,{ "dir" : dir } 

For this example, the event retrieves a element from the params dictionary that is supplied to every custom event. The dictionary is optionally filled by the caller of the event.

Example of invoking the custom event named 'mycustom_event' from the zos client command line tool:

      zos event mycustom_event directory_to_list="/tmp" 

In this example, a message is sent from the client tool to the job running on the server.

Example of invoking the same custom event from a joblet:

 class SimpleJoblet(Joblet):
      def joblet_started_event(self):          ...
          self.sendEvent("mycustom_event", { ... } ) 

In the example above, a message is sent from the joblet running on a resource to the job running on the server. The running job has access to a factset which is the aggregation of the job instance factset (jobinstance.*), the deployed job factset (job.*, jobargs.*), the User factset (user.*), the Matrix factset (matrix.*) and any jobargs or policy facts supplied at the time the job is started.

Fact values are retrieved using the GridObjectInfo functions that the Job class inherits.

Example of retrieving the value of the job instance fact state.string from the jobinstance namespace:

 class Simple(Job):
      def job_started_event(self):
          jobstate = self.getFact("jobinstance.state.string")
          print "job state=%s" % (jobstate)  

Field Summary

static int CANCELLED_STATE

Cancelled end state.

See Constant Field Values

static int CANCELLING_STATE

The job is cancelling and transistions to Cancelled.

See Constant Field Values

static int COMPLETED_STATE

The job has completed the end state.

See Constant Field Values

static int COMPLETING_STATE

The job is completing and transitions to: Completing.

See Constant Field Values

static int FAILED_STATE

The job failed end state.

See Constant Field Values

static int FAILING_STATE

The job is failing and transitions to Failed.

See Constant Field Values

static int PAUSED_STATE

The job is paused and transitions to: Running/Completing/Failing/Cancelling.

See Constant Field Values

static int QUEUED_STATE

The job is queued and transitions to Starting/Failing/Cancelling.

See Constant Field Values

static int RUNNING_STATE

The job is running and transistions to Paused/Completing/Failing/Cancelling.

See Constant Field Values

static int STARTING_STATE

The job is starting and transitions to Running/Failing/Cancelling.

See Constant Field Values

static int SUBMITTED_STATE

The job is submitted and transitions to Queued/Failing.

See Constant Field Values

String TERMINATION_TYPE_ADMIN

Indicates the job was cancelled by the admin.

See Constant Field Values

String TERMINATION_TYPE_JOB

Indicates the job was cancelled by a job function and only applies if the job is in CANCELLED_STATE. The value is obtained from the jobinstance.terminationtype fact.

See Constant Field Values

String TERMINATION_TYPE_TIMEOUT

Indicates the job was cancelled due to exceeding the job timeout value and only applies if the job is in CANCELLED_STATE. The value is obtained from the jobinstance.terminationtype fact.

See Constant Field Values

String TERMINATION_TYPE_USER

Indicates the job was cancelled by a client user and only applies if the job is in CANCELLED_STATE. The value is obtained from the jobinstance.terminationtype fact.

See Constant Field Values

Methods

Detail:

cancel ()

Cancel this job instance.

cancel(reason)

Cancel this job instance with a reason.

cancelAllJoblets()

Cancel all joblets for this job instance. Any joblets that are running or waiting are cancelled. If the job is set to autoterminate, then the job will terminate normally.

changePriority(priority)

Change priority of this job with the specified string priority name.

child_job_cancelled_event(Job job)

child_job_cancelled_event is fired when a child job has transitioned to the cancelled state. A job writer can optionally implement this event to handle job cleanup.

child_job_completed_event(Job job)

child_job_completed_event is fired when a child job has transitioned to the completed state. A job writer can optionally implement this event to handle any job cleanup.

child_job_failed_event(Job job)

child_job_failed_event is fired when a child job has transitioned to the failed state. A job writer can optionally implement this event to handle any job cleanup.

child_job_started_event(Job job)

child_job_started_event is fired when a child job has transitioned to the running state. A job writer can optionally implement this event to handle job state transitions.

getChildJobs()

Retrieves a list of child jobs submitted by this job. The elements are instance of job.

getJoblet(int jobletNumber)

Retrieves a scheduled joblet. joblet may not be running on a resource. The joblet factset is available using the base GridObjectInfo methods. Retrieves the state facts to determine the state of the joblet.

Parameters: jobletNumber - number of joblet.

Returns: Joblet instance..

Raises: Exception - if joblet number refers to a joblet that has not yet been created.

fail()

Fail this job instance.

fail(String reason)

Fail this job instance with a reason.

Parameters: reason - Reason message to put in job log.

id

Retrieve job identification assigned by server on job submission. Returns None until job is submitted or scheduled.

job_cancelled_event()

job_cancelled_event is fired when a job is transitioning to the cancelled state. A job writer can optionally implement this event to handle job cleanup.

job_completed_event()

job_completed_event is fired when the job is transitioning to the completed state. A job writer can optionally implement this event to handle any job cleanup.

job_failed_event()

job_failed_event is fired when the job is transitioning to the failed state. A job writer can optionally implement this event to handle any job cleanup.

job_paused_event()

Fired when a job is paused by user or administrator. A job writer can optionally implement this event to handle state changes.

job_resumed_event()

Fired when a previously paused job is resumed. A job writer can optionally implement this event to handle state changes.

job_started_event()

job_started_event is the first event to be called when a job is started. A job writer is required to implement this event. This event is not fired until a job instance has transitioned out of the queued state. After this event has completed, the job instance is in the running state. Job setup or scheduling of joblets is typically implemented in this event.

joblet_cancelled_event(int jobletNumber, String resourceID, String reason)

joblet_cancelled_event is fired when the joblet has had execution cancelled. The joblet may or may not have been running. A job writer can optionally implement this event to handle a joblet state change. Not that if the job has ended, this event will not be called.

Parameters:

jobletNumber - Joblet number that has been cancelled (0 based).

resourceId - ID of resource where joblet was cancelled. Can be None if was not running.

reason - ID of resource where joblet ran to completion.

joblet_completed_event(int jobletNumber, String resourceId)

joblet_completed_event is fired when the joblet has completed execution on a resource. A job writer can optionally implement this event to handle a joblet state change. Not that if the job has ended, this event will not be called.

Parameters:

jobletNumber - Joblet number that has completed (0 based).

resourceId - ID of resource where joblet ran to completion.

joblet_failed_event(int jobletNumber, String resourceId, String errorMsg)

joblet_failed_event is fired when the joblet has failed execution on a resource. A job writer can optionally implement this event to handle a joblet state change. Not that if the job has ended, this event will not be called.

Parameters:

jobletNumber - Joblet number that has failed.

resourceId - ID of resource where joblet ran.

errorMsg - Error message for the joblet error.

joblet_retry_event(int jobletNumber, String resourceId, String errorMsg)

joblet_retry_event is fired when the joblet is transitioning back to a waiting state after a failed execution on a resource. This event will not fire if the maximum retry settings have been met and joblet transitions to the failed state. A job writer can optionally implement this event to handle a joblet state change.

Parameters:

jobletNumber - Joblet number that is to be retried.

resourceId - ID of resource where joblet ran.

errorMsg - Error message for the joblet error.

joblet_started_event(int jobletNumber)

joblet_started_event is fired when the joblet has started execution on a resource. A job writer can optionally implement this event to handle a joblet state change.

Parameters:

jobletNumber - Joblet number that is to be retried.

pause()

Pause this job instance.

provisioner_cancelled_event(PyDictionary params)

provisioner_cancelled_event is fired when the provision operation submitted by this job has been cancelled. A job writer can optionally implement this event to handle any job response to provision cancellation.

Parameters:

params - Dictionary containing "action", "resource" and "provisionjob" keys.

provisioner_completed_event(PyDictionary params)

provisioner_completed_event is fired when the provision operation submitted by this job has completed. A job writer can optionally implement this event to handle any job response to provision completion.

The following example demonstrates a handler for the completion of suspending a resource

    def provisioner_completed_event(params): 
        if params["action"] == "Suspend":
            print "Successfully suspended resource '%s'" % (params["resource"])

Valid “action” strings include:

   "Provision"
   "Clone"
   "Move"
   "Shutdown"
   "Restart"
   "Destroy"
   "Suspend"
   "Pause"
   "Resume"
   "Personalize"
   "Save Config"
   "Apply Config"
   "Create Template"
   "Migrate"
   "Checkpoint"
   "Restore"
   "Install Agent"
   "Make Standalone"
   "Check Status"
   "Delete"
   "Cancel Action" 

The "resource" key contains the resource ID of the resource the action was acted on. The "provisionjob" key contains the job ID of the provisioning adapter job that was run.

Parameters:

params - Dictionary containing “action”, “resource” and “provisionjob” keys.

provisioner_failed_event(PyDictionary params)

provisioner_failed_event is fired when the provision operation submitted by this job has failed. A job writer can optionally implement this event to handle any job response to provision failure.

Parameters:

params - Dictionary containing “action”, “resource” and “provisionjob” keys.

resume()

Resume this job instance if it is currently paused.

runJob(String job, PyDictionary params, String policy)

Run a new child job using supplied policy text. The supplied policy text is aggregated with the child job's policies.

Parameters:

job - Name of job to run.

params - Dictionary of job arguments.

constraint - Constraint object.

Raises:

Job instance of new child job.

runJob(String job, PyDictionary params, Constraint constraint)

Run a new child job using supplied resource Constraint. The supplied constraint is aggregated with the child job's resource constraints.

Parameters:

job - Name of job to run.

params - Dictionary of job arguments.

policy - Policy text.

Raises:

Job instance of new child job.

runJob

Run a new child job. for processing optionally not returning until the child job's job_started_event has completed.

runJob

Run a new child job using attributes defined in the supplied RunJobSpec.

runJob

Run a new child job.

runJob

Run a new child job.

schedule

Schedule a single joblet.

schedule

Schedule one or more joblets.

schedule

Schedule one or more joblets with additional joblet facts.

schedule

Schedule a set of joblets using the passed ParameterSpace as the basis for joblet assignment.

schedule

Schedule a set of joblets using the passed ParameterSpace as the basis for joblet assignment.

scheduleSweep

Schedule as many joblets as there are matching resources using the job's resource constraints. Each created joblet is preassigned to run on only the specified resource. <p> Note that joblets created with scheduleSweep() will take precedence over regularly scheduled joblets and will prevent regular joblets from running until all preassigned ones are complete. For this reason it is not normally recommended to mix schedule() and scheduleSweep in the same job.

scheduleSweep

Schedule as many joblets as there are matching resources using the job's resource constraints. Each created joblet is preassigned to run on only the specified resource. <p> Note that joblets created with scheduleSweep() will take precedence over regularly scheduled joblets and will prevent regular joblets from running until all preassigned ones are complete. For this reason it is not normally recommended to mix <code>schedule() and scheduleSweep in the same job.

sendEvent

Send an event to the parent job.

sendEventToChildren

Send an event to all children of this parent job.

sendEvent

Send an event to another job. This is typically used to communicate to a child job.

sendClientEvent

Send an event to the client. This allows communication from the job running on the server to a client that is listening to job events using the Client Toolkit.

The following example sends an event to a client with a payload consisting of integer and a string:

    self.sendClientEvent("client_event",{ "param1": 1024, "param2":"hello world" })
sendJobletEvent

Send an event from server to joblet execution on a resource. Joblet must be running on a resource or this event is ignored. Typically used to communicate state changes to a long running joblet.

setAutoTerminate

Control whether a job auto terminates when all child jobs and joblets are finished. Setting to false makes the job a daemon and will not cease until an error or terminate() is called. Default is true.

setAutoTerminateTime

Control whether a job auto terminates when all child jobs and joblets are finished. Setting to a value > 0 causes the job to terminate (cancel) after the specified number of seconds. A value =0 is equivalent to 'true'. This may be called multiple times to reset the termination timer.

start

Start a job that is queued.

terminate

Terminate this job instance. Typically used when the fact <code>job.autoterminate</code> has been set to <code>False</code>.

validateJobContainer

Assert instance is correctly initialized

_getTimers

Retrieve list of timers if any need to be cancelled on job end.

See Also