Skip to content

Commit

Permalink
Revert "refactor: Jobs 이름 변경 (#78)" (#79)
Browse files Browse the repository at this point in the history
This reverts commit 595ac5b.
  • Loading branch information
sunghwan2789 authored Aug 9, 2020
1 parent 595ac5b commit 437c09c
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 98 deletions.
8 changes: 4 additions & 4 deletions Bible2PPT/Data/BibleContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Data.Entity;
using Bible2PPT.Bibles;
using Bible2PPT.Jobs;
using Bible2PPT.PPT;
using SQLite.CodeFirst;

namespace Bible2PPT.Data
Expand All @@ -11,7 +11,7 @@ class BibleContext : DbContext
public DbSet<Book> Books { get; set; }
public DbSet<Chapter> Chapters { get; set; }
public DbSet<Verse> Verses { get; set; }
public DbSet<BibleJob> Jobs { get; set; }
public DbSet<Job> Jobs { get; set; }

public BibleContext() : this("Data Source=:memory:")
{
Expand Down Expand Up @@ -53,8 +53,8 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
modelBuilder.Entity<Verse>().HasIndex(e => e.SourceId);
modelBuilder.Entity<Verse>().HasIndex(e => e.ChapterId);

modelBuilder.Entity<BibleJobBible>().HasRequired(e => e.Job).WithMany(e => e.JobBibles);
modelBuilder.Entity<BibleJobBible>().HasRequired(e => e.Bible).WithMany();
modelBuilder.Entity<JobBible>().HasRequired(e => e.Job).WithMany(e => e.JobBibles);
modelBuilder.Entity<JobBible>().HasRequired(e => e.Bible).WithMany();
}
}
}
4 changes: 2 additions & 2 deletions Bible2PPT/MainForm.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using Bible2PPT.Bibles;
using Bible2PPT.Data;
using Bible2PPT.Extensions;
using Bible2PPT.Jobs;
using Bible2PPT.PPT;
using Bible2PPT.Sources;
using Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -505,7 +505,7 @@ private void BuildButton_Click(object sender, EventArgs e)
destination = $"{Path.GetTempFileName()}.pptx";
}

var job = new BibleJob
var job = new Job
{
Bibles = biblesToBuild.ToList(),
CreatedAt = DateTime.Now,
Expand Down
20 changes: 10 additions & 10 deletions Bible2PPT/MainForm.History.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
using System.Windows.Forms;
using Bible2PPT.Bibles;
using Bible2PPT.Data;
using Bible2PPT.Jobs;
using Bible2PPT.PPT;
using Microsoft.Extensions.DependencyInjection;

namespace Bible2PPT
{
partial class MainForm
{
private readonly BindingList<BibleJob> jobHistory = new BindingList<BibleJob>();
private readonly BindingList<Job> jobHistory = new BindingList<Job>();

private void InitializeHistoryComponent()
{
historyDataGridView.AutoGenerateColumns = false;
historyCreatedAtColumn.DataPropertyName = nameof(BibleJob.CreatedAt);
historyBiblesColumn.DataPropertyName = nameof(BibleJob.Bibles);
historyQueryStringColumn.DataPropertyName = nameof(BibleJob.QueryString);
historySplitChaptersIntoFileColumn.DataPropertyName = nameof(BibleJob.SplitChaptersIntoFiles);
historyCreatedAtColumn.DataPropertyName = nameof(Job.CreatedAt);
historyBiblesColumn.DataPropertyName = nameof(Job.Bibles);
historyQueryStringColumn.DataPropertyName = nameof(Job.QueryString);
historySplitChaptersIntoFileColumn.DataPropertyName = nameof(Job.SplitChaptersIntoFiles);

using (var scope = ScopeFactory.CreateScope())
{
Expand All @@ -40,7 +40,7 @@ private void InitializeHistoryComponent()
Builder.JobProgress = new Progress<EventArgs>(Builder_JobProgressChanged);
}

private DataGridViewRow FindHistoryDataGridViewRow(BibleJob job)
private DataGridViewRow FindHistoryDataGridViewRow(Job job)
{
foreach (DataGridViewRow row in historyDataGridView.Rows)
{
Expand Down Expand Up @@ -136,7 +136,7 @@ private void AutoOpenCheckBox_CheckedChanged(object sender, EventArgs e)
private void HistoryOpenResultButton_Click(object sender, EventArgs e)
{
// 선택한 기록이 없으면 아무 작업도 안함
if (!(historyDataGridView.CurrentRow?.DataBoundItem is BibleJob job))
if (!(historyDataGridView.CurrentRow?.DataBoundItem is Job job))
{
return;
}
Expand Down Expand Up @@ -169,7 +169,7 @@ private void HistoryOpenResultButton_Click(object sender, EventArgs e)

private void HistoryLoadButton_Click(object sender, EventArgs e)
{
if (!(historyDataGridView.CurrentRow?.DataBoundItem is BibleJob job))
if (!(historyDataGridView.CurrentRow?.DataBoundItem is Job job))
{
return;
}
Expand All @@ -193,7 +193,7 @@ private void HistoryLoadButton_Click(object sender, EventArgs e)

private void HistoryDeleteButton_Click(object sender, EventArgs e)
{
if (!(historyDataGridView.CurrentRow?.DataBoundItem is BibleJob job))
if (!(historyDataGridView.CurrentRow?.DataBoundItem is Job job))
{
return;
}
Expand Down
6 changes: 3 additions & 3 deletions Bible2PPT/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
using System.Threading;
using System.Windows.Forms;
using Bible2PPT.Controls;
using Bible2PPT.Jobs;
using Bible2PPT.PPT;
using Bible2PPT.Services;
using Microsoft.Extensions.DependencyInjection;

namespace Bible2PPT
{
internal partial class MainForm : AssemblyIconForm
{
private BibleJobWorker Builder { get; }
private Builder Builder { get; }
private BibleService BibleService { get; }
private IServiceScopeFactory ScopeFactory { get; }

private readonly Dictionary<int, CancellationTokenSource> workCts = new Dictionary<int, CancellationTokenSource>();

public MainForm(BibleJobWorker builder, BibleService bibleService, IServiceScopeFactory scopeFactory)
public MainForm(Builder builder, BibleService bibleService, IServiceScopeFactory scopeFactory)
{
Builder = builder;
BibleService = bibleService;
Expand Down
10 changes: 5 additions & 5 deletions Bible2PPT/Jobs/BibleJobWorker.cs → Bible2PPT/PPT/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
using Microsoft.Extensions.DependencyInjection;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;

namespace Bible2PPT.Jobs
namespace Bible2PPT.PPT
{
class BibleJobWorker : JobManager
class Builder : JobManager
{
private HiddenPowerPoint PowerPoint { get; }
private ZippedBibleService ZippedBibleService { get; }
private IServiceScopeFactory ScopeFactory { get; }

public BibleJobWorker(HiddenPowerPoint ppt, ZippedBibleService bibleService, IServiceScopeFactory scopeFactory)
public Builder(HiddenPowerPoint ppt, ZippedBibleService bibleService, IServiceScopeFactory scopeFactory)
{
PowerPoint = ppt;
ZippedBibleService = bibleService;
Expand Down Expand Up @@ -47,7 +47,7 @@ void ExtractTemplate()
});
}

protected override async Task ProcessAsync(BibleJob job, CancellationToken token)
protected override async Task ProcessAsync(Job job, CancellationToken token)
{
await base.ProcessAsync(job, token).ConfigureAwait(false);

Expand Down Expand Up @@ -174,7 +174,7 @@ private static void CreateDirectoryIfNotExists(string path)
}
}

public new void Queue(BibleJob job)
public new void Queue(Job job)
{
using (var scope = ScopeFactory.CreateScope())
{
Expand Down
18 changes: 4 additions & 14 deletions Bible2PPT/Jobs/BibleJob.cs → Bible2PPT/PPT/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
using System.Linq;
using Bible2PPT.Bibles;

namespace Bible2PPT.Jobs
namespace Bible2PPT.PPT
{
internal class BibleJob
internal class Job
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

public virtual ICollection<BibleJobBible> JobBibles { get; set; }
public virtual ICollection<JobBible> JobBibles { get; set; }

[NotMapped]
public List<Bible> Bibles
{
get => JobBibles.Select(i => i.Bible).ToList();
set => JobBibles = value.Select(i => new BibleJobBible { Bible = i, }).ToList();
set => JobBibles = value.Select(i => new JobBible { Bible = i, }).ToList();
}

public TemplateTextOptions TemplateBookNameOption { get; set; }
Expand All @@ -29,14 +29,4 @@ public List<Bible> Bibles
public string OutputDestination { get; set; }
public DateTime CreatedAt { get; set; }
}

class BibleJobBible
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

public virtual BibleJob Job { get; set; }

public virtual Bible Bible { get; set; }
}
}
15 changes: 15 additions & 0 deletions Bible2PPT/PPT/JobBible.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations.Schema;
using Bible2PPT.Bibles;

namespace Bible2PPT.PPT
{
class JobBible
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

public virtual Job Job { get; set; }

public virtual Bible Bible { get; set; }
}
}
18 changes: 18 additions & 0 deletions Bible2PPT/PPT/JobCompletedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace Bible2PPT.PPT
{
class JobCompletedEventArgs : EventArgs
{
public Job Job { get; }
public Exception Exception { get; }
public bool IsFaulted => Exception != null;
public bool IsCancelled => Exception is OperationCanceledException;

public JobCompletedEventArgs(Job job, Exception ex)
{
Job = job;
Exception = ex;
}
}
}
60 changes: 5 additions & 55 deletions Bible2PPT/Jobs/JobManager.cs → Bible2PPT/PPT/JobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Bible2PPT.Bibles;
using Bible2PPT.Extensions;

namespace Bible2PPT.Jobs
namespace Bible2PPT.PPT
{
class JobManager : IDisposable
{
Expand All @@ -18,10 +17,10 @@ class JobManager : IDisposable


private readonly SemaphoreSlim Semaphore = new SemaphoreSlim(1, 1);
private readonly ConcurrentDictionary<BibleJob, CancellationTokenSource> JobCancellations = new ConcurrentDictionary<BibleJob, CancellationTokenSource>();
private readonly ConcurrentDictionary<Job, CancellationTokenSource> JobCancellations = new ConcurrentDictionary<Job, CancellationTokenSource>();

[SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "just log")]
public void Queue(BibleJob job)
public void Queue(Job job)
{
JobCancellations[job] = new CancellationTokenSource();
OnJobQueued(new JobQueuedEventArgs(job));
Expand Down Expand Up @@ -52,15 +51,15 @@ public void Queue(BibleJob job)
});
}

public void Cancel(BibleJob job)
public void Cancel(Job job)
{
if (JobCancellations.TryGetValue(job, out var cts))
{
cts.CancelIfNotDisposed();
}
}

protected virtual Task ProcessAsync(BibleJob job, CancellationToken token)
protected virtual Task ProcessAsync(Job job, CancellationToken token)
{
token.ThrowIfCancellationRequested();
return Task.CompletedTask;
Expand Down Expand Up @@ -107,53 +106,4 @@ public void Dispose()
}
#endregion
}

class JobQueuedEventArgs : EventArgs
{
public BibleJob Job { get; }

public JobQueuedEventArgs(BibleJob job)
{
Job = job;
}
}

class JobProgressEventArgs : EventArgs
{
public BibleJob Job { get; }
public Chapter CurrentChapter { get; }

public int QueriesDone { get; }
public int Queries { get; }
public int ChaptersDone { get; }
public int Chapters { get; }

public double Progress => (Chapters == 0)
? (double)QueriesDone / Queries
: (double)Math.Min(QueriesDone + 1, Queries) / Queries * ChaptersDone / Chapters;

public JobProgressEventArgs(BibleJob job, Chapter currentChapter, int queriesDone, int queries, int chaptersDone, int chapters)
{
Job = job;
CurrentChapter = currentChapter;
QueriesDone = queriesDone;
Queries = queries;
ChaptersDone = chaptersDone;
Chapters = chapters;
}
}

class JobCompletedEventArgs : EventArgs
{
public BibleJob Job { get; }
public Exception Exception { get; }
public bool IsFaulted => Exception != null;
public bool IsCancelled => Exception is OperationCanceledException;

public JobCompletedEventArgs(BibleJob job, Exception ex)
{
Job = job;
Exception = ex;
}
}
}
30 changes: 30 additions & 0 deletions Bible2PPT/PPT/JobProgressEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using Bible2PPT.Bibles;

namespace Bible2PPT.PPT
{
class JobProgressEventArgs : EventArgs
{
public Job Job { get; }
public Chapter CurrentChapter { get; }

public int QueriesDone { get; }
public int Queries { get; }
public int ChaptersDone { get; }
public int Chapters { get; }

public double Progress => (Chapters == 0)
? (double)QueriesDone / Queries
: (double)Math.Min(QueriesDone + 1, Queries) / Queries * ChaptersDone / Chapters;

public JobProgressEventArgs(Job job, Chapter currentChapter, int queriesDone, int queries, int chaptersDone, int chapters)
{
Job = job;
CurrentChapter = currentChapter;
QueriesDone = queriesDone;
Queries = queries;
ChaptersDone = chaptersDone;
Chapters = chapters;
}
}
}
14 changes: 14 additions & 0 deletions Bible2PPT/PPT/JobQueuedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Bible2PPT.PPT
{
class JobQueuedEventArgs : EventArgs
{
public Job Job { get; }

public JobQueuedEventArgs(Job job)
{
Job = job;
}
}
}
Loading

0 comments on commit 437c09c

Please sign in to comment.