parameters: (self, *args, **kwargs)
return_statement: return result
docstring: Save the job, with current status, to the database.
          Ensure that all data is consistent before doing so.
docstring_summary: Save the job, with current status, to the database.
          Ensure that all data is consistent before doing so.
docstring_tokens: Save the job with current status to the database . Ensure that all data is consistent before doing so .
function: def save(self, *args, **kwargs):
          """Save the job, with current status, to the database.
          Ensure that all data is consistent before doing so.
          """
          # If update_fields has been specified, add our field names to it,
          # if it hasn't been specified, then we're just doing a normal save.
          update_fields = kwargs.get('update_fields', [])
  
          # Get status before save...
          status_before = self.status or 'new'
  
          # If this job already exists in the database, retrieve a copy of
          # the job in its prior state.
          if self.pk:
              self_before = self.__class__.objects.get(pk=self.pk)
              if self_before.status != self.status:
                  status_before = self_before.status
  
          # Sanity check: Is this a failure? Ensure that the failure value
          # matches the status.
          failed = bool(self.status in ('failed', 'error', 'canceled'))
          if self.failed != failed:
              self.failed = failed
              if 'failed' not in update_fields:
                  update_fields.append('failed')
  
          # Sanity check: Has the job just started? If so, mark down its start
          # time.
          if self.status == 'running' and not self.started:
              self.started = now()
              if 'started' not in update_fields:
                  update_fields.append('started')
  
          # Sanity check: Has the job just completed? If so, mark down its
          # completion time, and record its output to the database.
          if self.status in ('successful', 'failed', 'error', 'canceled') and not self.finished:
              # Record the `finished` time.
              self.finished = now()
              if 'finished' not in update_fields:
                  update_fields.append('finished')
  
          dq = decimal.Decimal('1.000')
          if self.elapsed is None:
              self.elapsed = decimal.Decimal(0.0).quantize(dq)
  
          # If we have a start and finished time, and haven't already calculated
          # out the time that elapsed, do so.
          if self.started and self.finished and self.elapsed == 0.0:
              td = self.finished - self.started
              elapsed = decimal.Decimal(td.total_seconds())
              self.elapsed = elapsed.quantize(dq)
              if 'elapsed' not in update_fields:
                  update_fields.append('elapsed')
  
          # Ensure that the job template information is current.
          if self.unified_job_template != self._get_parent_instance():
              self.unified_job_template = self._get_parent_instance()
              if 'unified_job_template' not in update_fields:
                  update_fields.append('unified_job_template')
  
          if self.cancel_flag and not self.canceled_on:
              # Record the 'canceled' time.
              self.canceled_on = now()
              if 'canceled_on' not in update_fields:
                  update_fields.append('canceled_on')
          # Okay; we're done. Perform the actual save.
          result = super(UnifiedJob, self).save(*args, **kwargs)
  
          # If status changed, update the parent instance.
          if self.status != status_before:
              # Update parent outside of the transaction for Job w/ allow_simultaneous=True
              # This dodges lock contention at the expense of the foreign key not being
              # completely correct.
              if getattr(self, 'allow_simultaneous', False):
                  connection.on_commit(self._update_parent_instance)
              else:
                  self._update_parent_instance()
  
          # Done.
          return result
function_tokens: def save ( self , * args , * * kwargs ) : # If update_fields has been specified, add our field names to it, # if it hasn't been specified, then we're just doing a normal save. update_fields = kwargs . get ( 'update_fields' , [ ] ) # Get status before save... status_before = self . status or 'new' # If this job already exists in the database, retrieve a copy of # the job in its prior state. if self . pk : self_before = self . __class__ . objects . get ( pk = self . pk ) if self_before . status != self . status : status_before = self_before . status # Sanity check: Is this a failure? Ensure that the failure value # matches the status. failed = bool ( self . status in ( 'failed' , 'error' , 'canceled' ) ) if self . failed != failed : self . failed = failed if 'failed' not in update_fields : update_fields . append ( 'failed' ) # Sanity check: Has the job just started? If so, mark down its start # time. if self . status == 'running' and not self . started : self . started = now ( ) if 'started' not in update_fields : update_fields . append ( 'started' ) # Sanity check: Has the job just completed? If so, mark down its # completion time, and record its output to the database. if self . status in ( 'successful' , 'failed' , 'error' , 'canceled' ) and not self . finished : # Record the `finished` time. self . finished = now ( ) if 'finished' not in update_fields : update_fields . append ( 'finished' ) dq = decimal . Decimal ( '1.000' ) if self . elapsed is None : self . elapsed = decimal . Decimal ( 0.0 ) . quantize ( dq ) # If we have a start and finished time, and haven't already calculated # out the time that elapsed, do so. if self . started and self . finished and self . elapsed == 0.0 : td = self . finished - self . started elapsed = decimal . Decimal ( td . total_seconds ( ) ) self . elapsed = elapsed . quantize ( dq ) if 'elapsed' not in update_fields : update_fields . append ( 'elapsed' ) # Ensure that the job template information is current. if self . unified_job_template != self . _get_parent_instance ( ) : self . unified_job_template = self . _get_parent_instance ( ) if 'unified_job_template' not in update_fields : update_fields . append ( 'unified_job_template' ) if self . cancel_flag and not self . canceled_on : # Record the 'canceled' time. self . canceled_on = now ( ) if 'canceled_on' not in update_fields : update_fields . append ( 'canceled_on' ) # Okay; we're done. Perform the actual save. result = super ( UnifiedJob , self ) . save ( * args , * * kwargs ) # If status changed, update the parent instance. if self . status != status_before : # Update parent outside of the transaction for Job w/ allow_simultaneous=True # This dodges lock contention at the expense of the foreign key not being # completely correct. if getattr ( self , 'allow_simultaneous' , False ) : connection . on_commit ( self . _update_parent_instance ) else : self . _update_parent_instance ( ) # Done. return result