Simon Simon - 3 months ago 12
JSON Question

Background process to update DB every few seconds with JSON data

I am a complete novice with this but...

I have a small ASP MVC C# application reading an SQL database which I would like to be updated by a background process updating the DB with a JSON request potentially up to every minute or few seconds.

What is the best way to implement the background JSON DB update? In the MVC app on a persistent timer (is that even possible?) or independently (completely outside of the app) in a separate process with an executable running in the background with an internal programmatic timer or else using some kind of scheduler?

EDIT: For the sake of understanding - it is market prices in the JSON string that obviously need to be updated in the DB quite often ie potentially up to every few seconds if desirable or nessesary

Answer

I would use a Windows Service combined with the Quartz.net package.

You can run anything you want and on any schedule.

EDIT: From the above discussion I gather that your job would poll for market prices (a web request) that would run every few seconds and on getting the result would update your database.

EDIT2:

This would be your Quartz job:

public class FetchAndSaveFinancialData : IJob
{
   public void Execute()
   {
       //web request to get info

       //save to db
   }
}

Then your windows service base class:

public class YourFinancialServiceBase : ServiceBase
{
   protected override void OnStart(string[] args)
   {
      ServiceMain();
      base.OnStart(args);
   }
   protected override void OnStop()
   {
      base.OnStop();
   }
   protected void ServiceMain()
   {
       var scheduler = StdSchedulerFactory.GetDefaultScheduler();

       var job = JobBuilder.Create<FetchAndSaveFinancialData>().WithIdentity("Job1", "Group1").Build();

       ITrigger trigger = TriggerBuilder.Create().WithIdentity("Trigger1","Group1")
           .StartNow()
           .WithSimpleSchedule(x=>x
             .WithIntervalInSeconds(5)
             .RepeatForever()
            ).Build();

       scheduler.ScheduleJob(job,trigger);

       scheduler.Start();
   }

}
Comments