How to ensure the SharePoint 2010 workflows end in given period

Many question has been asked about this problem.

in SharePoint Best Practices;

* For performance consideration ; you have to plan your timer jobs will always finish on time.
* You need to choose appropriate schedule interval to do it.

But this is not suitable for every condition ; Let me give some information about the issue.

You have planing to develop a custom timer job  for example this job has responsible to get some remote data from a remote System in 1 minute interval .
And what if the remote operation has taken more than 60sec. or what if that the timer job has faced with some problems and not getting finished itsjob in given period. You got the idea ūüôā

In some conditions ; you may need to ensure no matter what ,the timer job has to be finished its job in given period . and If doesnt , you may want to terminate the running instance of the timer job and start a new instance of it.

The problem is here ;

If a timer has over its period and continue to running ; After the period has elapsed the new instance wont be started by  default behaviour of the SharePoint.
In SharePoint there is no way to run  two instance of a same timer job definition at same time

And even you have implemented some control logic in that timer job which responsible for the check and terminate if previous instance has running to stop it. It doesnt work because the timer job code wont be run again so it is never step through the SPTimerJob.Execute() method one more time.

And there is no way in OOB configuration to provide this functionality.

Resolution;

Resolution of this problem is easy but tricky. First you need to have 2 timer job.

EnsureTimerJobFinished

1)  Controler Timer job: (Scheduled) Has responsible to run worker timer job in defined interval and responsible to timing. (if the worker timer job has still running it can kill the previous one and start a new instance)

2)  Worker Timer Job. (Once) : Does the execute actual operation.

3)  If the Controler has detected that the previous instance of the worker timer job still running. It can terminate it. (Or you can extend your solution and make your own decision here)

4) After termination , The Controller can run another instance of the worker timer job.

Advertisements

Getting Last Accessed user for a SharePoint Site

Assume that you would like to get last accessed users for a specific site or web in SharePoint. The correct approch is creating a custom IIS Module to get update statistics by every request. If you dont want to do that there you may alternatively use following method.

But we have some conditions:
1) Usage and Health Data Collection Service Application is running.
2) And Microsoft SharePoint Foundation Usage Data Import job running in a small interval (one hour maybe). (Because the data first stores in a Usage file in file System and when the timer job has been executed that data transfered to you Logging Database) Thats mean it is not real time. (If you would like a statistics in real time i suggest that use a custom IIS module)

Than you can search the last 5 accessed user from SQL Server Management Studio by quering Logging Database Server (It would be delayed by interval of Import Timer Job)

Declare @serUrl as nvarchar(MAX), @sitUrl as nvarchar(MAX) , @wUrl as nvarchar(MAX)
Declare webcursor CURSOR FOR select serverurl,siteurl,weburl from RequestUsage group by ServerUrl,SiteUrl,WebUrl
Open webcursor

FETCH NEXT FROM webcursor INTO @serUrl,@sitUrl,@wUrl
WHILE @@FETCH_STATUS = 0 
BEGIN
PRint 'ServerUrl: ' + @serUrl + ' Site Url:' + @sitUrl +  ' Web Url:' + @wUrl
select Top 5 LogTime, UserLogin,ServerUrl,SiteUrl,WebUrl from RequestUsage 
where ServerUrl = @serUrl and SiteUrl = @sitUrl and WebUrl =  @wUrl
--Dont Forget to Exclude SharePoint\system , Central Administration Web Site, Crawler Account, And Timer Service Account for correct results.
and ServerUrl <> 'http://contoso.com:32843'  and UserLogin <> 'SHAREPOINT\system' and UserLogin<> 'Contoso\Crawler'
order by LogTime desc 
FETCH NEXT FROM webcursor INTO @serUrl,@sitUrl,@wUrl
END
CLOSE webcursor
DEALLOCATE webcursor

how to delete sharepoint timer job using powershell

When developing custom sharepoint timer jobs sometimes you want to delete junk jobs created by deployment attempts .

Warning !!! DO NOT DELETE ANY TIMER JOB UNLESS BE SURE

Searching listing and find timer job to delete.We need an id.
Get-SPTimerJob | where { $_.name -like “*<your search criteria>*” } |ft id,name

Set job to a variable
$job = Get-SPTimerJob -id <GUID>

And delete it.
$job.Delete()

just it.