xfra35 / f3-cron

Job scheduling for the PHP Fat-Free Framework
GNU General Public License v3.0
72 stars 22 forks source link

Force synchronous mode for all tasks #6

Closed Pilskalns closed 6 years ago

Pilskalns commented 6 years ago

Hi,

I try to develop from Windows (will be hosting on Unix), but plugin tries to run tasks in async mode. Is there a way to force run all tasks in sync mode?

I have tried both php index.php /cron and trough browser http://local.domain/cron

Pilskalns commented 6 years ago

Hmm... and is there practical reason why plugin is not calling beforeRoute() of routed class? :)

xfra35 commented 6 years ago

Hi @Pilskalns,

it's not possible at the moment but I guess it should be possible to add an option, like /cron?async=false.

May I ask why you need to run all tasks synchronously?

xfra35 commented 6 years ago

is there practical reason why plugin is not calling beforeRoute()

Sure: the plugin is not doing any routing ^^

Routing is the process of mapping each web resource to a piece of code. That's what is happening when you call $f3->run().

This plugin is doing something different: it executes due tasks. There's no routing involved.

Pilskalns commented 6 years ago
  1. sync/async

    May I ask why you need to run all tasks synchronously?

I develop on Windows, async mode not working with /cron. When using /cron/@jobID it forces for specific task sync mode and works. But then it looses point of using scheduled timing from config.

When calling in async mode from CLI, I get error, which comes somewhere from PHP itself: The process cannot access the file because it is being used by another process.

  1. beforRoute() Thanks for the clarification. I use it to setup database connections, hive keys etc. common things to autoloaded class.

Then I guess __construct() for autoloaded class be better place for it. Though currently did workaround adding self::beforeRoute($f3); to functions called from f3-cron.

Pilskalns commented 6 years ago

P.S. /cron?async=false does not get mapped trough up to run() function in f3-cron.

Though I can try make this working and then do PR.

xfra35 commented 6 years ago

So if I understand you well, you need sync mode only because async mode fails on Windows.

I'll check if I can have async mode work on Windows and if not, I'll disable it.

Pilskalns commented 6 years ago

For now, to do all tasks in sync mode I was able to do simple fix, which works both - CLI and web.

image

Pilskalns commented 6 years ago

Hi @xfra35,

I have put previous and Windows async feature on forked branch

Everything works from CLI and web. I have two tasks working with DB:

  1. Long running querying external API for data
  2. Quick Generating report and updating another table
xfra35 commented 6 years ago

Hi @Pilskalns,

I just added support for Windows to the plugin. Can you download the latest release and see if it fixes it up?

Having async mode working on Windows, you shouldn't need the sync flag anymore.

Pilskalns commented 6 years ago

Hello @xfra35,

Technically it works, but it flashes a window for each async task run. It stays open until task finished. It can be suppressed by using adding background switch like start /b to start command.

xfra35 commented 6 years ago

Thanks for reporting. Fixed in https://github.com/xfra35/f3-cron/commit/af0b4fd81b2d8d129da32a70ec0adbb4bc66eee0