Answers.Body: <p>I recently dealt with a similar issue. Keep your tasks small and quick by using subtasks or by creating multiple instances of your main task. In your example, make each entry creation its own tasks or subtask, so that each step only has one process to take care of. You can have a private array on the task object to store an array of entry data to iterate through. </p>
  
  <pre><code>private $entry_data = array();
  //Fill array with the data you want to use
  
  public function runStep($step)
  {
  
      $new_entry = $entry_data[$step];
      //code to create new entry
  
      return true;
  
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-24T16:28:25.577Z
Answers.Text: Yea this is what I am doing. The actual code that is executed isn't anything too taxing.
Answers.UserDisplayName: nfourtythree
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-24T16:33:51.080Z
Answers.Text: Cool - do you mind posting some code to help debug?
Answers.UserDisplayName: Aaron Berkowitz
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-24T21:02:47.597Z
Answers.Text: Added code to my original post, slimmed it down slightly just to make it move obvious as to what was happening so there might be a typo or two but should give the gist
Answers.UserDisplayName: nfourtythree
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-24T14:04:19.987Z
Answers.LastActivityDate: 2015-02-24T14:04:19.987Z
Answers.UserDisplayName: Aaron Berkowitz
Body: <p>I have created a simple task to automatically create some entries if they don't exist and this all works fine.</p>
  
  <p>The only issue I am having is that if the task is still running and I try and load a page on the site or another control panel page it simply hangs until the task is complete.</p>
  
  <p>I can't seem to get the task to run in a similar way to the task that runs when updating a section's Entry URL Format (the task spinner and progress bar in the top right).</p>
  
  <p>I have tried using the <a href="https://github.com/pixelandtonic/PowerNap" rel="nofollow">PowerNap</a> example and changing the total steps to 100 (to give me enough time to see what is happening) and the same thing happens.</p>
  
  <p>Do I have to create it as a specific background task?</p>
  
  <p><strong>Edit:</strong> To show sample code</p>
  
  <pre><code>&lt;?php
  namespace Craft;
  
  /**
   * Inception task
   */
  class Bounce_TotalUpdateTask extends BaseTask
  {
      /**
       * Defines the settings.
       *
       * @access protected
       * @return array
       */
      protected function defineSettings()
      {
          return array();
      }
  
      /**
       * Returns the default description for this task.
       *
       * @return string
       */
      public function getDescription()
      {
          return 'Update Total Record';
      }
  
      /**
       * Gets the total number of steps for this task.
       *
       * @return int
       */
      public function getTotalSteps()
      {
          return craft()-&gt;elements-&gt;getCriteria(ElementType::User, array(
              "group" =&gt; "members",
              "limit" =&gt; null,
          ))-&gt;total();
      }
  
      /**
       * Runs a task step.
       *
       * @param int $step
       * @return bool
       */
      public function runStep($step)
      {
          $users = craft()-&gt;elements-&gt;getCriteria(ElementType::User, array(
              "group" =&gt; "members",
              "limit" =&gt; null,
          ))-&gt;find();
  
          $section = craft()-&gt;sections-&gt;getSectionByHandle("totals");
  
          if (isset($users[$step])) {
              $user = $users[$step];
  
              $record = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry, array(
                      "section" =&gt; "totals",
                      "authorId" =&gt; $user-&gt;id,
                  )
              )-&gt;first();
  
              if (!$record) {
                  $entry = new EntryModel();
                  $entry-&gt;sectionId  = $section-&gt;id;
                  $entry-&gt;typeId     = $section-&gt;id;
                  $entry-&gt;authorId   = $user-&gt;id;
                  $entry-&gt;enabled    = true;
  
                  craft()-&gt;entries-&gt;saveEntry($entry);
              }
  
              return true;
          } else {
              return "Result " . ($step) . " does not exist";
          }
      }
  }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-24T16:02:11.823Z
Comments.Text: Do you have gzip output compression enabled either at the PHP level or the Apache level?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-24T16:44:56.490Z
Comments.Text: Not on my local environment which is where I am getting the issue. When I hit my controller action that creates the task I see that runPendingTasks starts the process.
Comments.UserDisplayName: nfourtythree
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-24T16:46:31.403Z
Comments.Text: If I change the Entry URL format for say a news section from news/ to newz/ the ResaveAllElements task runs as expected
Comments.UserDisplayName: nfourtythree
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-25T13:35:09.217Z
Comments.Text: any ideas @BradBell ?
Comments.UserDisplayName: nfourtythree
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-02-25T19:29:16.250Z
Comments.Text: Not particularly... the only other time we've seen that happen is when gzip compression was enabled and we added this config setting (http://buildwithcraft.com/docs/config-settings#runTasksAutomatically) as a workaround.
Comments.UserDisplayName: Brad Bell
Tags: <plugin-development><control-panel><tasks>
Title: Creating Task cause Site/CP to hang
ThreadText: # Creating Task cause Site/CP to hang
  
  (Asked by: username_0 on 2015-02-24)
  
  I have created a simple task to automatically create some entries if they don't exist and this all works fine.
  
  The only issue I am having is that if the task is still running and I try and load a page on the site or another control panel page it simply hangs until the task is complete.
  
  I can't seem to get the task to run in a similar way to the task that runs when updating a section's Entry URL Format (the task spinner and progress bar in the top right).
  
  I have tried using the [PowerNap](https://github.com/pixelandtonic/PowerNap) example and changing the total steps to 100 (to give me enough time to see what is happening) and the same thing happens.
  
  Do I have to create it as a specific background task?
  
  **Edit:** To show sample code
  
  ```
  <?php
  namespace Craft;
  
  /**
   * Inception task
   */
  class Bounce_TotalUpdateTask extends BaseTask
  {
      /**
       * Defines the settings.
       *
       * @access protected
       * @return array
       */
      protected function defineSettings()
      {
          return array();
      }
  
      /**
       * Returns the default description for this task.
       *
       * @return string
       */
      public function getDescription()
      {
          return 'Update Total Record';
      }
  
      /**
       * Gets the total number of steps for this task.
       *
       * @return int
       */
      public function getTotalSteps()
      {
          return craft()->elements->getCriteria(ElementType::User, array(
              "group" => "members",
              "limit" => null,
          ))->total();
      }
  
      /**
       * Runs a task step.
       *
       * @param int $step
       * @return bool
       */
      public function runStep($step)
      {
          $users = craft()->elements->getCriteria(ElementType::User, array(
              "group" => "members",
              "limit" => null,
          ))->find();
  
          $section = craft()->sections->getSectionByHandle("totals");
  
          if (isset($users[$step])) {
              $user = $users[$step];
  
              $record = craft()->elements->getCriteria(ElementType::Entry, array(
                      "section" => "totals",
                      "authorId" => $user->id,
                  )
              )->first();
  
              if (!$record) {
                  $entry = new EntryModel();
                  $entry->sectionId  = $section->id;
                  $entry->typeId     = $section->id;
                  $entry->authorId   = $user->id;
                  $entry->enabled    = true;
  
                  craft()->entries->saveEntry($entry);
              }
  
              return true;
          } else {
              return "Result " . ($step) . " does not exist";
          }
      }
  }
  
  ```
  
  **Tags:** plugin-development, control-panel, tasks
  
  > **Comments:**
  > **username_1**: Do you have gzip output compression enabled either at the PHP level or the Apache level?  
  > **username_0**: Not on my local environment which is where I am getting the issue. When I hit my controller action that creates the task I see that runPendingTasks starts the process.  
  > **username_0**: If I change the Entry URL format for say a news section from news/ to newz/ the ResaveAllElements task runs as expected  
  > **username_0**: any ideas @username_1 ?  
  > **username_1**: Not particularly... the only other time we've seen that happen is when gzip compression was enabled and we added this config setting (http://buildwithcraft.com/docs/config-settings#runTasksAutomatically) as a workaround.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2015-02-24. Score: 2
  
  I recently dealt with a similar issue. Keep your tasks small and quick by using subtasks or by creating multiple instances of your main task. In your example, make each entry creation its own tasks or subtask, so that each step only has one process to take care of. You can have a private array on the task object to store an array of entry data to iterate through.
  
  ```
  private $entry_data = array();
  //Fill array with the data you want to use
  
  public function runStep($step)
  {
  
      $new_entry = $entry_data[$step];
      //code to create new entry
  
      return true;
  
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Yea this is what I am doing. The actual code that is executed isn't anything too taxing.  
  > **username_2**: Cool - do you mind posting some code to help debug?  
  > **username_0**: Added code to my original post, slimmed it down slightly just to make it move obvious as to what was happening so there might be a typo or two but should give the gist  
  
  ---
  
  