user18182496
user18182496

Reputation:

Laravel 9 - task scheduler controller function

Hello i'm looking on how to execute a controller function with a cronjob. I've read the documentation but it doesn't explain on how to call a controller function.

I have this function:

    function oracle(Request $request)  
{
    // some get-insert statements using DB Facade
}

Let's say i want to run this function every 10 minutes for each db record that has status = 0

then in laravel scheduler something like:

     $schedule->call(function () {
       //.. DB query to check all orders that have status = 0
       //.. for each record that returns 0
       //.. execute oracle()
       }
    })->everyMinute();

Basically all orders with status 0 are on mysql, now the oracle() logic triggers on a manual click. This function gets data from mysql and inserts to the oracle database. Once the record is on oracle it returns status 1 to mysql. I have to do this with a cron.

Which way should i go for?

Upvotes: -1

Views: 927

Answers (1)

Maksim
Maksim

Reputation: 2957

You need a Service Layer in your application to do this. Nice example here.

You must:

  • Move logic from Controller to Service
  • Call this service in Controller
  • Call this service in sheduler.

Upvotes: 1

Related Questions