mrahhal / MR.AspNetCore.Jobs

A background processing library for Asp.Net Core.
MIT License
59 stars 9 forks source link

Assembly scanning for CronJobRegistry option #14

Closed kornect closed 6 years ago

kornect commented 6 years ago

Hi,

First, thanks for the great library, I was able to get up and running in no time with background jobs. I use AutoFac to separate my application modules each with its own cron jobs that need to be run. I would like to be able to scan these modules and add register the cron jobs they contain, some functionality to wrap options.UseCronJobRegistry<T>(); with something like options.UseAssemblyCronJobRegistry(Assembly); or something similar.

Not sure if this will be a great feature, too specific to my need or I'm just lazy to keep writing the assembly scanning myself (I'm planning to use this library on more than one project), If it is, I'm willing to work on the extension to perform this.

mrahhal commented 6 years ago

Hi,

This shouldn't be too hard to do, especially if you're writing the scanning code already. Find all types that extend CronJobRegistry, and call UseCronJobRegistry on them. The method should probably be called UseCronJobRegistries(Assembly).

I'd be happy to accept contributions if you want to work on it.

kornect commented 6 years ago

So after playing around with a few tricks I decided that it's best to provide an extension to JobsOptions class to register the cron jobs, simple and not too fancy. Take a look at this and tell me what you think.

    public static class JobsOptionsExtensions
    {
        /// <summary>
        /// Registers the cron jobs registries within the assembly
        /// </summary>
        /// <param name="assembly">Assembly containing classes extending from <see cref="CronJobRegistry"/>.</param>
        public static void UseCronJobRegistries(this JobsOptions jobsOptions, Assembly assembly)
        {
            // get all classes extending cron job registry
            var types = assembly.GetTypes()
                .Where(type => typeof(CronJobRegistry)
                .IsAssignableFrom(type));

            foreach (var type in types)
            {
                var registry = Activator.CreateInstance(type) as CronJobRegistry;
                jobsOptions.UseCronJobRegistry(registry);
            }
        }

        /// <summary>
        /// Registers the cron jobs registries within the assembly
        /// </summary>
        /// <param name="assemblies">Assembly containing classes extending from <see cref="CronJobRegistry"/>.</param>
        public static void UseCronJobRegistries(this JobsOptions jobsOptions, IEnumerable<Assembly> assemblies)
        {
            foreach (var assembly in assemblies)
            {
                jobsOptions.UseCronJobRegistries(assembly);
            }
        }
    }

This works for me and I was able to register all registries in different assemblies.

mrahhal commented 6 years ago

I was going to suggest an extension, but thought against it because it really doesn't matter when it's on JobsOptions.

But yeah, looks good, and guess we can go for an extension. Looks great 👍

kornect commented 6 years ago

Sorted with PR: https://github.com/mrahhal/MR.AspNetCore.Jobs/pull/15 Thanks @mrahhal