1

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 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";
        }
    }
}
5
  • 1
    Do you have gzip output compression enabled either at the PHP level or the Apache level?
    – Brad Bell
    Feb 24 '15 at 16:02
  • 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. Feb 24 '15 at 16:44
  • If I change the Entry URL format for say a news section from news/ to newz/ the ResaveAllElements task runs as expected Feb 24 '15 at 16:46
  • any ideas @BradBell ? Feb 25 '15 at 13:35
  • Not particularly... the only other time we've seen that happen is when gzip compression was enabled and we added this config setting (buildwithcraft.com/docs/config-settings#runTasksAutomatically) as a workaround.
    – Brad Bell
    Feb 25 '15 at 19:29
1

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;

}
3
  • Yea this is what I am doing. The actual code that is executed isn't anything too taxing. Feb 24 '15 at 16:28
  • Cool - do you mind posting some code to help debug? Feb 24 '15 at 16:33
  • 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 Feb 24 '15 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.