I'm trying to create a scheduled task that runs every x amount of time that can move a page to the trash can where the requirement is met. Oh and this has to be done through SQL as the scheduled task is run as a windows task meaning it has no link to the Umbraco project, just has access to the database!
In the umbracoNode table, there is a 'trashed' row so I thought changing this would move the page to the trashcan but I was wrong!
Could anyone shed some light on why just setting the trashed from 0 to 1 hasn't moved the page to the trash can and what else I need to do?!
why use the Windows Task Scheduler and not the builtin scheduler of umbraco or hangfire?
Then you have access to the service api of Umbraco to properly move the node to the recycle bin.
<scheduledTasks>
<!-- add tasks that should be called with an interval (seconds) -->
<!-- <task log="true" alias="test60" interval="60" url="http://localhost/umbraco/test.aspx"/>-->
</scheduledTasks>
I was going to do this but couldnt get it working. I was pointing the url in the task to my controller /umbraco/surface/controllername/method and it wouldnt hit it.
Could you give me a link to some docs on the service api?
using Umbraco.Web.Mvc;
namespace handt.Controllers
{
public class RemoveSoldProductsController : SurfaceController
{
public static string RemoveSoldProducts()
{
return "hit me";
}
}
}
As simple as it comes
This is my scheduled task:
<scheduledTasks>
<!-- add tasks that should be called with an interval (seconds) -->
<task log="true" alias="test60" interval="10" url="https://localhost:61884/umbraco/Surface/RemoveSoldProducts/RemoveSoldProducts"/>
</scheduledTasks>
So got that in there! When passing the parameters into the MoveToRecyceBin method i have to pass in IContent, Can i pass the node ID in here? At the moment this is my process:
When a product is purchased this product gets saved to a table with details such as alias, nodeId, title, time sold, delete time and if its deleted or not.
My task that now gets hit (yess) will run every hour or so, if the current time is > the delete time of the item in the database then teh item will be moved to the recycle bin!
If i cant use the nodeId what other info is required that i can pass into the MoveToRecycleBin method?
So if you have an id, you just call the contentservice like above to get the content object and then you can pass this into the MoveToRecycleBin method.
One last thing! I now have everything working as expected when I hit the controller URL myself, but the scheduled task doesn't actually do anything... I have a breakpoint in the controller and this doesn't get hit at all.
Is there anything wrong with the task itself?
<scheduledTasks>
<!-- add tasks that should be called with an interval (seconds) -->
<task log="true" alias="test60" interval="60" url="https://localhost:61884/umbraco/Surface/RemoveSoldProducts/RemoveSoldProducts"/>
</scheduledTasks>
When entering the URL myself it works as expected. But that's all.
Move page to trash using SQL
Hi all,
I'm trying to create a scheduled task that runs every x amount of time that can move a page to the trash can where the requirement is met. Oh and this has to be done through SQL as the scheduled task is run as a windows task meaning it has no link to the Umbraco project, just has access to the database!
In the umbracoNode table, there is a 'trashed' row so I thought changing this would move the page to the trashcan but I was wrong!
Could anyone shed some light on why just setting the trashed from 0 to 1 hasn't moved the page to the trash can and what else I need to do?!
Thanks all, Lewis
Hi Lewis,
why use the Windows Task Scheduler and not the builtin scheduler of umbraco or hangfire?
Then you have access to the service api of Umbraco to properly move the node to the recycle bin.
Hope this helps.
/Michaël
Hi Michael,
I was going to do this but couldnt get it working. I was pointing the url in the task to my controller /umbraco/surface/controllername/method and it wouldnt hit it.
Could you give me a link to some docs on the service api?
Thanks, Lewis
Hi lewis,
I am not at my desk atm. When I am back I will see if I can find some usefull links.
/Michaël
Hi Lewis,
is it possible to show us the controller and the task defined in the scheduled taks config which causes troubles?
Thanks!
/Michaël
Hi Michael,
This is my controller:
As simple as it comes
This is my scheduled task:
Thanks, Lewis
Hi Lewis,
what do you see when you hit the url in the browser?
Also, why do you declare your method static?
Thanks!
/Michaël
I get the following:
Not sure why its static, I'm still learning the who MVC side of things!
Thanks, Lewis
Lewis,
remove the static part and hit it again.
Hope this helps!
/Michaël
Got it!
Thanks Michael!!!!
Now just to come up with the SQL -_-
Hi Lewis,
no need for SQL that's why you use this controller to have access to the services of Umbraco!
Take a look at the link that @Dave has posted.
Have a nice day!
/Michaël
Hi Lewis,
You don't need the sql. You can use the content service to do this.
In your controller action this available under
this.Services.ContentService
Then you can use the MoveToRecycleBin method https://our.umbraco.org/apidocs/csharp/api/Umbraco.Core.Services.ContentService.html#UmbracoCoreServicesContentServiceMoveToRecycleBinUmbracoCoreModelsIContentSystemInt32_
Dave
Hi Dave,
So got that in there! When passing the parameters into the MoveToRecyceBin method i have to pass in IContent, Can i pass the node ID in here? At the moment this is my process:
When a product is purchased this product gets saved to a table with details such as alias, nodeId, title, time sold, delete time and if its deleted or not.
My task that now gets hit (yess) will run every hour or so, if the current time is > the delete time of the item in the database then teh item will be moved to the recycle bin!
If i cant use the nodeId what other info is required that i can pass into the MoveToRecycleBin method?
Thanks, Lewios
Hi Lewis,
you can use the contentservice to get the content object:
https://our.umbraco.org/documentation/reference/management/services/contentservice#getbyidint-id
So if you have an id, you just call the contentservice like above to get the content object and then you can pass this into the
MoveToRecycleBin
method.Hope this helps.
/Michaël
So something like this:
Thanks, Lewis
Didnt see the get id part!
Works a treat!
Hi Lewis,
nice to see you have found the solution!
Have a nice day.
/Michaël
Hi Michael,
One last thing! I now have everything working as expected when I hit the controller URL myself, but the scheduled task doesn't actually do anything... I have a breakpoint in the controller and this doesn't get hit at all.
Is there anything wrong with the task itself?
When entering the URL myself it works as expected. But that's all.
Thanks, Lewis
Hi Lewis,
I see
https://...
could this be the issue? And isn't ishttp://...
?Hope this helps!
/Michaël
My VS is running over https:// so this shouldnt be an issue?
Thanks, Lewis
Lewis,
ok and what about the log files? Do you see anything there?
/Michaël
Nothing in there that i dont expect!
This is my full logs from start up:
Doesn't give me an error stating that it cant find the path or anything..!
Hi Lewis,
I see you scheduled task is configured for this url :
https://localhost:61884
But you run Umbraco using this url :
http://localhost/umbraco
accordng to this log lineCan you check the config mentioned in my other answer ?
Hi Lewis,
Can you set your log level to DEBUG and check if there is a mention of "Does not run on slave servers" ?
Also can you check what you have configured as umbracoApplicationUrl https://our.umbraco.org/documentation/Reference/Config/umbracoSettings/#webrouting
If this is not set Umbraco will try to find it from the request. This url is also used for the scheduler.
Dave
How do I set my log level to debug? I'm running the project on debug mode? I have also searched the solution for 'Does not run on slave servers'
This is what i have in my umbracoSettings:
is working on a reply...