taiko
taiko

Reputation: 478

RESTful service to make a request to foreign API and update SQL Server database

I want to make a RESTful API (or any other way that can get it done, really) to have it work in a loop to do a specified task everyday at the same hour.

Specifically, I want it to access a foreign API, let's say, at midnight everyday, request the specified data and update the database accordingly. I know how to make a request to an API and make it do something. But I want it to do it automatically so I don't even have to interact with it, not even having to make requests.

The reason for this is that I'm working on a project that requires multiple platforms (and even if it was only one platform the users would be several) and I can't make a request to a foreign API (mainly because it's trial, it's a school project) every time a user logs in or clicks a button on each platform.

I don't know how to even do that (or if it's even possible) with a web service. I've tried with a web form doing it async with BackgroundWorker but nothing.

I thought I might have better luck here with more experienced people.

Hope you can help me out.

Thanks, in advance, Fábio.

Upvotes: 0

Views: 97

Answers (2)

taiko
taiko

Reputation: 478

I managed to get there, thanks to the help of @Pedro Gaspar - LoboFX. I didn't want the Windows Scheduler as I want it reflected on the code and I don't exactly have access to the server where it's going to be. That said, what got me there was something like this:

private static string LigacaoBD="something";
    private static Perfil perfil = new Perfil(LigacaoBD);

    protected void Page_Load(object sender, EventArgs e)
    {
        Task.Factory.StartNew(() => teste());

    }

    private void teste()
    {

        bool verif = false;

        while (true)
        {
            if (DateTime.UtcNow.Hour + 1 == 22 && DateTime.UtcNow.Minute == 12 && DateTime.UtcNow.Second == 0)
                verif = false;

            if (!verif)
            {
                int resposta = perfil.Guardar(DateTime.UtcNow.ToString());
                verif = true;

            }
            Thread.Sleep(1000);
        }
    }

It's inserting into the database through a class library. And with this loop it garantees that it only inserts once (hence the bool) and when it gets to the specified hour, minute and second it resets, allowing it to insert again. If something happens that the servers goes down, when it gets back up it inserts anyway. The only problem is that if it's already inserted and the server goes down it will insert again. But for that there are stored procedures. Well, not for the DateTime.UtcNow.ToString() but that was just a test.

Upvotes: 0

Pedro Gaspar
Pedro Gaspar

Reputation: 878

Don't know if I get it right, but it seems to me that the easiest way to do what you want (have a program scheduled to work at a given time, every day) is to use Windows Scheduler to schedule your application to run always on the specific time you want.

Upvotes: 1

Related Questions