2

I just wondered how to get the context in scheduled tasks. There is the static method

Context::createDefaultContext()

but it's marked as internal. I know that the context shouldn't be created but rather passed down to your service from higher services. But this doesn't seem possible in scheduled tasks.

1 Answer 1

4

I think I found the answer in a GitHub issue comment: https://github.com/shopware/platform/issues/1245#issuecomment-673537348

The only valid use case to create a default context is when you are in a CLI context, meaning when you write your own CLI command (or scheduled task in that regard). But you should be fully aware that you need to take care of handling translations and currency for example by yourself in that case.

Another valid use case for the default context is the usage in tests of course, that was the original intent behind that method. In the docs the method is used to not bloat the code examples.

So it seems to be the way to go in scheduled tasks to create the context there.

2
  • Yep the default context is the way to go for scheduled tasks or CLI commands
    – j_elfering
    Mar 18, 2021 at 12:14
  • Thanks for confirming @j_elfering :)
    – newgennerd
    Mar 19, 2021 at 13: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.