Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running StaticCacheFullBuildTask via QueuedJobs admin logs user out #171

Open
emteknetnz opened this issue Aug 14, 2023 · 1 comment
Open

Comments

@emteknetnz
Copy link
Member

From #168 (review)

6.0.0

  1. Run vendor/bin/sake dev/tasks/SilverStripe-StaticPublishQueue-Task-StaticCacheFullBuildTask from CLI - this create a queuedjob
  2. Run it via the QueuedJobsAdmin it times out after 30 seconds and logs me out.
  3. It did create files in the public/cache directory though messages for the job showed [ALERT] Fatal Error (E_ERROR): Maximum execution time of 30 seconds exceeded {"code":1,"message":"Maximum execution time of 30 seconds exceeded","file":"/var/www/vendor/silverstripe/framework/src/ORM/FieldType /DBDate.php","line":87,"trace":null} [] Tried reverting framework to 5.0.0, same issue.
@joelgrondrup
Copy link

joelgrondrup commented Aug 22, 2024

Hi guys,

I had a similar problem, but in the CMS. After starting a job which should generate some static pages for caching I was always logged out. I thought it was a memory issue, but it wasn't.

After investigating I found the problem in the end of a function called runJob($jobId) in the class QueuedJobService in the SilverStripe Queued Jobs module.

There is a call to $this->unsetRunAsUser($runAsUser, $originalUser), but if the runAsUser is null, Security will never log in a user again after the job has been running in a new Kernel.

To fix the problem I injected a custom class on top of QueuedJobService and made a few changes to the protected function unsetRunAsUser:

/**
     * @param Member|null $runAsUser
     * @param Member|null $originalUser
     */
    protected function unsetRunAsUser(Member $runAsUser = null, Member $originalUser = null)
    {
        // No runAsUser was set, so we don't need to do anything.
        //This fixes the problem
        if ($runAsUser === null **&& $originalUser === null**) {
            return;
        }

        // There was no originalUser, so we should make sure that we set the user back to null.
        if (!$originalUser) {

            if (Controller::has_curr()) {
                Security::setCurrentUser(null);
            } else {
                $_SESSION['loggedInAs'] = null;
            }

            return;
        }

        // Okay let's reset our user.
        if (Controller::has_curr()) {
            Security::setCurrentUser($originalUser);
            **//This fixes the problem
            $identityStore = Injector::inst()->get(IdentityStore::class);
            $identityStore->logIn($originalUser, true);**
        } else {
            $_SESSION['loggedInAs'] = $originalUser->ID;
        }
    }

Now, I'm still logged into the cms after creating the static pages. This is probably not best practice, but it fixed my problem.

Summary.

  1. Create a yml with this content:

SilverStripe\Core\Injector\Injector:
Symbiote\QueuedJobs\Services\QueuedJobService:
class: CustomQueuedJobService

  1. Copy the class QueuedJobService from the SilverStripe Queued Jobs module and make a custom class and change the function as described above: CustomQueuedJobService.txt

Voila.

Hope it's useful for someone. And that maybe @symbiote can have a look at this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants