crunzphp / crunz

A PHP-based job scheduler
MIT License
176 stars 16 forks source link

Any way to delete a php script task file after its execution #52

Open soc8ren opened 1 year ago

soc8ren commented 1 year ago

First, thanks for this awesome library. I am using it for learning purposes. I have a few tasks in my tasks directory, all of which are php files running with the PHP BINARY by Crunz. I was wondering if there is a way to delete the php file after its execution.

Things that I have tried to achieve this:-

  1. Tried adding UNLINK(__FILE__); to the task php. But, I can't have it before return $schedule as the script won't complete and the task file gets deleted. I can't have it afterwards either, as anything after return $schedule doesn't run.
  2. I also tried running the scheduler to delete the file $schedule->run(PHP_BINARY . -r 'UNLINK(__FILE__'); But, __FILE__ here returns a value of Command line code and I tried various versions of it, but none of them work.

I am trying to self destroy the task once it has been executed from within the task php file itself. I am aware of using sh or bash to do this. But, I would really love to know if there was a way to achieve this from within the PHP file itself.

PabloKowalczyk commented 1 year ago

I think you could try to call unlink in Closure passed to \Crunz\Event::after.

soc8ren commented 1 year ago

I think you could try to call unlink in Closure passed to \Crunz\Event::after.

Thanks for this. But, upon rechecking, I had totally forgotten about the pre and post-hooks functionality. I will also have to try if after on schedule would work. WiIll report back.