You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey,
I managed to get a simple example working in my ASP.NET Core project, but once I tried to make the creation of my triggers and jobs a bit more complicated I can't get my jobs to run.
publicstaticvoidAddStoreCrawlers(thisIServiceCollectionservices,boolbDevExecute){List<JobKey>jobKeys=new(){
services.RegisterJob<CrawlerExample>(_sStoreGroup),};
services.AddQuartz(q =>{ q.AddTrigger(options =>{ options.WithIdentity($"{_sStoreGroup}_Trigger", _sStoreGroup);if(bDevExecute){// Start once immediately for development environment options.WithSimpleSchedule(x => x.WithIntervalInSeconds(5).WithMisfireHandlingInstructionFireNow().WithRepeatCount(0));}else{// Run every month options.WithCronSchedule("0 0 0 1 * ? *");}foreach(var jobKey in jobKeys) options.ForJob(jobKey);});});}privatestatic JobKey RegisterJob<T>(thisIServiceCollectionservices,stringgroupName)whereT:CrawlerBase{JobKeyjobKey=new JobKey($"{typeof(T).Name}_Job", groupName);
services.AddQuartz(q =>{ q.AddJob<T>(options =>{ options.WithIdentity(jobKey);});});returnjobKey;}
And lastly my example of job:
publicclassCrawlerBase:IJob{publicasync Task Execute(IJobExecutionContextcontext){try{await Crawl();// SOME OTHER STUFF - for example logging to my UI interface or something elseawait Task.FromResult(true);}catch{await Task.FromResult(false);}}protectedvirtual Task Crawl(){return Task.CompletedTask;}}publicclassCrawlerExample:CrawlerBase{privatereadonlyIStoreRepository_storeRepository;publicCrawlerExample(IStoreRepositorystoreRepository){_storeRepository=storeRepository;}protectedoverrideasync Task Crawl(){// DO STUFF}}
My plan is to have a lot of small jobs that need to run every month and all of them do very similar stuff - that's why the abstraction.
I am basically creating web crawlers that need to get information into my database every month.
Additionally, I would like to be able to toggle if I want my crawlers to run once immediately when I am in debug mode - or for example force trigger of one job somehow so I can work in this project - this part sadly fails. I do believe that I managed to trigger jobs like this before adding dependency injection and with interval set for every 30s.
Am I approaching this in wrong way?
(I've been also considering if it's smart to have it as part of my API code as most of documentation isn't for ASP.NET Core - maybe have these jobs as separate project?)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hey,
I managed to get a simple example working in my ASP.NET Core project, but once I tried to make the creation of my triggers and jobs a bit more complicated I can't get my jobs to run.
In my
Program.cs
I do following:This is how I add my triggers and jobs:
And lastly my example of job:
My plan is to have a lot of small jobs that need to run every month and all of them do very similar stuff - that's why the abstraction.
I am basically creating web crawlers that need to get information into my database every month.
Additionally, I would like to be able to toggle if I want my crawlers to run once immediately when I am in debug mode - or for example force trigger of one job somehow so I can work in this project - this part sadly fails. I do believe that I managed to trigger jobs like this before adding dependency injection and with interval set for every 30s.
Am I approaching this in wrong way?
(I've been also considering if it's smart to have it as part of my API code as most of documentation isn't for ASP.NET Core - maybe have these jobs as separate project?)
Beta Was this translation helpful? Give feedback.
All reactions