static void WriteErrorFile(string errorString) { if (jobDirectoryUri != null) { try { if (jobDirectoryUri.Scheme == "azureblob") { using (Azure.AzureDfsClient azureClient = new Azure.AzureDfsClient(jobDirectoryUri)) { azureClient.EnsureDirectory(jobDirectoryUri, false); Uri path = azureClient.Combine(jobDirectoryUri, "ppmError.txt"); azureClient.PutDfsFile(path, System.Text.Encoding.UTF8.GetBytes(errorString)); } } else if (jobDirectoryUri.Scheme == "hdfs") { using (Hdfs.HdfsClient hdfsClient = new Hdfs.HdfsClient(Environment.UserName)) { hdfsClient.EnsureDirectory(jobDirectoryUri, false); Uri path = hdfsClient.Combine(jobDirectoryUri, "ppmError.txt"); hdfsClient.PutDfsFile(path, System.Text.Encoding.UTF8.GetBytes(errorString)); } } } catch (Exception ee) { Console.Error.WriteLine("Got exception uploading error " + errorString + "\nException: " + ee.ToString()); } } }
static async Task HeartBeat(ILogger logger, PersistentProcessManager server, Task <int> exited, string jobName) { try { DateTime startTime = DateTime.UtcNow; if (jobDirectoryUri.Scheme != "azureblob") { logger.Log("Can't send heartbeat to non-Azure dfs " + jobDirectoryUri.AbsoluteUri); return; } string account, key, container, directoryName; Azure.Utils.FromAzureUri(jobDirectoryUri, out account, out key, out container, out directoryName); // get the full name of the blob from the job directory string heartbeatBlobName = directoryName + "heartbeat"; string killBlobName = directoryName + "kill"; using (Azure.AzureDfsClient client = new Azure.AzureDfsClient(account, key, container)) { CloudPageBlob killBlob = client.Container.GetPageBlobReference(killBlobName); CloudPageBlob heartbeatBlob = client.Container.GetPageBlobReference(heartbeatBlobName); await heartbeatBlob.CreateAsync(512); heartbeatBlob.Metadata["status"] = "running"; heartbeatBlob.Metadata["starttime"] = startTime.ToString(); if (jobName != null) { jobName = jobName.Replace('\r', ' ').Replace('\n', ' '); try { heartbeatBlob.Metadata["jobname"] = jobName; } catch (Exception e) { logger.Log("Got exception trying to set jobname metadata to '" + jobName + "': " + e.ToString()); heartbeatBlob.Metadata["jobname"] = "[got exception setting job name]"; } } while (true) { logger.Log("Uploading heartbeat properties"); heartbeatBlob.Metadata["heartbeat"] = DateTime.UtcNow.ToString(); await heartbeatBlob.SetMetadataAsync(); logger.Log("Heartbeat sleeping"); Task <int> t = await Task.WhenAny(exited, Task.Delay(1000).ContinueWith((d) => { return(259); })); int exitCode = t.Result; string status = null; if (t == exited) { status = (exitCode == 0) ? "success" : "failure"; } else if (killBlob.Exists()) { exitCode = 1; status = "killed"; server.TriggerFullShutdown(1, "job was cancelled"); } if (status != null) { logger.Log("Uploading final heartbeat properties " + exitCode + " " + status); heartbeatBlob.Metadata["status"] = status; await heartbeatBlob.SetMetadataAsync(); logger.Log("Heartbeat exiting"); return; } } } } catch (Exception e) { logger.Log("Heartbeat got exception " + e.ToString()); } }