Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to retrieve a list of jobs which are currently being scheduled? #197

Closed
sambunting opened this issue Mar 21, 2018 · 2 comments

Comments

@sambunting
Copy link

Is there a way to retrieve the IJob object for the scheduled job based of the specific name?

I was hoping I would be able to do this by calling a method such as JobManager.GetSchedule("name").ScheduledJobs(). This would return a List of IJobs which are yet to be executed.

Is there a way to do this? Or would I have to look into other ways such as storing jobs in a database.

Thanks

@MarkusKgit
Copy link

I don't think you can get the IJob objects as they are internally stored as List<Action>. If you just want to execute the Jobs though then you can do it with a bit of reflection:

internal class Program
    {
        private static void Main(string[] args)
        {
            var registry = new Registry();
            registry.Schedule<CountingJob>().WithName("Job1").ToRunEvery(10).Seconds();
            JobManager.Initialize(registry);
            var schedule = JobManager.GetSchedule("Job1");
            var scheduleType = schedule.GetType();
            var jobs = scheduleType
              .GetProperty("Jobs", BindingFlags.NonPublic | BindingFlags.Instance)
              .GetValue(schedule) as List<Action>;
            var job = jobs.FirstOrDefault();
            for (int i = 0; i < 5; i++)
            {
                job.Invoke();
            }
            Console.ReadLine();
        }

        internal class CountingJob : IJob
        {
            private static int counter;

            public void Execute()
            {
                Console.WriteLine(++counter);
            }
        }
    }

@tallesl
Copy link
Contributor

tallesl commented Sep 2, 2018

On the library redesign (#214) you'll own your schedules, so you won't have this problem there.

@tallesl tallesl closed this as completed Sep 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants