Beispiel #1
0
        ContainerBuilder ConfigureContainer()
        {
            var cb = new ContainerBuilder();

            cb.RegisterModule(new QuartzAutofacFactoryModule());
            cb.Register(
                c =>
                new PollerAutofacJobFactory(
                    c.Resolve <ILifetimeScope>(), "Poller.Job", _onLogDebug, _onLogInfo, _onLogWarning))
            .AsSelf()
            .As <IJobFactory>()
            .SingleInstance();

            _containerConfiguration(cb);
            cb.RegisterType <JobsManager>().AsSelf();
            foreach (var taskAdapter in _concreteTask)
            {
                TaskAdapter task = taskAdapter.Value;

                cb.RegisterType(task.Type).Named <ITask>(task.TaskName);

                cb.Register(
                    context =>
                {
                    try
                    {
                        var resolveNamed = context.ResolveNamed <ITask>(task.TaskName);
                        return(new Job(resolveNamed, _onException));
                    }
                    catch (Exception ex)
                    {
                        _onException(
                            this,
                            new Exception(
                                string.Format(
                                    "There was an error in the inicialization of the job: {0}; error message:{1}", task.TaskName, ex.Message),
                                ex));
                        throw;
                    }
                }).Named <Job>(task.TaskName).AsSelf().InstancePerLifetimeScope();
            }

            return(cb);
        }
Beispiel #2
0
        public void ConfigureBackgroundJobs(ServiceConfigurator <JobsManager> svc)
        {
            svc.UsingQuartzJobFactory(() => _container.Resolve <PollerAutofacJobFactory>());

            foreach (var taskAdapter in _concreteTask)
            {
                string      name            = taskAdapter.Key;
                TaskAdapter adapter         = taskAdapter.Value;
                string      taskDescription = adapter.TaskDescription;
                if (adapter.IntervalInSeconds > 0)
                {
                    svc.ScheduleQuartzJob(
                        q =>
                    {
                        q.WithJob(
                            JobBuilder.Create <Job>().WithIdentity(name).WithDescription(taskDescription).Build);
                        q.AddTrigger(
                            () =>
                            TriggerBuilder.Create()
                            .WithSchedule(
                                SimpleScheduleBuilder.RepeatSecondlyForever(adapter.IntervalInSeconds))
                            .Build());
                    });
                }
                else
                {
                    svc.ScheduleQuartzJob(
                        q =>
                    {
                        q.WithJob(
                            JobBuilder.Create <Job>().WithIdentity(name).WithDescription(taskDescription).Build);
                        q.AddTrigger(
                            () =>
                            TriggerBuilder.Create()
                            .WithSimpleSchedule(s =>
                                                s.RepeatForever()
                                                .WithInterval(TimeSpan.FromMilliseconds(1)))
                            .StartNow()
                            .Build());
                    });
                }
            }
        }