Since Windows Azure doesn’t have SQL Agent you’ll have to create your own worker role to work like a SQL Agent in order to schedule the running of an stored procedure.
Well I’m all set with this part except of the part with the dates. This should be very logic, but it doesn’t seems so to me. So how do I check if today is the first day of the month and the first day of the week.
This is what I have so far:
Trace.WriteLine("SqlWorkerRole entry point called", "Information");
while (true)
{
DateTime firstDayOfMonth = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1, 0, 0, 0);
if (DateTime.UtcNow > firstDayOfMonth)
{
Guid? jobId = StartJob("MonthJob");
if (jobId.HasValue)
{
Trace.WriteLine("Working", "Information");
ExecuteMonthJob();
StopJob(jobId.Value);
}
}
else
{
Thread.Sleep(60000);
}
DateTime firstDayOfWeek = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1, 0, 0, 0);
if (DateTime.UtcNow > firstDayOfWeek)
{
Guid? jobId = StartJob("WeekJob");
if (jobId.HasValue)
{
Trace.WriteLine("Working", "Information");
ExecuteWeekJob();
StopJob(jobId.Value);
}
}
else
{
Thread.Sleep(60000);
}
}
I need help with this parts (first day of month)
DateTime firstDayOfMonth = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1, 0, 0, 0);
if (DateTime.UtcNow > firstDayOfMonth)
and (first day of week)
DateTime firstDayOfWeek = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1, 0, 0, 0);
if (DateTime.UtcNow > firstDayOfWeek)
Well, you could as an alternative use Enzo Cloud Backup Community Edition (free software). It has a scheduler built-in (that requires a x-small worler role) and you can schedule T-SQL calls. The scheduler can do weekly, but at this time it doesn’t do monthly; still it would be trivial in the stored proc itself to add logic to determine if your proc ran this month or not.