-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOperationsJobFactory.cs
More file actions
62 lines (55 loc) · 2.41 KB
/
Copy pathFileOperationsJobFactory.cs
File metadata and controls
62 lines (55 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Scheduler
{
public class FileOperationsJobFactory : IJobFactory
{
public IJob CreateJob()
{
return new FileJob();
}
// This should be the actual work
private class FileJob : IJob
{
private Random random = new Random();
//public async Task RunAsync(IJobItem jobItem, CancellationToken token)
//{
// var fileJobItem = jobItem as FileJobItem;
// using (var fileStream = File.OpenWrite(fileJobItem.FilePath))
// {
// Console.WriteLine($"Writing in file [{fileJobItem.FilePath}]");
// var buffer = Encoding.UTF8.GetBytes($"Text for file {fileJobItem.FilePath}");
// for (var index = 0; index < 10000; index++)
// {
// await fileStream.WriteAsync(buffer, 0, buffer.Length, token);
// }
// Console.WriteLine($"Completed writing in file [{fileJobItem.FilePath}]");
// }
//}
// The stupid version when we create a thread instead of just using the file stream write task
public Task RunAsync(IJobItem jobItem, CancellationToken token)
{
return Task.Run(async () =>
{
var fileJobItem = jobItem as FileJobItem;
using (var fileStream = File.OpenWrite(fileJobItem.FilePath))
{
var buffer = Encoding.UTF8.GetBytes($"Text for file {fileJobItem.FilePath}{Environment.NewLine}");
// In order to add some randomness, we'll set randomly the number of lines that should be written
var noOfLines = random.Next(10000, 1000000);
Console.WriteLine($"Writing in file [{fileJobItem.FilePath}] {noOfLines} lines");
for (var index = 0; index < noOfLines; index++)
{
await fileStream.WriteAsync(buffer, 0, buffer.Length, token);
}
Console.WriteLine($"Completed writing in file [{fileJobItem.FilePath}]");
}
});
}
}
}
}