Timer Job from a SPWeb - sharepoint

I guess starting a timer job from within the code requires Farm admin credentials. However, I need to start a timer job from a web part that will be used in any site. Now when I try to start the job it gives me an access denied error obviously because the app pool identity is not farm admin. Any ideas on how to resolve this issue?
Thanks,

Timer jobs run as the farm administrator and are not intended to be triggered directly by an end-user. Since some jobs may be resource intensive, only the farm admin can create new jobs or modify the schedule for existing jobs.
One solution is to use the SPWorkItem infrastructure to queue user tasks which are then processed by a custom timer job derived from SPWorkItemJobDefinition. Your webpart would call SPSite.AddWorkItem to add the work item. When your timer job runs, it will look for any work items with the matching WorkItemType GUID and invoke the ProcessWorkItem overload.

Your are right. To start a timer job the app pool user has to be farm administrator. Since starting a timer job requires you to update a SPJobDefinition object with an SPSchedule. The SPJobDefinition is a SPPersistedObject which is stored in the SharePoint Config Database. Only farm administrators can write into this db.
I don't see a way to get pass this issue.
Workaround:
Depending on your requirements you could write a master job that runs on regular basis. This job could query a SharePoint list and start another job defined by such a list item. Since the master job runs under the Farm Administrator account, the job would be able to start a new timer job.

Related

User Scheduled Jobs in Azure

We have a requirement for generating reports based on a schedule. These schedules can be configured by the users for a report. Initially We were planning to use Quartz in a worker role and Create jobs based on the user input.
I'm not sure whether the Azure Scheduler can be used for this because I feel any jobs that could run at the application level could be configured in Azure but not the user jobs.
Please validate and let me know if there are any resources which I could look at.
How about using Azure Web Jobs?
(https://learn.microsoft.com/en-us/azure/app-service/web-sites-create-web-jobs)
Can you please explain more about the nature of reports you're running?

Does the same job will run if web job run on multiple instance?

When I read the documentation for azure WebJobs, it found below statement For Continuous WebJobs to run reliably and on all instances refer to image below
My WebJob workflow:
Need to prepare the report for the newly created user in my application at 12 AM EST and send me the report in Email in daily occurrence.This time is changeable by UI so I need to run job continuously to find and run schedule at selected time
My Question
If WebJob runs all instance say two instances in running now for my web app.
Will I receive two email such that WebJob in each instance prepare the report and send to me?
Will get only one email irrespective of how many WebJobs are running?
A Continuous WebJob by default runs on all instances of your App Service Plan.
Whether your WebJob will run twice depends on how you implemented it. A Service Bus Queue listening WebJob will only run a queue message once, no matter how many instances (though if it fails, it'll run more than once).
If you wish, you can also make the WebJob run on a single instance by including a settings.job file in your WebJob with the following content:
{
"is_singleton": true
}

Microsoft Azure Worker Role or Azure Scheduler for Backup and Restore?

Creating Backup and restore Azure Table Storage Scheduler, where user will schedule the job or task to backup Azure Table at particular time, from Wizard. Which way I must Prefer?
Should I use Azure Service i.e Creating WebRole and Worker Role.
In this case how to Execute Worker Role on Schedule.
Can I use Web Job with Scheduler?
How to use Azure Scheduler to achive this task? I have googled out
where in some blogs they suggested to use Scheduler with Azure
Queue.
You can use either. Azure WebJobs are simpler and easier to use and would work well for your scenario.
You can use Azure WebJob with Azure Scheduler. You can set the schedule ahead of time where the scheduler will trigger the WebJob where you can perform your backup task.
Please see these articles
http://azure.microsoft.com/en-us/documentation/articles/websites-dotnet-deploy-webjobs/#configure
http://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/#CreateScheduled
I would use the Azure Scheduler. If your are familiar with a Cron job, this is very similar. All the Azure Scheduler does is make a REST call to an end point on a schedule. This would probably be more cost affective then setting up a worker role. Also, I would make sure to take into account security when setting up the Azure Scheduler. I would pass some type of secret to the API as a parameter so that it's not open to anonymous calls (although technically it would be.) If you want to execute certain tasks during the call, then I would store them in a table in your database. The worker role came before the Azure Scheduler, so the worker role lost in my book when the scheduler was made public.
UPDATE:
Another thought, is to look into the automation option in Azure: https://msdn.microsoft.com/library/azure/dn643629.aspx
With this option you can execute PowerShell scripts:
http://azure.microsoft.com/blog/2014/08/20/azure-automation-capabilities-in-depth-the-azure-automation-powershell-cmdlets/

how to write azure web role - worker role schedule task?

I am reaching a conflict in my azure project,
I'm trying to seclude task :
on every 16th of the month of .xml file form individual web site.
In my project I have one web role and one worker role,
I trying to seclude on every 16th of the month that the web role will create a message(connect to the site and download the .xml file), insert it to the queue storage, the worker role gets the message process it and delete the message.
Suggestion anyone??
Have you taken a look at this post - http://blog.smarx.com/posts/building-a-task-scheduler-in-windows-azure? It discusses one approach for building a task scheduler, which seems pretty much like what you're looking to do.
In your worker role you can start a timer or use this: http://quartznet.sourceforge.net/
An Azure web role is a windows server, and as such has a task scheduler built in, just like any other windows server. You can use a startup task to add a scheduled task to the task scheduler on a web or worker role which will then be fired on the 16th of every month.
However, keep in mind that if you have multiple web roles / worker roles, each one will have this scheduled task set up. So you'll need to have some way of knowing if another role has taken care of the work yet or not, or the scheduled task will run once for each web role.

Azure - how do I run a job that calls a function in the webservice every hour?

Im using Microsoft Azure and have a webservice and a SQL Azure db, I want to run a function every hour but not sure how to go about doing this?
I assume it has something to do with Azure Worker Roles, but not sure how to get the worker role to run and to call the webservice.
In the Run() method of either a Web Role or Worker Role, you could kick off a thread that sleeps until the top of the hour, wakes up, performs whatever task(s) you want, and goes back to sleep. Just remember that, when having multiple instances of a Web or Worker Role that's doing scheduling, you need to make sure only one of those instances is actually doing the scheduling. One way to accomplish that is to try leasing a blob, prior to starting the scheduler thread. If you lock it, go for it. If not, just recheck periodically. Eventually the instance that obtained the lock will release it when its instance recycles (which should happen at least once monthly).
Alternatively, you could place messages on a queue with visibilitytimeout set to a specific # of seconds correlating to some hourly time period. Then, each of your Web or Worker instances can periodically poll the queue for tasks to work on. The messages you push into the queue won't be visible to queue-readers until the visibility timeout period is reached.
A worker role runs constantly. In your worker role, you should:
Check whether or not the function has been run this hour. If so, do nothing.
If function has not been executed this hour, execute function.
In function, invoke web service and do your dirty work.
Check the SQL Azure Agent project and its references to a great articles by the SQL Azure Team.

Resources