예제 #1
0
        public void Add(IBackgroundWorker worker)
        {
            var timer = worker.GetType()
                        .GetProperty("Timer", BindingFlags.NonPublic | BindingFlags.Instance)?
                        .GetValue(worker);

            if (timer == null)
            {
                return;
            }

            var period = timer.GetType()
                         .GetProperty("Period", BindingFlags.Public | BindingFlags.Instance)?
                         .GetValue(timer)?
                         .To <int>();

            if (!period.HasValue)
            {
                return;
            }

            var adapterType   = typeof(HangfireBackgroundWorkerAdapter <>).MakeGenericType(worker.GetType());
            var workerAdapter = ServiceProvider.GetRequiredService(adapterType) as IHangfireBackgroundWorkerAdapter;

            RecurringJob.AddOrUpdate(
                recurringJobId: worker.GetType().FullName,
                methodCall: () => workerAdapter.ExecuteAsync(),
                cronExpression: CronGenerator.FormMilliseconds(period.Value));
        }
예제 #2
0
        public void BuildWorker(IBackgroundWorker worker)
        {
            int?period;
            var workerType = worker.GetType();

            if (worker is AsyncPeriodicBackgroundWorkerBase || worker is PeriodicBackgroundWorkerBase)
            {
                if (typeof(TWorker) != worker.GetType())
                {
                    throw new ArgumentException($"{nameof(worker)} type is different from the generic type");
                }

                var timer = (AbpTimer)worker.GetType().GetProperty("Timer", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(worker);
                period = timer?.Period;
            }
            else
            {
                return;
            }

            if (period == null)
            {
                return;
            }

            JobDetail = JobBuilder
                        .Create <QuartzPeriodicBackgroundWorkerAdapter <TWorker> >()
                        .WithIdentity(workerType.FullName)
                        .Build();
            Trigger = TriggerBuilder.Create()
                      .WithIdentity(workerType.FullName)
                      .WithSimpleSchedule(builder => builder.WithInterval(TimeSpan.FromMilliseconds(period.Value)).RepeatForever())
                      .Build();
        }
예제 #3
0
    public Task AddAsync(IBackgroundWorker worker)
    {
        if (worker is IHangfireBackgroundWorker hangfireBackgroundWorker)
        {
            var unProxyWorker = ProxyHelper.UnProxy(hangfireBackgroundWorker);
            if (hangfireBackgroundWorker.RecurringJobId.IsNullOrWhiteSpace())
            {
                RecurringJob.AddOrUpdate(() => ((IHangfireBackgroundWorker)unProxyWorker).DoWorkAsync(), hangfireBackgroundWorker.CronExpression);
            }
            else
            {
                RecurringJob.AddOrUpdate(hangfireBackgroundWorker.RecurringJobId, () => ((IHangfireBackgroundWorker)unProxyWorker).DoWorkAsync(),
                                         hangfireBackgroundWorker.CronExpression);
            }
        }
        else
        {
            int?period;

            if (worker is AsyncPeriodicBackgroundWorkerBase or PeriodicBackgroundWorkerBase)
            {
                var timer = worker.GetType()
                            .GetProperty("Timer", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(worker);

                if (worker is AsyncPeriodicBackgroundWorkerBase)
                {
                    period = ((AbpAsyncTimer)timer)?.Period;
                }
                else
                {
                    period = ((AbpTimer)timer)?.Period;
                }
            }
예제 #4
0
        public void Add(IBackgroundWorker worker)
        {
            if (worker is IHangfireBackgroundWorker hangfireBackgroundWorker)
            {
                RecurringJob.AddOrUpdate(() => hangfireBackgroundWorker.ExecuteAsync(),
                                         hangfireBackgroundWorker.CronExpression);
            }
            else
            {
                int?period;

                if (worker is AsyncPeriodicBackgroundWorkerBase || worker is PeriodicBackgroundWorkerBase)
                {
                    var timer = (AbpTimer)worker.GetType()
                                .GetProperty("Timer", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(worker);
                    period = timer?.Period;
                }
                else
                {
                    return;
                }

                if (period == null)
                {
                    return;
                }

                var adapterType   = typeof(HangfirePeriodicBackgroundWorkerAdapter <>).MakeGenericType(worker.GetType());
                var workerAdapter = Activator.CreateInstance(adapterType) as IHangfireBackgroundWorker;

                RecurringJob.AddOrUpdate(() => workerAdapter.ExecuteAsync(), GetCron(period.Value));
            }
        }
예제 #5
0
    public void Add(IBackgroundWorker worker)
    {
        if (worker is IHangfireBackgroundWorker hangfireBackgroundWorker)
        {
            if (hangfireBackgroundWorker.RecurringJobId.IsNullOrWhiteSpace())
            {
                RecurringJob.AddOrUpdate(() => hangfireBackgroundWorker.DoWorkAsync(),
                                         hangfireBackgroundWorker.CronExpression);
            }
            else
            {
                RecurringJob.AddOrUpdate(hangfireBackgroundWorker.RecurringJobId, () => hangfireBackgroundWorker.DoWorkAsync(),
                                         hangfireBackgroundWorker.CronExpression);
            }
        }
        else
        {
            int?period;

            if (worker is AsyncPeriodicBackgroundWorkerBase or PeriodicBackgroundWorkerBase)
            {
                var timer = (AbpTimer)worker.GetType()
                            .GetProperty("Timer", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(worker);
                period = timer?.Period;
            }
예제 #6
0
        internal SentryClient(
            SentryOptions options,
            IBackgroundWorker worker)
        {
            _options = options ?? throw new ArgumentNullException(nameof(options));

            options.SetupLogging(); // Only relevant if this client wasn't created as a result of calling Init

            if (worker == null)
            {
                var composer = new SdkComposer(options);
                Worker = composer.CreateBackgroundWorker();
            }
            else
            {
                options.DiagnosticLogger?.LogDebug("Worker of type {0} was provided via Options.", worker.GetType().Name);
                Worker = worker;
            }
        }
예제 #7
0
        protected virtual async Task ReScheduleJobAsync(IBackgroundWorker worker)
        {
            if (worker is IQuartzBackgroundWorker quartzWork)
            {
                Check.NotNull(quartzWork.Trigger, nameof(quartzWork.Trigger));
                Check.NotNull(quartzWork.JobDetail, nameof(quartzWork.JobDetail));

                if (quartzWork.ScheduleJob != null)
                {
                    await quartzWork.ScheduleJob.Invoke(_scheduler);
                }
                else
                {
                    await DefaultScheduleJobAsync(quartzWork);
                }
            }
            else
            {
                var adapterType = typeof(QuartzPeriodicBackgroundWorkerAdapter <>).MakeGenericType(worker.GetType());

                var workerAdapter = Activator.CreateInstance(adapterType) as IQuartzBackgroundWorkerAdapter;

                workerAdapter?.BuildWorker(worker);

                if (workerAdapter?.Trigger != null)
                {
                    await DefaultScheduleJobAsync(workerAdapter);
                }
            }
        }