crunz icon indicating copy to clipboard operation
crunz copied to clipboard

Non-numeric identifier for run individual task

Open judgedim opened this issue 3 years ago • 10 comments

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    |
+---+------------+-----------------------+-------------+----------------+

judgedim avatar Sep 09 '22 15:09 judgedim

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

PabloKowalczyk avatar Sep 19 '22 06:09 PabloKowalczyk

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.

markorbida avatar Nov 08 '22 12:11 markorbida

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

jszczypk avatar Oct 14 '23 08:10 jszczypk

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

PabloKowalczyk avatar Oct 15 '23 10:10 PabloKowalczyk

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.

jszczypk avatar Oct 15 '23 13:10 jszczypk

@jszczypk You can also create a PR :)

PabloKowalczyk avatar Oct 15 '23 13:10 PabloKowalczyk

I will. :)

jszczypk avatar Oct 15 '23 13:10 jszczypk

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 avatar Oct 15 '23 19:10 lucatacconi

@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?

lucatacconi avatar Nov 21 '23 15:11 lucatacconi

@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.

PabloKowalczyk avatar Dec 02 '23 11:12 PabloKowalczyk