title: Django celery - to check if a task is complete, if complete to restart the task (if not complete, do not restart the task)
body: <p>I am currently trying to run a periodic check on a scheduled celery task every minute. If the task is still running, to let it continue running and not interrupt it, but if the task has no longer running, to activate the task and start running it. But at the moment, I cannot get the script to run only when it is no longer running. I have tried two methods of doing it but my script does not detect the existing running script and it starts to run even when it shouldn't and my task starts to run simultaneously. I am using celery 4.2.0 and django 1.11.6. Any tips on how I can solve this problem? Thanks</p>
  <h2>in views.py</h2>
  <h1>Task to run</h1>
  <pre><code>@task(name='send-one-task')
  def send_one_task():
  
      for i in range(1,100):
          time.sleep(1)
          print (&quot;test1 &quot; + str(i))
  
      return None
  </code></pre>
  <p>I have tried two methods to to check if process is complete and stopped running - if its not, do not rerun it</p>
  <h2>method 1</h2>
  <pre><code>@task(name='send-two-task')
  def send_two_task():
  
      # method 1
      from celery import current_task
      if current_task.request.task != &quot;send-one-task&quot;:
          send_one_task()
      else:
          pass
      
      return None
  </code></pre>
  <h2>method 2</h2>
  <pre><code>@task(name='send-two-task')
  def send_two_task():
  
      from celery.task.control import inspect
      insp = inspect()
      testactive = insp.active()
      checkrunning = list(testactive.values())
  
      try:
          #the script is still running - so ok.
          print (checkrunning[0][1].get(&quot;name&quot;))
          print (&quot;task still running - ok&quot;)
          pass
  
      except:
          #the task has failed so need to restart
          print (&quot;task not running - restart task&quot;)
          send_one_task()
          pass
  
      return None
  </code></pre>
  <p>To schedule the check on the task to get it running every 60 seconds if it is not already</p>
  <h1>In celery.py</h1>
  <pre><code>'send_second_task': {
      'task': 'send-two-task',
      'schedule': 60.0,
  },
  </code></pre>