---Presenter: Eric WeathersSubject: How to Configure Settings for Scheduled Tasks
Duration: 00:07:44---00:00:05.160 --> 00:00:11.793 - OK, so ultimately what we're kind of talking about here is 00:00:11.793 --> 00:00:13.602 - configuring settings for. 00:00:15.050 --> 00:00:18.030 - Scheduled tasks like when they're going to run and the 00:00:18.030 --> 00:00:21.308 - best practice for doing that. So initially when you kind of 00:00:21.308 --> 00:00:24.586 - open up a controller here or not a controller, I'm sorry. 00:00:25.840 --> 00:00:27.230 - Uh, OK. 00:00:28.680 --> 00:00:29.820 - The job service. 00:00:31.060 --> 00:00:35.920 - When you're adding a job, you need to set a Cron expression. 00:00:35.920 --> 00:00:41.185 - Basically for how often it runs, and you can look up it's. I 00:00:41.185 --> 00:00:46.045 - think the website is called like Crontab Guru I believe is the 00:00:46.045 --> 00:00:50.905 - website that that you can play around with the Cron settings to 00:00:50.905 --> 00:00:53.335 - kind of learn what those are. 00:00:54.660 --> 00:00:58.833 - But but ultimately, you need to set the Cron expression 00:00:58.833 --> 00:01:02.320 - here. So typically you know, sometimes you could just do it 00:01:02.320 --> 00:01:06.441 - like this, where you set where you set the value here. So you 00:01:06.441 --> 00:01:09.928 - could do like daily. Or you could do an actual string. 00:01:11.330 --> 00:01:14.810 - Of Cron setting, so like if you wanted to do every minute. 00:01:18.850 --> 00:01:21.931 - Like that, but the way we want to do that, 'cause This is 00:01:21.931 --> 00:01:25.012 - hard code and we don't want to hardcode that in there is that 00:01:25.012 --> 00:01:27.619 - we want to pull that out into settings. So let me. 00:01:29.920 --> 00:01:34.672 - So the first thing that we do is created options should already 00:01:34.672 --> 00:01:36.652 - be here called job service 00:01:36.652 --> 00:01:41.572 - options. And basically you set your you know you set your value 00:01:41.572 --> 00:01:45.436 - that you want to use your property. So in this case I'm 00:01:45.436 --> 00:01:48.656 - doing a product sync, so pretty simply just called product 00:01:48.656 --> 00:01:52.520 - synchron and then you just need to set that in the development 00:01:52.520 --> 00:01:56.062 - and the development Jason or in whatever your app settings are. 00:01:56.062 --> 00:02:00.248 - So I've added a section here to my app settings for job service 00:02:00.248 --> 00:02:03.146 - options, 'cause That's the name of my options class. 00:02:04.680 --> 00:02:08.136 - And then the name of the property with the value. So this 00:02:08.136 --> 00:02:12.168 - is going to run if you look up front on the Crontab Guru. This 00:02:12.168 --> 00:02:14.184 - is going to run every 15 minutes 00:02:14.184 --> 00:02:17.671 - by default. And that's when it's going to run 00:02:17.671 --> 00:02:20.128 - automatically. You can go into the hangfire dashboard and 00:02:20.128 --> 00:02:21.766 - trigger it to start whenever but. 00:02:22.820 --> 00:02:25.650 - But that's that's how we're going to. That's how we 00:02:25.650 --> 00:02:29.046 - start. That's how we start that off. OK, so now we need 00:02:29.046 --> 00:02:32.159 - to inject that in, so let me know whatever reason had. 00:02:37.450 --> 00:02:39.767 - We're going to go to the startup. 00:02:41.910 --> 00:02:42.760 - Glass. 00:02:43.860 --> 00:02:46.920 - And we're going to go to connect options, so interconnect 00:02:46.920 --> 00:02:49.980 - options. We have our configuration and we need to go 00:02:49.980 --> 00:02:53.958 - get our section of job service options so that we have. We have 00:02:53.958 --> 00:02:57.324 - those options here and then. We're going to add a Singleton 00:02:57.324 --> 00:03:00.996 - and the singles in again is the lifespan that you get one 00:03:00.996 --> 00:03:03.750 - instance of it throughout. Obviously this value is not 00:03:03.750 --> 00:03:06.810 - changing regularly, so it's not really going to change during 00:03:06.810 --> 00:03:10.176 - the life of the application. So we're just going to Singleton 00:03:10.176 --> 00:03:13.236 - and then we add that required service of the options. 00:03:13.570 --> 00:03:17.038 - We have that value in there, so now we have we have 00:03:17.038 --> 00:03:17.905 - basically this information. 00:03:19.180 --> 00:03:22.248 - Right here, stored as. 00:03:24.010 --> 00:03:25.390 - This class. 00:03:27.220 --> 00:03:30.091 - In our services or dependency injection container, which is 00:03:30.091 --> 00:03:34.876 - why we added to our services. So the next thing we can do is we 00:03:34.876 --> 00:03:38.066 - can inject those into our job service. So up here. 00:03:39.110 --> 00:03:43.118 - We have our job service options as a private readonly and then 00:03:43.118 --> 00:03:46.792 - we can inject those into the constructor of our job service 00:03:46.792 --> 00:03:51.813 - right here. So we have that value and as a single team we 00:03:51.813 --> 00:03:55.476 - just have the instance. So we have this value wherever we 00:03:55.476 --> 00:04:00.138 - need to use it. So once we have it as a read only, we've 00:04:00.138 --> 00:04:04.134 - literally just set that value here so we can run through a 00:04:04.134 --> 00:04:07.797 - real quick one of doing that. So let's say we're doing. 00:04:09.960 --> 00:04:12.090 - A process. 00:04:13.410 --> 00:04:14.450 - Orders. 00:04:28.640 --> 00:04:31.388 - So we're just not going to implement it, but that'll be OK. 00:04:31.388 --> 00:04:34.594 - So then we need to basically add it to our job manager here, so 00:04:34.594 --> 00:04:37.571 - this add or update is going to add it to our job manager. 00:04:38.520 --> 00:04:42.516 - So we'll still easy copy paste here, so we'll change our job 00:04:42.516 --> 00:04:47.178 - ID so that we have a different ID on the on the job. But 00:04:47.178 --> 00:04:51.174 - instead of an arm for our job, we're not going to obviously 00:04:51.174 --> 00:04:53.838 - process products. In this case, we're going to. 00:04:55.780 --> 00:04:57.320 - Sales orders. 00:04:58.900 --> 00:05:02.002 - And then we have our same recurring job options that get 00:05:02.002 --> 00:05:05.386 - set up here with just basically time time zone. You can set 00:05:05.386 --> 00:05:09.616 - other things and you can look up how to do that on get on, hang 00:05:09.616 --> 00:05:10.744 - on hang fires, GitHub. 00:05:11.920 --> 00:05:14.897 - But now we just need to change this value 'cause we don't want 00:05:14.897 --> 00:05:17.645 - to use the product synchron. Let's say we want to run the 00:05:17.645 --> 00:05:20.164 - sales orders every five minutes. So the first thing we're going 00:05:20.164 --> 00:05:22.912 - to do is we're going to job service options and we're going 00:05:22.912 --> 00:05:24.286 - to add a new property here. 00:05:26.180 --> 00:05:27.160 - Sales. 00:05:29.440 --> 00:05:30.100 - Run. 00:05:33.970 --> 00:05:37.610 - I've saved that, so now we need to add that sales order sync ran 00:05:37.610 --> 00:05:40.990 - into the values here in our job service options, so will add a 00:05:40.990 --> 00:05:44.630 - new line. So or did I call it to call sales order sales orders? 00:05:45.570 --> 00:05:46.620 - Sales order. 00:05:48.990 --> 00:05:49.270 - I'm. 00:05:50.780 --> 00:05:52.778 - We're going to run this every. 00:05:53.710 --> 00:05:55.090 - 5 minutes. 00:05:59.100 --> 00:06:02.733 - Now this options is already injected into 00:06:02.733 --> 00:06:04.290 - the startup container. 00:06:05.830 --> 00:06:09.890 - So we don't need to do that again. You only need to do that 00:06:09.890 --> 00:06:13.370 - once for the entire for the entire class, so will forgo that 00:06:13.370 --> 00:06:16.560 - option. But then the next and then. So the following thing 00:06:16.560 --> 00:06:19.750 - after that would be to inject it into your constructor. Again, 00:06:19.750 --> 00:06:23.520 - we've already done that, so now I can just keep adding on to 00:06:23.520 --> 00:06:27.000 - this. And I can say instead of job service dot options dot 00:06:27.000 --> 00:06:30.480 - value dot product synchron can say sales order sync run, so now 00:06:30.480 --> 00:06:34.540 - this job is going to run every 15 minutes and this job is going 00:06:34.540 --> 00:06:35.990 - to run every five minutes. 00:06:36.860 --> 00:06:40.336 - And that's really it in terms of reviewing settings for the 00:06:40.336 --> 00:06:43.496 - scheduled tasks, like, that's really the only setting that we 00:06:43.496 --> 00:06:46.972 - would really want changes. How often, but what? This allows us 00:06:46.972 --> 00:06:50.764 - to do in this in this format is that obviously you're only 00:06:50.764 --> 00:06:54.556 - changing one value here. You don't need to do a rebuild, you 00:06:54.556 --> 00:06:58.348 - can change the value, turn the Apple off, turn that, pull back 00:06:58.348 --> 00:07:02.772 - on, and you're good to go. Or if it's hosted in Azure, you can 00:07:02.772 --> 00:07:06.564 - change the value and then just restart the Azure Service and it 00:07:06.564 --> 00:07:10.754 - will take. It will go live. So basically once once we hand once 00:07:10.754 --> 00:07:14.208 - we hand the whole you know application off to the client. 00:07:15.110 --> 00:07:17.882 - All of this is configurable and they say OK, well, every 00:07:17.882 --> 00:07:20.402 - five minutes isn't. We don't need to run every five 00:07:20.402 --> 00:07:22.670 - minutes. We're only getting orders every 15 minutes. They 00:07:22.670 --> 00:07:26.198 - can go back and change this to 15 minutes and be good to go. 00:07:27.450 --> 00:07:29.452 - So that's kind of it when it 00:07:29.452 --> 00:07:33.620 - comes down to. Settings so pretty simple, but. 00:07:35.320 --> 00:07:38.460 - But definitely definitely good to know and necessity when it 00:07:38.460 --> 00:07:42.856 - comes to handing this off to the client and not having to do much 00:07:42.856 --> 00:07:44.426 - do much more honest though.