Skip to content

Commit

Permalink
Improving the logging a bit and doing some code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
renemadsen committed Nov 14, 2024
1 parent 84f8e92 commit 6993d2d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
5 changes: 4 additions & 1 deletion ServiceItemsPlanningPlugin/Handlers/PushMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ public PushMessageHandler(eFormCore.Core sdkCore, DbContextHelper dbContextHelpe

public async Task Handle(PushMessage message)
{
Console.WriteLine("ItemsPlanningService PushMessageHandler.Handle : PushMessage");
Planning planning = await _dbContext.Plannings.FirstOrDefaultAsync(x => x.Id == message.PlanningId);
if (planning != null)
{
Console.WriteLine("ItemsPlanningService PushMessageHandler.Handle : Planning found in DB : " + planning.Id);
await using MicrotingDbContext microtingDbContext = _sdkCore.DbContextHelper.GetDbContext();
List<PlanningSite> planningSites =
await _dbContext.PlanningSites.Where(x => x.PlanningId == message.PlanningId && x.WorkflowState != Constants.WorkflowStates.Removed).ToListAsync();
Expand All @@ -64,6 +66,7 @@ public async Task Handle(PushMessage message)
Site site = await microtingDbContext.Sites.FirstOrDefaultAsync(x => x.Id == planningSite.SiteId);
if (site != null)
{
Console.WriteLine($"ItemsPlanningService PushMessageHandler.Handle : Site found in DB : {site.Id} - {site.Name}");
PlanningNameTranslation planningNameTranslation =
await _dbContext.PlanningNameTranslation.FirstAsync(x =>
x.PlanningId == planning.Id
Expand All @@ -85,7 +88,7 @@ await microtingDbContext.FolderTranslations.FirstAsync(x =>
&& x.MicrotingSdkSiteId == planningSite.SiteId
&& x.Status != 100
&& x.WorkflowState == Constants.WorkflowStates.Created);
Log.LogEvent($"[DBG] ItemsPlanningService PushMessageHandler.Handle : Sending push message body: {body}, title : {planningNameTranslation.Name} to site.id : {site.Id}");
Log.LogEvent($"ItemsPlanningService PushMessageHandler.Handle : Sending push message body: {body}, title : {planningNameTranslation.Name} to site.id : {site.Id}");
Case @case =
await microtingDbContext.Cases.FirstAsync(x =>
x.Id == planningCaseSite.MicrotingSdkCaseId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public async Task Handle(ScheduledItemExecuted message)
return;
}

Log.LogEvent($"ScheduledItemExecutedHandler.Task: SiteIds {siteIds}");
Log.LogEvent($"ScheduledItemExecutedHandler.Task: SiteIds {{string.Join(\", \", siteIds)}}");

if (message.PlanningSiteId.HasValue)
{
Expand Down
47 changes: 20 additions & 27 deletions ServiceItemsPlanningPlugin/Scheduler/Jobs/SearchListJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

using Sentry;

namespace ServiceItemsPlanningPlugin.Scheduler.Jobs;
Expand All @@ -37,7 +36,6 @@ namespace ServiceItemsPlanningPlugin.Scheduler.Jobs;
using Microting.ItemsPlanningBase.Infrastructure.Data;
using Microting.ItemsPlanningBase.Infrastructure.Data.Entities;
using Microting.ItemsPlanningBase.Infrastructure.Enums;
using Microting.eFormApi.BasePn.Infrastructure.Helpers;
using Rebus.Bus;

public static class DateTimeExtensions
Expand All @@ -49,21 +47,13 @@ public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
}
}

public class SearchListJob : IJob
public class SearchListJob(
DbContextHelper dbContextHelper,
IBus bus,
eFormCore.Core sdkCore)
: IJob
{
private readonly ItemsPlanningPnDbContext _dbContext;
private readonly IBus _bus;
private readonly eFormCore.Core _sdkCore;

public SearchListJob(
DbContextHelper dbContextHelper,
IBus bus,
eFormCore.Core sdkCore)
{
_dbContext = dbContextHelper.GetDbContext();
_bus = bus;
_sdkCore = sdkCore;
}
private readonly ItemsPlanningPnDbContext _dbContext = dbContextHelper.GetDbContext();

public async Task Execute()
{
Expand All @@ -76,7 +66,7 @@ private async Task ExecutePush()
{
if (DateTime.UtcNow.Hour == 6)
{
Log.LogEvent("SearchListJob.Task: SearchListJob.Execute got called");
Console.WriteLine("SearchListJob.Task: SearchListJob.ExecutePush got called");
var now = DateTime.UtcNow;

var baseQuery = _dbContext.Plannings
Expand All @@ -94,12 +84,14 @@ private async Task ExecutePush()

var pushReadyPlannings = await pushReady.ToListAsync();

Console.WriteLine($"SearchListJob.Task: Found {pushReadyPlannings.Count} pushReadyPlannings");

foreach (Planning planning in pushReadyPlannings)
{
if ((((DateTime)planning.NextExecutionTime!).AddDays(-1).Date - now.Date).Days ==
planning.DaysBeforeRedeploymentPushMessage)
{
await _bus.SendLocal(new PushMessage(planning.Id));
await bus.SendLocal(new PushMessage(planning.Id));
}
}
}
Expand All @@ -117,19 +109,19 @@ private async Task ExecuteDeploy()
.Single(x => x.Name == "ItemsPlanningBaseSettings:EndTime").Value);
if (DateTime.UtcNow.Hour < startTime)
{
Log.LogEvent(
Console.WriteLine(
$"SearchListJob.Task: ExecuteDeploy The current hour is smaller than the start time of {startTime}, so ending processing");
return;
}

if (DateTime.UtcNow.Hour > endTime)
{
Log.LogEvent(
Console.WriteLine(
$"SearchListJob.Task: ExecuteDeploy The current hour is bigger than the end time of {endTime}, so ending processing");
return;
}

Log.LogEvent("SearchListJob.Task: SearchListJob.Execute got called");
Console.WriteLine("SearchListJob.Task: SearchListJob.ExecuteDeploy got called");
var now = DateTime.UtcNow;
now = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
var baseQuery = _dbContext.Plannings
Expand All @@ -141,12 +133,12 @@ private async Task ExecuteDeploy()
.Where(x => x.Enabled)
.ToListAsync();

Log.LogEvent($"SearchListJob.Task: Found {planningsForExecution.Count} plannings");
Console.WriteLine($"SearchListJob.Task: Found {planningsForExecution.Count} plannings");

var scheduledItemPlannings = new List<Planning>();
scheduledItemPlannings.AddRange(planningsForExecution);

await using var sdkDbContext = _sdkCore.DbContextHelper.GetDbContext();
await using var sdkDbContext = sdkCore.DbContextHelper.GetDbContext();

foreach (var planning in scheduledItemPlannings)
{
Expand Down Expand Up @@ -198,9 +190,9 @@ private async Task ExecuteDeploy()
await planningSite.Update(_dbContext);
}

await _bus.SendLocal(new ScheduledItemExecuted(planning.Id));
await bus.SendLocal(new ScheduledItemExecuted(planning.Id));

Log.LogEvent($"SearchListJob.Task: Planning {planning.Id} executed");
Console.WriteLine($"SearchListJob.Task: Planning {planning.Id} executed");
}
}
}
Expand Down Expand Up @@ -247,8 +239,9 @@ private async Task ExecuteCleanUp()
}
catch (Exception ex)
{
Log.LogException(ex.Message);
Log.LogException(ex.StackTrace);
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
SentrySdk.CaptureException(ex);
}
}
}

0 comments on commit 6993d2d

Please sign in to comment.