<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def TerminateStuckRunIfNeeded(self, job):
 """Cleans up job state if the last run is stuck."""

    if job.current_run_id and job.last_run_time and job.lifetime:
      now = rdfvalue.RDFDatetime.Now()
      # We add additional 10 minutes to give the job run a chance to kill itself
      # during one of the HeartBeat calls (HeartBeat checks if a cron job is
      # run is running too long and raises if it is).
      expiration_time = (
          job.last_run_time + job.lifetime + rdfvalue.Duration("10m"))
      if now > expiration_time:
        run = data_store.REL_DB.ReadCronJobRun(job.cron_job_id,
                                               job.current_run_id)
        run.status = "LIFETIME_EXCEEDED"
        run.finished_at = now
        data_store.REL_DB.WriteCronJobRun(run)
        data_store.REL_DB.UpdateCronJob(
            job.cron_job_id, current_run_id=None, last_run_status=run.status)
        stats_collector_instance.Get().RecordEvent(
            "cron_job_latency", (now - job.last_run_time).seconds,
            fields=[job.cron_job_id])
        stats_collector_instance.Get().IncrementCounter(
            "cron_job_timeout", fields=[job.cron_job_id])

        return True

    return False