Schedule task behaviour - unexpected when triggering from outside (webhook)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
2 years ago
Hello.
I have a webhook, basically an api-endpoint that triggers a schedule task when called upon. This is called from another application outside Nop. (The task runs on schedule and also needs to be triggered like this for timestamp)

The scheduled task triggered creates, updates and unpublish some products. When running on schedule and triggered with "Run now" from inside Nop Admin everything works fine. But when triggered with the webhook (its public) and executes exactly like the code in "Run Now" method the products are created. But the updates and unpublish is behaving strange.

My custom log says that the product is unpublished so I know the code runs, but its not unpublished when looking in the database and admin space. Im using ProductService updating product with product.Published = 0 and _productService.Update(product) so nothing advanced.  Also when updating Im updating the UpdatedOnUtc but this isn't changed either. And as said, it works when triggering from Nop Admin.

In my head Im thinking its some authorization problem, since Im logged in when triggering with "Run Now" and the web hook triggering isn't. But in the code there is nothing checking för permissions when executing the tasks or services.

Hosted on Azure Webapp.

Any idées?
Thanks
2 years ago
Is creating the new products a long running process?  You could be hitting an Azure/db time out.

First thing I would do is to run it locally and hit the endpoint with something like Postman to rule out as much as possible.  I would also make use of _logger and try/catch for exceptions to determine exactly where things go awry (log how long new products take, how many iterations, does it exit before updates, etc).
2 years ago
Thanks for the reply. I have tested these things already but it's not the case unfortunately.
The task can run for up to 20 mins but does no bulk updates, only sinlge updates with ProductService (just to avoid timeout)

Its like, why does only create work, but not update. Blows my mind right now.. have debugged like a crazy.
2 years ago
Just to make sure I understand, the exact same code runs successfully from a task and when triggered with "run now", but only partially succeeds when triggered from the webhook, correct?  And when triggered from the webhook your logger claims the updates are performed, however, they are not actually performed?  

I assume your public webhook is different than the task format of https://yourwebsite.com/Admin/ScheduleTask/RunNow/1 ?

Is it possible to post the code for the webhook?
2 years ago
Another thought, if this is version 4.4+ be sure to use UpdateProductAsync so the thread doesn't march past your updates before they return:
https://github.com/nopSolutions/nopCommerce/blob/54bfd794e69cab352202176720efed2dacb65419/src/Libraries/Nop.Services/Catalog/ProductService.cs#L582
2 years ago
How are you running the task?  FYI, this is how ScheduleTaskController does it:
public virtual async Task<IActionResult> RunTask(string taskType)
{
    var scheduleTask = await _scheduleTaskService.GetTaskByTypeAsync(taskType);
    if (scheduleTask == null)
        //schedule task cannot be loaded
        return NoContent();

    var task = new Task(scheduleTask);
    await task.ExecuteAsync();

    return NoContent();
}
2 years ago
It's version 4.2.
Here's the code, its almost the same as the ScheduleTaskController in version 4.2. So hard to believe this is where the problem is. I know for a fact the code runs and iterates the products and runs the update, create, unpublish loops. Maybe just log more more more to try and find the needle in the haystack. The code should be nothing wrong with tough because its works when triggered from within Nop Admin.

 public IActionResult Trigger(int id)
        {
            try
            {
                //try to get a schedule task with the specified id
                var scheduleTask = _scheduleTaskService.GetTaskById(id)
                                   ?? throw new ArgumentException("Schedule task cannot be loaded", nameof(IActionResult));

                if(scheduleTask == null)
                    return NotFound("No task found");

                var task = new Task(scheduleTask) { Enabled = true };
                task.Execute(true, false);
            }
            catch (Exception e)
            {
                _logger.Error(e.Message, e);
            }

            return NoContent();
        }
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.