Execute cron that creates a new record using Entity

Hi!

The application needs to query an external api every 60 seconds to check if it has new orders. If have new orders, it must create a new record in the orders database.

For this I must create a routine to run using cron from linux.

Where should I put the php file of this routine and how do I access the database of this routine via Entity of cakephp? Is there a sample?

Thanks in advance,
jjvg

Basic CLI tools can be implemented in Commands.

But in your case I would personally rather go with a queueing system like GitHub - dereuromark/cakephp-queue: Queue plugin for CakePHP - simple, pure PHP and without dependencies. or GitHub - dereuromark/cakephp-queue: Queue plugin for CakePHP - simple, pure PHP and without dependencies.

Because what would you do if

  • your API doesn’t respond after 60 seconds/at all?
  • responds with such a huge data-set that you can’t process it in 60 seconds.

You don’t want to re-fetch data from the API if you haven’t processed the “old” request.

Regarding “accessing the database”: In CakePHP you should never directly access the database. Otherwise why would you use a MVC framework with an ORM?

What you need is the LocatorAwareTrait explained in Query Builder - 4.x
With that you can get a table instance which behaves the same like you have in your generated controllers from bin/cake bake.

So like

public function execute(Arguments $args, ConsoleIo $io)
{
    $tableInstance = $this->getTableLocator()->get('Categories');
    $query = $tableInstance->find();
}

is the same as in your controller doing

public funtion index()
{
    $query = $this->Categories->find();
}

With that table instance you can basically do all CRUD operations which are already present in your controller.

Kevin, thanks for your fast answer.

The two queueing system looks pointing to the same link :slight_smile:

Thank you!

Right sorry, the second one should have been GitHub - cakephp/queue: A queue-interop compatible Queueing library

2 Likes

Which one would you recommend? or does one plugin has some advantages over the other? I’ve read both documentations but since I’ve never implemented something like this, I don’t know which one should I use.

My goal is to read mysql data from several tables, apply some formulas and create records in another table.

I personally prefer dereuromark/cakephp-queue but its just because I am used to it.

But before that you should get used to Cakephp and how its ORM works so you know how to “create records in another table”

2 Likes

Thank you Kevin!

I’m trying to use dereuromark/cakephp-queue.

  • Add cakephp-queue to the project
    composer require dereuromark/cakephp-queue

  • Load plugin src/Application.php’s bootstrap() using:
    $this->addPlugin('Queue');

  • Create the tables using the Migrations plugin:
    bin/cake migrations migrate -p Queue

  • Configuration: create a file called app_queue.php inside your config folder

return [
    'Queue' => [
        'workermaxruntime' => 60,
        'sleeptime' => 15,
    ],
];
  • I created a task pollingOrdersTask.php that extends Queue\Queue\Task and implements the run method, then I placed it in src/Queue/Task/ folder.

  • bin/cake queue add pollingOrdersTask
    returns that it can not add pollingOrdersTask task

I’m a bit lost here. :slight_smile:

you need to start the queue worker via

bin/cake queue run

this will then process all queued jobs which have not yet been processed.

1 Like

If you want to add tasks via CLI you need to implement the AddInterface which is described here.

But I usually just call the $this->QueuedJobs->createJob() function to add tasks to the queue in controller actions.

1 Like