Outlines metadata for the registration of Tasks with MEF
Inheritance: PluginMetadataComposition
Esempio n. 1
0
        /// <summary>
        /// Adds a new delegate task to the task manager with the specified trigger
        /// </summary>
        /// <param name="triggerName"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        public ApplicationTaskManager AddDelegateTask(string triggerName, Action <TaskExecutionContext> callback)
        {
            var metaData = new TaskMetadata(triggerName, true);

            AddTask(new Lazy <AbstractTask, TaskMetadata>(() => new DelegateTask(null, callback), metaData));
            return(this);
        }
        /// <summary>
        /// This gets all tasks found in configuration blocks and dynamically creates real task definitions from them 
        /// and registers them with the task manager with the trigger name of the package they were found in 
        /// </summary>
        /// <param name="context"></param>
        public override void Execute(TaskExecutionContext context)
        {
            using (DisposableTimer.TraceDuration<ConfigurationTaskBuilder>("Building configuration tasks", "End building configuration tasks"))
            {
                //get all tasks in configuration
                foreach (var t in ApplicationContext.Settings.Tasks)
                {
                    //create a new config task context with the configuration task's parameters
                    var configTaskContext = new ConfigurationTaskContext(ApplicationContext, t.Parameters, t);

                    //create a new instance of the configuration task
                    var task = (ConfigurationTask)Activator.CreateInstance(t.TaskType, new[] { configTaskContext });
                    var meta = new TaskMetadata(null)
                    {
                        ComponentType = task.GetType(),
                        ContinueOnError = true,
                        Id = Guid.NewGuid(),
                    };
                    //now we need to check the trigger name, if it is either post-package-install or pre-package-uninstall 
                    //then we need to check if this task has been registered with a package. If so, then we change the trigger
                    //to be PackageFolderName-post-package-install so that the task only executes for the current packages installation
                    //changes. If its neither of these names, then we just use the normal trigger
                    if ((t.Trigger.InvariantEquals(TaskTriggers.PostPackageInstall) || t.Trigger.InvariantEquals(TaskTriggers.PrePackageUninstall))
                        && !t.PackageFolder.IsNullOrWhiteSpace())
                    {
                        var parts = t.PackageFolder.Split(Path.DirectorySeparatorChar);
                        var packageName = parts[parts.Length - 1];
                        var newTriggername = packageName + "-" + t.Trigger;
                        var t1 = t;
                        LogHelper.TraceIfEnabled<ConfigurationTaskBuilder>("Found {0} config task of type {1} for package, registering it as {2}", () => t1.Trigger, () => t1.TaskType.Name, () => newTriggername);
                        //ok, we've got a task declared on a package for post package install or pre package uninstall, so need to update the trigger accordingly
                        meta.TriggerName = newTriggername;
                    }
                    else
                    {
                        //nope, this is a task declared in config that is not a package... unexpected but i guess it could happen
                        //so we just use the normal trigger that it was declared with.
                        var t1 = t;
                        LogHelper.TraceIfEnabled<ConfigurationTaskBuilder>("Found {0} config task of type {1} outside of a package", () => t1.Trigger, () => t1.TaskType.Name);
                        meta.TriggerName = t.Trigger;
                    }

                    //create a new task definition
                    var taskDefinition = new Lazy<AbstractTask, TaskMetadata>(() => task, meta);
                    //add the definition to the manager
                    ApplicationContext.FrameworkContext.TaskManager.AddTask(taskDefinition);
                }
            }
            
        }
Esempio n. 3
0
 /// <summary>
 /// Registers a task handler with the manager.
 /// </summary>
 /// <param name="valueFactory">The factory which will create the handler.</param>
 /// <param name="metaData">The meta data for the task.</param>
 /// <returns></returns>
 public ApplicationTaskManager AddTask(Func <AbstractTask> valueFactory, TaskMetadata metaData)
 {
     return(AddTask(new Lazy <AbstractTask, TaskMetadata>(valueFactory, metaData)));
 }