6

I created a pipeline with only one block, on the block implementation I get a Policy object, something like this:

var testPolicy = context.GetPolicy<TestPolicy>();
Condition.Requires(testPolicy).IsNotNull("testPolicy is missing");
Condition.Requires(testPolicy.Test).IsNotNullOrEmpty("The testPolicy.Test can not be null or empty");

I need to run this pipeline only once, since it will spin one thread that will be doing some work while the app is running, so that's why I'm trying to run it from the Startup class Configure method like this:

Task.Run(() => testPipeline.Run(string.Empty, NodeContext.GetPipelineContextOptions()));

But in this case the testPolicy.Test won't be loaded properly, so the assertion will fail.

If I setup this pipeline to run as part of any existing one for example any of the carts pipeline it will properly load the testPolicy.Test so it is not an issue with the policy setting on the environments json files or anything like that. My guess is that at that point (running from Startup class Configure method) the CommerceContext or something else is not completely loaded/setup...?

Anyway, I guess the question really is, where can I start this pipeline so that it gets executed only once (as soon as the app starts) without any issue loading policy objects?

2

You need to run your pipeline in another way from Configure method in Startup.cs:

testPipeline.Run(this.NodeContext, this.NodeContext.GetPipelineContextOptions()).Wait();

This is OOTB example how to run pipeline. Also, try to add your code in the end of Configure method.

  • Thanks, that was one of the things I already tried and didn't work, my line is the last one in Configure method, also tried this one testPipeline.Run(string.Empty, NodeContext.GetPipelineContextOptions()); – Geykel Jan 24 '18 at 12:46
  • try to call .Wait() for run your pipeline synchronously – Andrei Paliakou Jan 24 '18 at 12:48
  • That's what I said, I tried with your example with the .Wait() also without it and it doesn't work – Geykel Jan 24 '18 at 12:56
  • First of all you should be sure that you added your pipeline configuration to right Environment Configuration. After that you need to run Bootstrap() and check again your pipeline run – Andrei Paliakou Jan 24 '18 at 13:08
  • My pipeline is configured in code, in my plugin ConfigureSitecore class, as I mentioned in the description of the problem, my pipeline runs fine if it is executed from any other place, for example, if I insert my pipeline in the add cart line pipeline or any other existing one it runs perfectly but of course I don't want that, I want to run it only one time as soon as the engine starts (the purpose of the pipeline is to start a new thread that will be listening for messages on a queue, so that is why I need it to run only one time right after the engine starts). – Geykel Jan 24 '18 at 13:17
1

IStartEnvironmentPipeline is run every time the environment process starts, so if you'd like to run some code on startup it's best to define a block for that pipeline.

Also, it's a good idea to guard for HasPipeline<TestPolicy> and return early if it doesn't exist. This will let you continue to use the engine engine code and start your custom job in environments that define the policy, but not ones that don't.

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.