Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-numeric identifier for run individual task #33

Open
judgedim opened this issue Sep 9, 2022 · 10 comments
Open

Non-numeric identifier for run individual task #33

judgedim opened this issue Sep 9, 2022 · 10 comments

Comments

@judgedim
Copy link

judgedim commented Sep 9, 2022

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
Copy link
Member

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

@markorbida
Copy link

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
Copy link

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
Copy link
Member

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

@jszczypk
Copy link

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
Copy link
Member

@jszczypk You can also create a PR :)

@jszczypk
Copy link

I will. :)

@lucatacconi
Copy link
Contributor

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
Copy link
Contributor

@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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants