Last active
August 29, 2015 14:02
-
-
Save Dillie-O/1667f71ffa4c082770bd to your computer and use it in GitHub Desktop.
Azure WebJob Import Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Configuration; | |
| using System.IO; | |
| using Microsoft.WindowsAzure.Jobs; | |
| namespace dataimportwebjob | |
| { | |
| partial class Program | |
| { | |
| public static JobHost Host; | |
| static void Main() | |
| { | |
| var path = ConfigurationManager.AppSettings["DirectoryBaseToWatch"]; | |
| var folder = ConfigurationManager.AppSettings["DirectoryFolderToWatch"]; | |
| var directories = Directory.GetDirectories(path, folder); | |
| var fsw = new FileSystemWatcher[directories.Length]; | |
| Console.WriteLine("Setting up monitoring for path: " + path + folder); | |
| for (var i = 0; i < directories.Length; i++) | |
| { | |
| fsw[i] = new FileSystemWatcher(directories[i]) | |
| { | |
| NotifyFilter = | |
| NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName | | |
| NotifyFilters.Size | |
| }; | |
| fsw[i].Created += fsw_Created; | |
| fsw[i].IncludeSubdirectories = true; | |
| fsw[i].EnableRaisingEvents = true; | |
| } | |
| Host = new JobHost(); | |
| Host.RunAndBlock(); | |
| } | |
| static void fsw_Created(object sender, FileSystemEventArgs e) | |
| { | |
| Console.WriteLine("File Detected: " + e.FullPath); | |
| var deleteAfterUpload = bool.Parse(ConfigurationManager.AppSettings["DeleteAfterUpload"]); | |
| while (!IsFileReady(e.FullPath)) | |
| System.Threading.Thread.Sleep(1000); | |
| UploadFileToBlob(e.Name, e.FullPath, deleteAfterUpload); | |
| } | |
| public static void ProcessImportFile([BlobInput("importjobsdata/{name}")] TextReader input, | |
| string name, | |
| [BlobOutput("importjobslog/log_{name}")] TextWriter writer) | |
| { | |
| writer.WriteLine("Starting import file process..."); | |
| var result = InputData(input, writer); | |
| var status = result == 0 ? "SUCCESS" : "FAIL"; | |
| var message = result == 0 | |
| ? "Import success." | |
| : "Import fail. " + result + " records failed to import. Check logs for details."; | |
| writer.WriteLine(message); | |
| // Write entry to ImportEntry table for use by web application. | |
| using (var db = new TrackingModels.TrackingContext()) | |
| { | |
| var entry = new TrackingModels.ImportEntry | |
| { | |
| Status = status, | |
| FileName = name, | |
| Message = message, | |
| CreatedAt = DateTime.Now | |
| }; | |
| db.ImportEntries.Add(entry); | |
| db.SaveChanges(); | |
| } | |
| writer.WriteLine("Import file process complete."); | |
| } | |
| public static void UploadFileToBlob(string name, string path, bool deleteAfterUpload) | |
| { | |
| var method = typeof(Program).GetMethod("Upload"); | |
| Host.Call(method, new { name = name, | |
| path = path, | |
| deleteAfterUpload = deleteAfterUpload | |
| }); | |
| } | |
| public static void Upload(string name, string path, // Local file | |
| [BlobOutput("importjobsdata/{name}")] Stream output, | |
| bool deleteAfterUpload) | |
| { | |
| using (var fileStream = File.OpenRead(path)) | |
| { | |
| fileStream.CopyTo(output); | |
| } | |
| if (deleteAfterUpload) | |
| { | |
| File.Delete(path); | |
| } | |
| } | |
| public static bool IsFileReady(String sFilename) | |
| { | |
| try | |
| { | |
| using (var fileStream = File.Open(sFilename, FileMode.Open, FileAccess.Read, FileShare.None)) | |
| { | |
| if (fileStream.Length > 0) | |
| return true; | |
| else | |
| return false; | |
| } | |
| } | |
| catch (Exception) | |
| { | |
| return false; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment