Long running Scheduled tasks

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 years ago
Hi

I am creating a plugin with a ScheduleTask which will call an external webservice that i have no control over.
This webservice is returning a very long list of products, and the call takes 3-4 minutes.
Thats more than the default timeout value of the WebClient which the TaskThread.cs is using:


using (var client = new WebClient())
{
     client.UploadValues(_scheduleTaskUrl, postData);
}


That part i can solve by using async/await on my Execute method like this:


public async void Execute()
{            
     await GetData();

     _aoProductService.SaveData(_variantData, _updaterName);          
}


Now my problem is that i need to save that data to my database.
I do that by calling this method:
_aoProductService.SaveData which has been instanciated by Depencency Injection.
That will cause an error as the context has been disposed by the await call.

Error:
"Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: 'NopObjectContext'."


Is there any other way of doing this. (maybe a good way of increasing the timeout of the WebClient?
Any help will be appreciated.

Best regards,
Axel
5 years ago
Can you not just EngineContext.Current.Resolve<>() an instance of your service after the await?
5 years ago
This can be really tricky. A co-worker of mine had luck by including an instance of IServiceScopeFactory in their constructor and doing something like this:


var process = new Task(delegate ()
{
    using (var scope = _serviceScopeFactory.CreateScope())
    {
        var myProductService = scope.ServiceProvider.GetService<ICustomProductServiceYouWrote>();
        myProductService.SaveData(_variantData, _updaterName);
    }
});
process.Start();
process.Wait();


IServiceScopeFactory being part of Microsoft's Dependency Injection library (i.e. not AutoFac). So far I've seen that it can be used in lieu of AutoFac anywhere in Nop, and not dispose any contexts.

Good luck!
5 years ago
Thanks for your posts.
No one have a solution which includes extending the timeout of the WebClient?
I would rather not make changes to a core nop library.

Maybe i will have 2 schedules running, one for getting the data, and saving it as csv to disk.
Then one schedule which runs through csv lines and saves to database.

I dont see any function to execute a schedule after another has finished.

Hmmmmm...
5 years ago
Has anyone found a solution for this issue yet?
We are having many random timeouts from our scheduled tasks, not sure why, but extending the WebClient timeout may help.
3 years ago
Sizzler wrote:
Has anyone found a solution for this issue yet?
We are having many random timeouts from our scheduled tasks, not sure why, but extending the WebClient timeout may help.

4.2/4.3 also shows timeout problems
3 years ago
look for this parameter
commonsettings.scheduletaskruntimeout
3 years ago
foxnetsoft wrote:
look for this parameter
commonsettings.scheduletaskruntimeout


Hi,
Is this setting came with nopcommerce 4.30 ?
so if you dont set value the default time for HttpClient is 100 Seconds (100,000 ms)
3 years ago
jigarsangoi wrote:
Is this setting came with nopcommerce 4.30 ?

Was added before. It was at 4.10 and 4.20. Not sure when was added.

Regards,
Tom
3 years ago
nop4you wrote:
Is this setting came with nopcommerce 4.30 ?
Was added before. It was at 4.10 and 4.20. Not sure when was added.

Regards,
Tom


Because in 4.10 I dont get too many exception
The scheduled task failed with the "A scheduled task canceled. Timeout expired
We skipped 4.20 & now in 4.30 getting too many logs for above exception
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.