Code Monkey home page Code Monkey logo

Comments (16)

kdcllc avatar kdcllc commented on May 28, 2024 1

@matteofabbri and @tomkerkhove,

I released a preview version (https://f.feedz.io/kdcllc/cronscheduler-aspnetcore/nuget/index.json) that supports the following:

  1. Job class
public class TestJob : IScheduledJob
    {
        private SchedulerOptions _options;
        private readonly ILogger<TestJob> _logger;

        public TestJob(SchedulerOptions options, ILogger<TestJob> logger)
        {
            _options = options;
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
        }

        // will be removed in the next release
        public string CronSchedule { get; }

        // will be removed in the next release
        public string CronTimeZone { get; }

        // will be removed in the next release
        public bool RunImmediately { get; }

        // will be removed in the next release
        public Task ExecuteAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("{cronSchedule} - {id}", _options.CronSchedule, Guid.NewGuid());

            return Task.CompletedTask;
        }
    }
  1. In the Controller or any other class you would have to provide the injection of the constructor:
public HomeController(
            ISchedulerRegistration schedulerRegistration,
            ILoggerFactory loggerFactory)
{
            _schedulerRegistration = schedulerRegistration;
            _loggerFactory = loggerFactory;
}

public IActionResult Index()
{
            var jobOptions = new SchedulerOptions
            {
                CronSchedule = "0/1 * * * * *",
                RunImmediately = true
            };

         _schedulerRegistration.AddOrUpdate(new TestJob(jobOptions, 
        _loggerFactory.CreateLogger<TestJob>()), jobOptions);
}

Please let me know if this what you have in mind?

thanks, kdcllc

from cronscheduler.aspnetcore.

kdcllc avatar kdcllc commented on May 28, 2024

@matteofabbri the short answer is yes, there are two types of job. Here is an example of scheduled job https://github.com/kdcllc/CronScheduler.AspNetCore/blob/master/src/CronSchedulerApp/Jobs/TorahQuoteJob.cs

from cronscheduler.aspnetcore.

matteofabbri avatar matteofabbri commented on May 28, 2024

Sorry but I dont understand, the one that you posted is used at startup, maybe you were talking about UserJob ?

what i mean is having a controller like this

public MyController(ISchedulingService service)
{
     this._service = service;
}

public ActionResult Index()
{
      _service.Add(new ScheduledJob());
}

from cronscheduler.aspnetcore.

tomkerkhove avatar tomkerkhove commented on May 28, 2024

I'm late to the game but is there a way to register jobs with a factory approach on startup?

Noticed that this got deprecated in 2.1 but without a replacement which is blocking us from upgrading it as we depend on that.

from cronscheduler.aspnetcore.

kdcllc avatar kdcllc commented on May 28, 2024

@tomkerkhove are you referring to this extension method?

[Obsolete("This method will be depreciated in the next release.")]
        public IServiceCollection AddJob<TJob>(Func<IServiceProvider, TJob> factory, string sectionName = "SchedulerJobs")
            where TJob : class, IScheduledJob
        {
            Services.AddSingleton(typeof(IScheduledJob), factory);

            var name = typeof(TJob).Name;

            // named options used within the scheduler itself.
            Services.AddChangeTokenOptions<SchedulerOptions>($"{sectionName}:{typeof(TJob).Name}", name, _ => { });

            return Services;
        }

The idea behind the depreciation was that IScheduledJobs interface will change. I was thinking completely removing configuration of the jobs from that interface and move them to Options instead, So in principle I could still keep this extension method as long as configurations are set thru the SchedulerOptions.

from cronscheduler.aspnetcore.

tomkerkhove avatar tomkerkhove commented on May 28, 2024

I'm not using SchedulerOptions at the moment but I can move to that if it would make it easier for you.

from cronscheduler.aspnetcore.

kdcllc avatar kdcllc commented on May 28, 2024

@tomkerkhove can you provide me with a sample code? It would be helpful to make sure that I consider this case scenario moving forward.

from cronscheduler.aspnetcore.

tomkerkhove avatar tomkerkhove commented on May 28, 2024

Sure!

from cronscheduler.aspnetcore.

tomkerkhove avatar tomkerkhove commented on May 28, 2024

Let me know what you think because going with Options is less ideals since it's not coming from configuration.

from cronscheduler.aspnetcore.

kdcllc avatar kdcllc commented on May 28, 2024

@tomkerkhove it can come from different sources. I am leaning towards keeping the interface in tack and removing obsolete attribute from factory extension method.

from cronscheduler.aspnetcore.

tomkerkhove avatar tomkerkhove commented on May 28, 2024

That would be ideal as our current approach should keep on working then, but don't feel obliged because of just me...

from cronscheduler.aspnetcore.

tomkerkhove avatar tomkerkhove commented on May 28, 2024

Any update on the way forward for my scenario @kdcllc?

from cronscheduler.aspnetcore.

kdcllc avatar kdcllc commented on May 28, 2024

@tomkerkhove I have been working on and off on the solution and unfortunately I am settled on breaking changes at this time.

  • IScheduledJob interface will look as follows:
    /// <summary>
    /// Forces the implementation of the required methods for the job.
    /// </summary>
    public interface IScheduledJob
    {
        /// <summary>
        /// The name of the executing job.
        /// </summary>
        string Name { get; }

        /// <summary>
        /// Job that will be executing on this schedule.
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task ExecuteAsync(CancellationToken cancellationToken);
    }
  • Options are configured based on Job Name
 services.AddScheduler(ctx =>
                {
                    var jobName = nameof(TestJob);

                    ctx.AddJob(
                        sp => new TestJob(mockLogger.Object),
                        configure: options =>
                        {
                            options.CronSchedule = "*/05 * * * * *";
                            options.RunImmediately = false;
                        },
                        jobName: jobName);
                });

I looked over your cases and the migration should be simple. Instead of configuring IScheduledJob properties inside of the service, the options now can be configured at configurations level as demonstrated above. The main advantage in your application will be: jobs' configurations are now monitor for changes and update themselves accordingly.

Let me know your thoughts, I will try to wrap it up today or tomorrow,

from cronscheduler.aspnetcore.

tomkerkhove avatar tomkerkhove commented on May 28, 2024

Sounds reasonable, thanks for explaining! Is this already available or do I have to wait for 3.0?

from cronscheduler.aspnetcore.

kdcllc avatar kdcllc commented on May 28, 2024

@tomkerkhove I released 3.0.0-preview1. I am not expecting any major changes. I am updating documentation at the moment.

from cronscheduler.aspnetcore.

tomkerkhove avatar tomkerkhove commented on May 28, 2024

Worked like a charm, thanks @kdcllc and sorry for hijacking this issue; just think about that now :/

from cronscheduler.aspnetcore.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.