Exemplo n.º 1
0
        private void StartActivator(AutomationConfiguration configuration, Job job)
        {
            if (string.IsNullOrEmpty(job.Activator))
            {
                return;
            }

            this.Log.Info($"Starting the activator {job.Activator}");

            var activatorConfiguration = configuration.Activators.SingleOrDefault(x => x.Id == job.Activator) ??
                                         throw new ConfigurationException($"The activator {job.Activator} cannot be found");

            var activator = Factory.CreateActivator(activatorConfiguration);

            var messageProvider = activator as BaseMessageProvider ??
                                  throw new ConfigurationException($"The activator {job.Activator} is not a BaseMessageProvider");

            messageProvider.MessageReceived += (sender, message) => { this.ProcessMessageReceived(configuration, job, message); };

            lock (RunningActivators)
            {
                RunningActivators.Add(activator);
                activator.Start(activatorConfiguration.Config);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create a new instance of the processor.
        /// </summary>
        /// <param name="automationConfiguration">The configuration file of the automation.</param>
        /// <param name="ruleId">The id of the rule to create.</param>
        /// <returns>The instance of the rule.</returns>
        public static IRule CreateRule(AutomationConfiguration automationConfiguration, string ruleId)
        {
            var ruleConfiguration = automationConfiguration.Rules.SingleOrDefault(x => x.Id == ruleId) ??
                                    throw new ConfigurationException($"The rule {ruleId} cannot be found");

            return(CreateRule(ruleConfiguration));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create a new instance of the processor.
        /// </summary>
        /// <param name="automationConfiguration">The configuration file of the automation.</param>
        /// <param name="processorId">The id of the processor to create.</param>
        /// <returns>The instance of the processor.</returns>
        public static IProcessor CreateProcessor(AutomationConfiguration automationConfiguration, string processorId)
        {
            var processorConfiguration = automationConfiguration.Processors.SingleOrDefault(x => x.Id == processorId) ??
                                         throw new ConfigurationException($"The processor {processorId} cannot be found");

            return(CreateProcessor(processorConfiguration));
        }
Exemplo n.º 4
0
        private void WakeUpActivator(AutomationConfiguration configuration, Job job)
        {
            if (string.IsNullOrEmpty(job.Activator))
            {
                return;
            }

            this.Log.Info($"WakeUp the activator {job.Activator}");

            var activatorConfiguration = configuration.Activators.SingleOrDefault(x => x.Id == job.Activator) ??
                                         throw new ConfigurationException($"The activator {job.Activator} cannot be found");

            IActivator runningActivator;

            lock (RunningActivators)
            {
                runningActivator = RunningActivators.FirstOrDefault(x => x.Id == job.Activator);

                if (runningActivator == null)
                {
                    throw new ConfigurationException($"The activator {job.Activator} is not running, ensure the RunAtStartup is true.");
                }
            }

            runningActivator.WakeUp(activatorConfiguration.Config);
        }
Exemplo n.º 5
0
        private void StartAutomation()
        {
            var configurationPath = Path.Combine(this.Environment.ContentRootPath, "configuration.json");

            if (!File.Exists(configurationPath))
            {
                Log.Error($"Cannot find the configuration file {configurationPath} to register jobs.");
                return;
            }

            var configuration = AutomationConfiguration.Read(configurationPath);

            foreach (var job in configuration.Jobs.Where(job => job.RunAtStartup))
            {
                BackgroundJob.Enqueue <AutomationRunner>(automation => automation.RunAtStartup(configuration, job));
            }

            foreach (var job in configuration.Jobs)
            {
                RecurringJob.RemoveIfExists(job.Id);
            }

            foreach (var job in configuration.Jobs.Where(job => !string.IsNullOrWhiteSpace(job.CronSchedule)))
            {
                RecurringJob.AddOrUpdate <AutomationRunner>(job.Id, automation => automation.RunRecurring(configuration, job), job.CronSchedule, TimeZoneInfo.Utc);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Run the specified job at startup.
        /// </summary>
        /// <param name="configuration">The complete configuration.</param>
        /// <param name="job">The job to be executed.</param>
        public void RunAtStartup(AutomationConfiguration configuration, Job job)
        {
            this.Log.Info($"Starting the job : {job.FriendlyName}");

            this.RunConnector(configuration, job);
            this.StartActivator(configuration, job);
        }
Exemplo n.º 7
0
 public void Setup()
 {
     Console.WriteLine("Setup");
     Armoury            = Get <Armoury>();
     VerificationErrors = new StringBuilder();
     Configuration      = Get <AutomationConfiguration>();
 }
        private AutomationInstance CreateAutomation(string uid, AutomationConfiguration configuration, WirehomeDictionary settings)
        {
            var repositoryEntitySource = _repositoryService.LoadEntity(configuration.Logic.Uid);
            var scriptHost             = _pythonScriptHostFactoryService.CreateScriptHost(_logger, new AutomationPythonProxy(uid, this));

            scriptHost.Initialize(repositoryEntitySource.Script);

            var context = new WirehomeDictionary
            {
                ["automation_uid"] = uid,
                ["logic_id"]       = configuration.Logic.Uid.Id,
                ["logic_version"]  = configuration.Logic.Uid.Version
            };

            // TODO: Remove scope as soon as all automations are migrated.
            scriptHost.SetVariable("scope", context);
            scriptHost.SetVariable("context", context);

            foreach (var variable in configuration.Logic.Variables)
            {
                scriptHost.SetVariable(variable.Key, variable.Value);
            }

            var automation = new AutomationInstance(uid, scriptHost);

            foreach (var setting in settings)
            {
                automation.Settings[setting.Key] = setting.Value;
            }

            return(automation);
        }
Exemplo n.º 9
0
 public ErrorditeDriver(AutomationSession automationSession, AutomationConfiguration configuration, IAppSession appSession)
 {
     _webDriver = new FirefoxDriver();
     _webDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); //wait for 5 seconds
     _automationSession = automationSession;
     _configuration     = configuration;
     _appSession        = appSession;
 }
        public void InitializeAutomation(string uid, AutomationConfiguration configuration)
        {
            if (uid == null)
            {
                throw new ArgumentNullException(nameof(uid));
            }
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            if (!configuration.IsEnabled)
            {
                _logger.Log(LogLevel.Information, $"Automation '{uid}' not initialized because it is disabled.");
                return;
            }

            var repositoryEntitySource = _repositoryService.LoadEntity(configuration.Logic.Uid);

            var scriptHost = _pythonEngineService.CreateScriptHost(_logger);

            scriptHost.Initialize(repositoryEntitySource.Script);

            var scope = new WirehomeDictionary
            {
                ["automation_uid"] = uid,
                ["logic_id"]       = configuration.Logic.Uid.Id,
                ["logic_version"]  = configuration.Logic.Uid.Version
            };

            scriptHost.SetVariable("scope", scope);
            foreach (var variable in configuration.Logic.Variables)
            {
                scriptHost.SetVariable(variable.Key, variable.Value);
            }

            var automationInstance = new Automation(scriptHost);

            automationInstance.Initialize();

            lock (_automations)
            {
                if (_automations.TryGetValue(uid, out var existingAutomationInstance))
                {
                    existingAutomationInstance.Deactivate();
                }

                automationInstance.Activate();

                _automations[uid] = automationInstance;
            }

            _logger.Log(LogLevel.Information, $"Automation '{uid}' initialized.");
        }
 private void TryInitializeAutomation(string uid, AutomationConfiguration configuration)
 {
     try
     {
         InitializeAutomation(uid, configuration);
     }
     catch (Exception exception)
     {
         _logger.Log(LogLevel.Error, exception, $"Error while initializing automation '{uid}'.");
     }
 }
        public void WriteAutomationConfiguration(string uid, AutomationConfiguration configuration)
        {
            if (uid == null)
            {
                throw new ArgumentNullException(nameof(uid));
            }
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            _storageService.Write(configuration, AutomationsDirectory, uid, DefaultFilenames.Configuration);
        }
Exemplo n.º 13
0
        /// <inheritdoc />
        protected override Message InternalRun(AutomationConfiguration configuration, Job job, Message message)
        {
            var processor = Factory.CreateProcessor(configuration, this.Processor);

            if (string.IsNullOrEmpty(this.RuleId))
            {
                processor.Execute(job, message);
                return(message);
            }

            var rule = Factory.CreateRule(configuration, this.RuleId);

            if (rule.IsMatch(job, message))
            {
                processor.Execute(job, message);
            }

            return(message);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Start the program.
        /// </summary>
        /// <param name="args">Arguments of the console.</param>
        private static void Main(string[] args)
        {
            var log4netConfig = new XmlDocument();

            log4netConfig.Load(File.OpenRead("log4net.config"));
            var repo = LogManager.CreateRepository(Assembly.GetEntryAssembly(), typeof(Hierarchy));

            XmlConfigurator.Configure(repo, log4netConfig["log4net"]);

            Log.Info("START CONSOLE");

            var path          = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "configuration.json");
            var configuration = AutomationConfiguration.Read(path);

            foreach (var job in configuration.Jobs.Where(job => job.RunAtStartup))
            {
                new JobRunner().RunAtStartup(configuration, job);
            }

            Log.Info("STOP CONSOLE");
        }
Exemplo n.º 15
0
        private void RunConnector(AutomationConfiguration configuration, Job job)
        {
            if (string.IsNullOrEmpty(job.Connector))
            {
                return;
            }

            this.Log.Info($"Starting the connector {job.Connector}");

            var connectorConfiguration = configuration.Connectors.SingleOrDefault(x => x.Id == job.Connector) ??
                                         throw new ConfigurationException($"The connector {job.Connector} cannot be found");

            var connector = Factory.CreateConnector(connectorConfiguration);

            var messageProvider = connector as BaseMessageProvider ??
                                  throw new ConfigurationException($"The connector {job.Connector} is not a BaseMessageProvider");

            messageProvider.MessageReceived += (sender, message) => { this.ProcessMessageReceived(configuration, job, message); };

            connector.Execute(connectorConfiguration.Config);
        }
Exemplo n.º 16
0
        private void ProcessMessageReceived(AutomationConfiguration configuration, Job job, Message message)
        {
            configuration.ThrowIfNull(nameof(configuration));
            job.ThrowIfNull(nameof(job));
            message.ThrowIfNull(nameof(message));

            this.Log.Info($"{job.Id} : A new message has been received");

            foreach (var actionRunner in job.Actions.Select(Factory.CreateAction))
            {
                try
                {
                    actionRunner.Run(configuration, job, message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
        }
 public void RunAtStartup(AutomationConfiguration configuration, Job job)
 {
     new JobRunner().RunAtStartup(configuration, job);
 }
Exemplo n.º 18
0
 /// <summary>
 /// Run the specified job multiple times.
 /// </summary>
 /// <param name="configuration">The complete configuration.</param>
 /// <param name="job">The job to be executed.</param>
 public void RunRecurring(AutomationConfiguration configuration, Job job)
 {
     this.Log.Info($"Running the job : {job.FriendlyName}");
     this.RunConnector(configuration, job);
     this.WakeUpActivator(configuration, job);
 }
Exemplo n.º 19
0
 /// <inheritdoc />
 protected override Message InternalRun(AutomationConfiguration configuration, Job job, Message message)
 {
     return(this.Actions.Select(Factory.CreateAction).Aggregate(message, (current, action) => action.Run(configuration, job, current)));
 }
Exemplo n.º 20
0
 public void Post(string uid, [FromBody] AutomationConfiguration configuration)
 {
     _automationsRegistryService.InitializeAutomation(uid, configuration);
 }
Exemplo n.º 21
0
 /// <summary>
 /// Run the specified action.
 /// </summary>
 /// <param name="configuration">The configuration of the automation.</param>
 /// <param name="job">The source job.</param>
 /// <param name="message">The message to process.</param>
 /// <returns>Return the message.</returns>
 public Message Run(AutomationConfiguration configuration, Job job, Message message)
 {
     this.Log.Info($"{job.Id} : Running the action type {this.Type}");
     return(this.InternalRun(configuration, job, message));
 }
Exemplo n.º 22
0
 /// <summary>
 /// Run the specified action.
 /// </summary>
 /// <param name="configuration">The configuration of the automation.</param>
 /// <param name="job">The source job.</param>
 /// <param name="message">The message to process.</param>
 /// <returns>Return the message.</returns>
 protected abstract Message InternalRun(AutomationConfiguration configuration, Job job, Message message);
Exemplo n.º 23
0
 public void PostConfiguration(string uid, [FromBody] AutomationConfiguration configuration)
 {
     _automationRegistryService.WriteAutomationConfiguration(uid, configuration);
 }
 public void RunRecurring(AutomationConfiguration configuration, Job job)
 {
     new JobRunner().RunRecurring(configuration, job);
 }