Example #1
0
        public static async Task <Result <int> > Install()
        {
            var res1 = ScheduledTasks.RegisterUserScheduledTask(ScheduledTasks.ComplianceCheck.TaskName, ScheduledTasks.ComplianceCheck.ActionPath, ScheduledTasks.ComplianceCheck.ActionArguments, ScheduledTasks.ComplianceCheck.TaskDescription, new List <Trigger> {
                ScheduledTasks.UnlockTrigger(), ScheduledTasks.LoginTrigger(), ScheduledTasks.EventTrigger(ScheduledTasks.ComplianceCheck.EventId)
            })
                       .Try()
                       .Match(result => new Result <Unit>(Unit.Default), exception => new Result <Unit>(new Exception($"Failed to register task: {ScheduledTasks.ComplianceCheck.TaskName}", exception)));

            var res2 = ScheduledTasks.RegisterSystemScheduledTask(ScheduledTasks.ComplianceSystemMeasurements.TaskName, ScheduledTasks.ComplianceSystemMeasurements.ActionPath, ScheduledTasks.ComplianceSystemMeasurements.ActionArguments, ScheduledTasks.ComplianceSystemMeasurements.TaskDescription, new List <Trigger> {
                ScheduledTasks.HourlyTrigger(), ScheduledTasks.EventTrigger(ScheduledTasks.ComplianceSystemMeasurements.EventId)
            })
                       .Try()
                       .Match(result => new Result <Unit>(Unit.Default), exception => new Result <Unit>(new Exception($"Failed to register task: {ScheduledTasks.ComplianceSystemMeasurements.TaskName}", exception)));

            var res3 = ScheduledTasks.RegisterUserScheduledTask(ScheduledTasks.ComplianceUserMeasurements.TaskName, ScheduledTasks.ComplianceUserMeasurements.ActionPath, ScheduledTasks.ComplianceUserMeasurements.ActionArguments, ScheduledTasks.ComplianceUserMeasurements.TaskDescription, new List <Trigger> {
                ScheduledTasks.HourlyTrigger(), ScheduledTasks.LoginTrigger(), ScheduledTasks.EventTrigger(ScheduledTasks.ComplianceUserMeasurements.EventId)
            })
                       .Try()
                       .Match(result => new Result <Unit>(Unit.Default), exception => new Result <Unit>(new Exception($"Failed to register task: {ScheduledTasks.ComplianceUserMeasurements.TaskName}", exception)));

            //var res4 = ScheduledTasks.RegisterSystemManualTask(ScheduledTasks.FullSystemDiskCleanup.TaskName,
            //        ScheduledTasks.FullSystemDiskCleanup.ActionPath, ScheduledTasks.FullSystemDiskCleanup.ActionArguments,
            //    ScheduledTasks.FullSystemDiskCleanup.TaskDescription, new List<Trigger> { ScheduledTasks.HourlyTrigger(), ScheduledTasks.EventTrigger(ScheduledTasks.FullSystemDiskCleanup.EventId) })
            //    .Match(result => new Result<Unit>(Unit.Default), exception => new Result<Unit>(new Exception($"Failed to register task: {ScheduledTasks.FullSystemDiskCleanup.TaskName}", exception)));

            var res5 =
                (await WindowsEventLog.CreateEventSource().ConfigureAwait(false))
                .Match(unit => new Result <Unit>(Unit.Default), exception => new Result <Unit>(new Exception("Failed to register event log source.", exception)));

            var installResult = new List <Result <Unit> > {
                res1, res2, res3, res5
            }.ToResult().Match(exitCodes => new Result <int>(0), exception => new Result <int>(exception));

            return(await Task.FromResult(installResult).ConfigureAwait(false));
        }
Example #2
0
 public static async Task <Result <Unit> > RunScheduledTask(Some <ScheduledTaskInfo> scheduledTaskInfo, bool waitForExit)
 {
     return(await F.AsyncTryFunc <Unit>(async() =>
     {
         var eventLogResult = await WindowsEventLog.WriteEventLog($"Running scheduled task '{scheduledTaskInfo.Value.TaskName}'", EventLogEntryType.Information, scheduledTaskInfo.Value.EventId).ConfigureAwait(false);
         var waitForExitResult =
             await eventLogResult
             .Match(
                 async unit =>
         {
             var res = waitForExit
                                     ? WaitForScheduledTaskExit(scheduledTaskInfo.Value.TaskName).ConfigureAwait(false)
                                     : Task.FromResult(new Result <Unit>(Unit.Default)).ConfigureAwait(false);
             return await res;
         },
                 async exception => await Task.FromResult(new Result <Unit>(exception)).ConfigureAwait(false)
                 ).ConfigureAwait(false);
         return waitForExitResult;
     }).ConfigureAwait(false));
 }