public JobInfo NextJob(string preferredStoreId) { using (var conn = new SqlConnection(_connectionString)) { conn.Open(); try { using (var cmd = conn.CreateCommand()) { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "Brightstar_NextJob"; cmd.Parameters.AddWithValue("storeId", String.IsNullOrEmpty(preferredStoreId) ? (object) DBNull.Value : preferredStoreId); cmd.Parameters.AddWithValue("workerId", _workerId); using (var reader = cmd.ExecuteReader()) { JobInfo ret = null; if (reader.Read()) { var jobId = reader.GetString(0); var storeId = reader.GetString(1); ret = new JobInfo(jobId, storeId) { JobType = (JobType) reader.GetInt32(2), Status = (JobStatus) reader.GetInt32(3), Data = reader.IsDBNull(4) ? null : reader.GetString(4), RetryCount = reader.GetInt32(5) }; } return ret; } } } finally { conn.Close(); } } }
private void LogJob(string jobId) { try { JobInfo jobInfo = null; using (var conn = new SqlConnection(_connectionString)) { conn.Open(); try { using (var cmd = conn.CreateCommand()) { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "Brightstar_GetJobDetail"; cmd.Parameters.AddWithValue("jobId", jobId); using (var reader = cmd.ExecuteReader()) { if (reader.Read()) { jobInfo = new JobInfo(reader.GetString(0), reader.GetString(1)); jobInfo.JobType = (JobType) reader.GetInt32(2); jobInfo.Status = (JobStatus) reader.GetInt32(3); jobInfo.StatusMessage = reader.IsDBNull(4) ? String.Empty : reader.GetString(4); jobInfo.ScheduledRunTime = reader.IsDBNull(5) ? (DateTime?) null : reader.GetDateTime(5); jobInfo.StartTime = reader.IsDBNull(6) ? (DateTime?) null : reader.GetDateTime(6); jobInfo.ProcessorId = reader.IsDBNull(7) ? String.Empty : reader.GetString(7); jobInfo.ProcessingCompleted = reader.IsDBNull(8) ? (DateTime?) null : reader.GetDateTime(8); jobInfo.ProcessingException = reader.IsDBNull(9) ? String.Empty : reader.GetString(9); jobInfo.RetryCount = reader.GetInt32(10); } } } } catch (Exception ex) { Trace.TraceError("Error retrieving job detail for job {0}. Cause: {1}", jobId, ex); } finally { conn.Close(); } } if (jobInfo != null) { var connectionString = RoleEnvironment.GetConfigurationSettingValue(AzureConstants.DiagnosticsConnectionStringName); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); tableClient.CreateTableIfNotExist("jobs"); TableServiceContext serviceContext = tableClient.GetDataServiceContext(); JobLogEntity jobLogEntity = new JobLogEntity(jobInfo); serviceContext.AddObject("jobs", jobLogEntity); serviceContext.SaveChangesWithRetries(); } } catch (Exception ex) { Trace.TraceError("Error logging detail for job {0}. Cause: {1}", jobId, ex); } }
/// <summary> /// Returns the info for the last job that committed to the specified store /// </summary> /// <param name="storeId"></param> /// <returns></returns> public JobInfo GetLastCommit(string storeId) { using (var conn = new SqlConnection(_connectionString)) { conn.Open(); try { using (var cmd = conn.CreateCommand()) { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "Brightstar_GetLastCommit"; cmd.Parameters.AddWithValue("storeId", storeId); using (var reader = cmd.ExecuteReader()) { JobInfo ret = null; if (reader.Read()) { var jobId = reader.GetString(0); ret = new JobInfo(jobId, storeId) { JobType = (JobType) reader.GetInt32(2), Status = (JobStatus) reader.GetInt32(3), StatusMessage = reader.IsDBNull(4) ? String.Empty : reader.GetString(4), }; if (!reader.IsDBNull(5)) ret.ProcessingCompleted = reader.GetDateTime(5); } return ret; } } } finally { conn.Close(); } } }
private void ProcessJob(JobInfo jobInfo) { switch (jobInfo.JobType) { case JobType.Transaction: { try { Trace.TraceInformation("ProcessJob: Starting processing on Transaction job {0}", jobInfo.Id); var storeManager = GetStoreManager(); var worker = new Server.StoreWorker(jobInfo.StoreId, storeManager); var transactionData = jobInfo.Data.Split(new string[] { AzureConstants.TransactionSeparator }, StringSplitOptions.None); var update = new UpdateTransaction(Guid.Parse(jobInfo.Id), worker, transactionData[0], transactionData[1], transactionData[2]); update.Run(); _jobQueue.CompleteJob(jobInfo.Id, JobStatus.CompletedOk, "Transaction completed successfully"); } catch (Exception ex) { Trace.TraceError("ProcessJob: Transaction job failed with exception {0}", ex); _jobQueue.CompleteJob(jobInfo.Id, JobStatus.CompletedWithErrors, String.Format("Transaction failed: {0}", ex)); } break; } case JobType.Import: { try { Trace.TraceInformation("ProcessJob: Starting processing on Import job {0}", jobInfo.Id); var storeManager = GetStoreManager(); var worker = new Server.StoreWorker(jobInfo.StoreId, storeManager); BlobImportSource importSource; if (!TryDeserialize(jobInfo.Data, out importSource)) { importSource = new BlobImportSource { BlobUri = jobInfo.Data }; } var import = new AzureImportJob(jobInfo.Id, worker, importSource, (jobId, statusMessage) => _jobQueue.UpdateStatus(jobId, statusMessage)); import.Run(); if (import.Errors) { Trace.TraceError("ProcessJob: Import job {0} failed with error: {1}. Marking job as CompletedWithErrors", jobInfo.Id, import.ErrorMessage); _jobQueue.CompleteJob(jobInfo.Id, JobStatus.CompletedWithErrors, import.ErrorMessage); } else { _jobQueue.CompleteJob(jobInfo.Id, JobStatus.CompletedOk, "Import completed successfully"); } } catch (Exception ex) { Trace.TraceError("ProcessJob: Import job {0} failed with exception {1}", jobInfo.Id, ex); _jobQueue.FailWithException(jobInfo.Id, "Import failed", ex); } break; } case JobType.Export: { try { Trace.TraceInformation("ProcessJob: Starting processing on Export job {0}", jobInfo.Id); var storeManager = GetStoreManager(); var worker = new Server.StoreWorker(jobInfo.StoreId, storeManager); BlobImportSource exportSource; if (!TryDeserialize(jobInfo.Data, out exportSource)) { exportSource = new BlobImportSource { BlobUri = jobInfo.Data }; } var export = new AzureExportJob(jobInfo.Id, worker, exportSource); export.Run(); _jobQueue.CompleteJob(jobInfo.Id, JobStatus.CompletedOk, "Export completed successfully"); } catch (Exception ex) { Trace.TraceError("ProcessJob: Export job {0} failed with exception {1}", jobInfo.Id, ex); _jobQueue.FailWithException(jobInfo.Id, "Export failed", ex); } break; } case JobType.DeleteStore: { try { Trace.TraceInformation("ProcessJob: Starting processing on Delete job {0}", jobInfo.Id); var storeManager = GetStoreManager(); storeManager.DeleteStore(jobInfo.StoreId); _jobQueue.CompleteJob(jobInfo.Id, JobStatus.CompletedOk, "Store deleted."); } catch (Exception ex) { Trace.TraceError("ProcessJob: DeleteStore {0} failed with exception {1}", jobInfo.StoreId, ex); _jobQueue.FailWithException(jobInfo.Id, "Delete failed", ex); } break; } default: // TODO: Implement me Thread.Sleep(1000); _jobQueue.UpdateStatus(jobInfo.Id, "20% Complete"); Thread.Sleep(1000); _jobQueue.UpdateStatus(jobInfo.Id, "40% Complete"); Thread.Sleep(1000); _jobQueue.UpdateStatus(jobInfo.Id, "60% Complete"); Thread.Sleep(1000); _jobQueue.UpdateStatus(jobInfo.Id, "80% Complete"); Thread.Sleep(1000); _jobQueue.CompleteJob(jobInfo.Id, JobStatus.CompletedOk, "Completed without any processing"); break; } }
// Not currently used //public IEnumerable<JobInfo> GetActiveJobs() //{ // using (var conn = new SqlConnection(_connectionString)) // { // conn.Open(); // try // { // using (var cmd = conn.CreateCommand()) // { // cmd.CommandType = CommandType.StoredProcedure; // cmd.CommandText = "GetActiveJobs"; // cmd.Parameters.AddWithValue("workerId", _workerId); // using (var reader = cmd.ExecuteReader()) // { // while (reader.Read()) // { // yield return new JobInfo(reader.GetString(0), reader.GetString(1)) // { // JobType = (JobType) reader.GetInt32(2), // Status = (JobStatus) reader.GetInt32(3), // StatusMessage = // reader.IsDBNull(4) ? String.Empty : reader.GetString(4), // RetryCount = reader.GetInt32(5) // }; // } // } // } // } // finally // { // conn.Close(); // } // } //} public JobInfo GetJob(string storeId, string jobId) { using (var conn = new SqlConnection(_connectionString)) { conn.Open(); try { using (var cmd = conn.CreateCommand()) { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "GetJob"; cmd.Parameters.AddWithValue("jobId", jobId); cmd.Parameters.AddWithValue("storeId", storeId); using (var reader = cmd.ExecuteReader()) { JobInfo ret = null; if (reader.Read()) { ret = new JobInfo(jobId, storeId) { JobType = (JobType) reader.GetInt32(2), Status = (JobStatus) reader.GetInt32(3), StatusMessage = reader.IsDBNull(4) ? String.Empty : reader.GetString(4) }; } return ret; } } } finally { conn.Close(); } } }
private void LogJob(string jobId) { try { JobInfo jobInfo = null; using (var conn = new SqlConnection(_connectionString)) { conn.Open(); try { using (var cmd = conn.CreateCommand()) { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "GetJobDetail"; cmd.Parameters.AddWithValue("jobId", jobId); using (var reader = cmd.ExecuteReader()) { if (reader.Read()) { jobInfo = new JobInfo(reader.GetString(0), reader.GetString(1)); jobInfo.JobType = (JobType)reader.GetInt32(2); jobInfo.Status = (JobStatus)reader.GetInt32(3); jobInfo.StatusMessage = reader.IsDBNull(4) ? String.Empty : reader.GetString(4); jobInfo.ScheduledRunTime = reader.IsDBNull(5) ? (DateTime?)null : reader.GetDateTime(5); jobInfo.StartTime = reader.IsDBNull(6) ? (DateTime?)null : reader.GetDateTime(6); jobInfo.ProcessorId = reader.IsDBNull(7) ? String.Empty : reader.GetString(7); jobInfo.ProcessingCompleted = reader.IsDBNull(8) ? (DateTime?)null : reader.GetDateTime(8); jobInfo.ProcessingException = reader.IsDBNull(9) ? String.Empty : reader.GetString(9); jobInfo.RetryCount = reader.GetInt32(10); } } } } catch (Exception ex) { Trace.TraceError("Error retrieving job detail for job {0}. Cause: {1}", jobId, ex); } finally { conn.Close(); } } if (jobInfo != null) { var connectionString = RoleEnvironment.GetConfigurationSettingValue(AzureConstants.DiagnosticsConnectionStringName); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); tableClient.CreateTableIfNotExist("jobs"); TableServiceContext serviceContext = tableClient.GetDataServiceContext(); JobLogEntity jobLogEntity = new JobLogEntity(jobInfo); serviceContext.AddObject("jobs", jobLogEntity); serviceContext.SaveChangesWithRetries(); } } catch (Exception ex) { Trace.TraceError("Error logging detail for job {0}. Cause: {1}", jobId, ex); } }