crunzphp / crunz

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

Non-numeric identifier for run individual task #33

Open judgedim opened 2 years ago

judgedim commented 2 years ago

Description
Currently we can run individual task only using dynamic numeric id -t, --task=TASK Which task to run. Provide task number from schedule:list command. Which does not guarantee that it does the same task every time.

What do you think for introduce non-numeric identifier which can be used alongside with numeric id for run command in more predictable way?

Example

<?php

use Crunz\Schedule;

$schedule = new Schedule();
$task = $schedule->run(PHP_BINARY . ' backup.php', ['--destination' => 'path/to/destination']);
$task
    ->identifier('backup')
    ->description('Copying the project directory');

return $schedule;

Usage:

Run schedule:run --task=backup

List crunz schedule:list


+---+------------+-----------------------+-------------+----------------+
| # | Identifier |            Task       | Expression  | Command to Run |
+---+------------+-----------------------+-------------+----------------+
| 1 |  backup    | Task description      | 0 * * * 1 * | scripts.php    |
+---+------------+-----------------------+-------------+----------------+
PabloKowalczyk commented 1 year ago

Hello, I thinking about that in the past and I'm not against it, out of curiosity what is your use case?

markorbida commented 1 year ago

My use case for this is I need to run some jobs on multiple servers/containers. I have worker processes pulling from a Redis queue.

jszczypk commented 11 months ago

I second that. I have two use cases:

  1. for collecting statistics about task (last run time, last elapsed time in seconds etc.) and storing them in db, there should be some constant identifier that will allow linking rows from schedule:list to database records
  2. my tasks list will come from database so their identifiers can change when the list will change, but I would like give users possibility to launch some tasks manually from UI even between scheduled running times
PabloKowalczyk commented 11 months ago

OK guys, would you consider sponsoring (financially) this feature?

jszczypk commented 11 months ago

Not really. :( But I have started to look into it yesterday. I will try to use existing id property of Event, which is now assigned randomly to be able to pass it through Scheduler->run (making sure that it is unique), list it in schedule:list and use it for schedule:run -t xxx. For the latter it will be required that assigned id will be non-numeric otherwise it will treat it as TaskNumber.

PabloKowalczyk commented 11 months ago

@jszczypk You can also create a PR :)

jszczypk commented 11 months ago

I will. :)

lucatacconi commented 11 months ago

For my Crunz-ui repository I needed a unique identifier for each task and I used a system like this:

$TASKS_DIR = 'XXXXX' // -> represents the path of the folder where the tasks files are contained

$row = [];
$row["filename"] = $taskFile->getFilename();
$row["real_path"] = $taskFile->getRealPath();
$row["subdir"] = str_replace( array( $TASKS_DIR, $row["filename"]),'',$row["real_path"]);
$row["task_path"] = str_replace($TASKS_DIR, '', $row["real_path"]);

$row["event_id"] = $oEVENT->getId();
$row["event_launch_id"] = $task_counter;
$row["event_file_id"] = $event_file_id;
$row["task_description"] = $oEVENT->description;
$row["expression"] = $oEVENT->getExpression();

$row["event_unique_key"] = md5($row["task_path"] . $row["task_description"] . $row["expression"]);

This method is a bit rustic but it worked in my case.

The contraindication is that if the event changes the description or configuration of the execution moment or the path within the folder where the tasks are contained, the event changes its unique id.

However, it works better than the id obtained from the getId() function which essentially changes every time a new event is added.

If you have better suggestions, they would help me too.

lucatacconi commented 9 months ago

@PabloKowalczyk, I would like to introduce, in the Event Class, a method to obtain a unique non-numeric identifier for the task that does not change even if new tasks are inserted.

I tested getId() method but the returned value changes every time new tasks are inserted.

I would like to use a similar method as shown before.

I would like to use the result of getExpression() method and the content of 'description' variable as parameters for calculating the unique key.

However, I need another parameter that allows me to uniquely identify the event (with these two parameters only I risk calculating the same ID for two tasks with the same description and the same launch date and time maybe in different subdir in tasks directory).

Do you think this could be a good idea? What do you think I can use as a parameter to better identify a task?

PabloKowalczyk commented 9 months ago

@lucatacconi I think it may work, feel free to open PR :)

Next step, after "stable ids", can be user-provided ids, but this needs more work.