Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

イベントログを定期的に削除する #1077

Merged
2 commits merged into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
<Compile Include="Services\LocalPathService.cs" />
<Compile Include="Services\EventLogSubmissionBackgroundService.cs" />
<Compile Include="Renderers\AccessibilityContentViewRenderer.cs" />
<Compile Include="Services\DataMaintainanceBackgroundService.cs" />
</ItemGroup>
<ItemGroup>
<None Include="appcenter-pre-build.sh" />
Expand Down
14 changes: 14 additions & 0 deletions Covid19Radar/Covid19Radar.Android/MainApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ private Lazy<AbsExposureDetectionBackgroundService> _exposureDetectionBackground
private readonly Lazy<AbsEventLogSubmissionBackgroundService> _eventLogSubmissionBackgroundService
= new Lazy<AbsEventLogSubmissionBackgroundService>(() => ContainerLocator.Current.Resolve<AbsEventLogSubmissionBackgroundService>());

private readonly Lazy<AbsDataMaintainanceBackgroundService> _dataMaintainanceService
= new Lazy<AbsDataMaintainanceBackgroundService>(() => ContainerLocator.Current.Resolve<AbsDataMaintainanceBackgroundService>());

private Lazy<ILoggerService> _loggerService
= new Lazy<ILoggerService>(() => ContainerLocator.Current.Resolve<ILoggerService>());

Expand Down Expand Up @@ -98,6 +101,7 @@ private void ScheduleBackgroundTasks()
{
_loggerService.Value.Exception("Failed to schedule ExposureDetectionBackgroundService", exception);
}

try
{
_eventLogSubmissionBackgroundService.Value.Schedule();
Expand All @@ -106,6 +110,15 @@ private void ScheduleBackgroundTasks()
{
_loggerService.Value.Exception("Failed to schedule EventLogSubmissionBackgroundService", exception);
}

try
{
_dataMaintainanceService.Value.Schedule();
}
catch (Exception exception)
{
_loggerService.Value.Exception("Failed to schedule DataMaintainanceBackgroundService", exception);
}
}

private void SetupENClient(ExposureNotificationClient client)
Expand Down Expand Up @@ -141,6 +154,7 @@ private void RegisterPlatformTypes(IContainer container)
container.Register<IExternalNavigationService, ExternalNavigationService>(Reuse.Singleton);
container.Register<IPlatformService, PlatformService>(Reuse.Singleton);
container.Register<AbsEventLogSubmissionBackgroundService, EventLogSubmissionBackgroundService>(Reuse.Singleton);
container.Register<AbsDataMaintainanceBackgroundService, DataMaintainanceBackgroundService>(Reuse.Singleton);
}

public Task<ExposureConfiguration> GetExposureConfigurationAsync()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

using System;
using Android.Content;
using Android.Runtime;
using AndroidX.Work;
using Covid19Radar.Repository;
using Covid19Radar.Services;
using Covid19Radar.Services.Logs;
using Java.Util.Concurrent;
using Prism.Ioc;
using Xamarin.Essentials;

namespace Covid19Radar.Droid.Services
{
public class DataMaintainanceBackgroundService : AbsDataMaintainanceBackgroundService
{
private const string CURRENT_WORK_NAME = "data_maintainance_worker_20220628";

private const long INTERVAL_IN_HOURS = 24;
private const long BACKOFF_DELAY_IN_MINUTES = 60;

public DataMaintainanceBackgroundService(
ILoggerService loggerService,
IEventLogRepository eventLogRepository
) : base(loggerService, eventLogRepository)
{
// do nothing
}

public override void Schedule()
{
LoggerService.StartMethod();

WorkManager workManager = WorkManager.GetInstance(Platform.AppContext);

PeriodicWorkRequest periodicWorkRequest = CreatePeriodicWorkRequest();
workManager.EnqueueUniquePeriodicWork(
CURRENT_WORK_NAME,
ExistingPeriodicWorkPolicy.Keep,
periodicWorkRequest
);

LoggerService.EndMethod();
}

private PeriodicWorkRequest CreatePeriodicWorkRequest()
{
var workRequestBuilder = new PeriodicWorkRequest.Builder(
typeof(DataMaintainanceBackgroundWorker),
INTERVAL_IN_HOURS, TimeUnit.Hours
)
.SetConstraints(new Constraints.Builder()
.SetRequiresBatteryNotLow(true)
.Build())
.SetBackoffCriteria(BackoffPolicy.Linear, BACKOFF_DELAY_IN_MINUTES, TimeUnit.Minutes);
return workRequestBuilder.Build();
}
}

[Preserve]
public class DataMaintainanceBackgroundWorker : Worker
{
private Lazy<AbsDataMaintainanceBackgroundService> _dataMaintainanceBackgroundService
=> new Lazy<AbsDataMaintainanceBackgroundService>(() => ContainerLocator.Current.Resolve<AbsDataMaintainanceBackgroundService>());
private Lazy<ILoggerService> _loggerService => new Lazy<ILoggerService>(() => ContainerLocator.Current.Resolve<ILoggerService>());

public DataMaintainanceBackgroundWorker(Context context, WorkerParameters workerParameters)
: base(context, workerParameters)
{
// do nothing
}

public override Result DoWork()
{
var dataMaintainanceBackgroundService = _dataMaintainanceBackgroundService.Value;
var loggerService = _loggerService.Value;

loggerService.StartMethod();

try
{
dataMaintainanceBackgroundService.ExecuteAsync().GetAwaiter().GetResult();
return Result.InvokeSuccess();
}
catch (Exception exception)
{
loggerService.Exception("Exception", exception);
return Result.InvokeFailure();
}
finally
{
loggerService.EndMethod();
}
}

public override void OnStopped()
{
base.OnStopped();

_loggerService.Value.Warning("OnStopped");
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,18 @@ public override Result DoWork()
public class MigrationProccessService : IMigrationProcessService
{
private readonly AbsExposureDetectionBackgroundService _exposureDetectionBackgroundService;
private readonly AbsDataMaintainanceBackgroundService _dataMaintainanceBackgroundService;

private readonly ILoggerService _loggerService;

public MigrationProccessService(
AbsExposureDetectionBackgroundService exposureDetectionBackgroundService,
AbsDataMaintainanceBackgroundService dataMaintainanceBackgroundService,
ILoggerService loggerService
)
{
_exposureDetectionBackgroundService = exposureDetectionBackgroundService;
_dataMaintainanceBackgroundService = dataMaintainanceBackgroundService;
_loggerService = loggerService;
}

Expand All @@ -86,6 +90,7 @@ public async Task SetupAsync()

await new WorkManagerMigrator(
_exposureDetectionBackgroundService,
_dataMaintainanceBackgroundService,
_loggerService
).ExecuteAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ internal class WorkManagerMigrator
};

private readonly AbsExposureDetectionBackgroundService _exposureDetectionBackgroundService;
private readonly AbsDataMaintainanceBackgroundService _dataMaintainanceBackgroundService;

private readonly ILoggerService _loggerService;

public WorkManagerMigrator(
AbsExposureDetectionBackgroundService exposureDetectionBackgroundService,
AbsDataMaintainanceBackgroundService dataMaintainanceBackgroundService,
ILoggerService loggerService
)
{
_exposureDetectionBackgroundService = exposureDetectionBackgroundService;
_dataMaintainanceBackgroundService = dataMaintainanceBackgroundService;
_loggerService = loggerService;
}

Expand All @@ -38,6 +42,7 @@ internal Task ExecuteAsync()
CancelOldWorks(workManager, OldWorkNames, _loggerService);

_exposureDetectionBackgroundService.Schedule();
_dataMaintainanceBackgroundService.Schedule();

_loggerService.EndMethod();

Expand Down
14 changes: 14 additions & 0 deletions Covid19Radar/Covid19Radar.iOS/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ private Lazy<AbsExposureDetectionBackgroundService> _exposureDetectionBackground
private readonly Lazy<AbsEventLogSubmissionBackgroundService> _eventLogSubmissionBackgroundService
= new Lazy<AbsEventLogSubmissionBackgroundService>(() => ContainerLocator.Current.Resolve<AbsEventLogSubmissionBackgroundService>());

private readonly Lazy<AbsDataMaintainanceBackgroundService> _dataMaintainanceBackgroundService
= new Lazy<AbsDataMaintainanceBackgroundService>(() => ContainerLocator.Current.Resolve<AbsDataMaintainanceBackgroundService>());

private Lazy<IExposureDetectionService> _exposureDetectionService
= new Lazy<IExposureDetectionService>(() => ContainerLocator.Current.Resolve<IExposureDetectionService>());

Expand Down Expand Up @@ -127,6 +130,7 @@ private void ScheduleBackgroundTask()
{
_loggerService.Value.Exception("Failed to schedule ExposureDetectionBackgroundService", exception);
}

try
{
_eventLogSubmissionBackgroundService.Value.Schedule();
Expand All @@ -135,6 +139,15 @@ private void ScheduleBackgroundTask()
{
_loggerService.Value.Exception("Failed to schedule EventLogSubmissionBackgroundService", exception);
}

try
{
_dataMaintainanceBackgroundService.Value.Schedule();
}
catch (Exception exception)
{
_loggerService.Value.Exception("Failed to schedule DataMaintainanceBackgroundService", exception);
}
}

private bool IsUniversalLinks(NSDictionary launchOptions)
Expand Down Expand Up @@ -274,6 +287,7 @@ private void RegisterPlatformTypes(IContainer container)
#endif
container.Register<IExternalNavigationService, ExternalNavigationService>(Reuse.Singleton);
container.Register<AbsEventLogSubmissionBackgroundService, EventLogSubmissionBackgroundService>(Reuse.Singleton);
container.Register<AbsDataMaintainanceBackgroundService, DataMaintainanceBackgroundService>(Reuse.Singleton);
}

public Task<ExposureConfiguration> GetExposureConfigurationAsync()
Expand Down
1 change: 1 addition & 0 deletions Covid19Radar/Covid19Radar.iOS/Covid19Radar.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
<Compile Include="Services\LocalPathService.cs" />
<Compile Include="Services\EventLogSubmissionBackgroundService.cs" />
<Compile Include="Renderers\AccessibilityContentViewRenderer.cs" />
<Compile Include="Services\DataMaintainanceBackgroundService.cs" />
</ItemGroup>
<ItemGroup>
<ImageAsset Include="Assets.xcassets\Contents.json">
Expand Down
1 change: 1 addition & 0 deletions Covid19Radar/Covid19Radar.iOS/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<string>APP_PACKAGE_NAME.exposure-notification</string>
<string>APP_PACKAGE_NAME.delete-old-logs</string>
<string>APP_PACKAGE_NAME.eventlog-submission</string>
<string>APP_PACKAGE_NAME.data-maintainance</string>
</array>
<key>UIAppFonts</key>
<array>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

using System;
using System.Threading;
using System.Threading.Tasks;
using BackgroundTasks;
using Covid19Radar.Common;
using Covid19Radar.Repository;
using Covid19Radar.Services;
using Covid19Radar.Services.Logs;
using Foundation;
using Xamarin.Essentials;

namespace Covid19Radar.iOS.Services
{
public class DataMaintainanceBackgroundService : AbsDataMaintainanceBackgroundService
{
private const int TASK_INTERVAL_IN_DAYS = 1;
private static readonly string BGTASK_IDENTIFIER = AppInfo.PackageName + ".data-maintainance";

private readonly IDateTimeUtility _dateTimeUtility;

public DataMaintainanceBackgroundService(
ILoggerService loggerService,
IEventLogRepository eventLogRepository,
IDateTimeUtility dateTimeUtility
) : base(loggerService, eventLogRepository)
{
_dateTimeUtility = dateTimeUtility;
}

public override void Schedule()
{
LoggerService.StartMethod();

var result = BGTaskScheduler.Shared.Register(BGTASK_IDENTIFIER, null, task =>
{
LoggerService.Info("Background task has been started.");

DateTime nextDateTime = _dateTimeUtility.UtcNow.Date.AddDays(TASK_INTERVAL_IN_DAYS);
ScheduleBgTask(nextDateTime);

var cancellationTokenSource = new CancellationTokenSource();
task.ExpirationHandler = cancellationTokenSource.Cancel;

_ = Task.Run(async () =>
{
try
{
await ExecuteAsync();
task.SetTaskCompleted(true);
}
catch (OperationCanceledException exception)
{
LoggerService.Exception($"Background task canceled.", exception);
task.SetTaskCompleted(false);
}
catch (Exception exception)
{
LoggerService.Exception($"Exception", exception);
task.SetTaskCompleted(false);
}
finally
{
cancellationTokenSource.Dispose();
}
}, cancellationTokenSource.Token);
});

if (result)
{
LoggerService.Debug("BGTaskScheduler.Shared.Register succeeded.");
}
else
{
LoggerService.Info("BGTaskScheduler.Shared.Register failed.");
}

ScheduleBgTask(_dateTimeUtility.UtcNow);

LoggerService.EndMethod();
}

private void ScheduleBgTask(DateTime nextDateTime)
{
LoggerService.StartMethod();

try
{
BGProcessingTaskRequest bgTaskRequest = new BGProcessingTaskRequest(BGTASK_IDENTIFIER)
{
EarliestBeginDate = NSDate.FromTimeIntervalSince1970(nextDateTime.ToUnixEpoch())
};

BGTaskScheduler.Shared.Submit(bgTaskRequest, out var error);
if (error != null)
{
NSErrorException exception = new NSErrorException(error);
LoggerService.Exception("BGTaskScheduler submit failed.", exception);
throw exception;
}
}
finally
{
LoggerService.EndMethod();
}
}
}
}

Loading