Jump to content

Tasker


mtwebit

Recommended Posts

Tasker is a module to handle and execute long-running jobs in Processwire. It provides a simple API  to create tasks (stored as PW pages), to set and query their state (Active, Waiting, Suspended etc.), and to execute them via Cron, LazyCron or HTTP calls.

Creating a task

$task = wire('modules')->Tasker->createTask($class, $method, $page, 'Task title', $arguments);

where $class and $method specify the function that performs the job, $page is the task's parent page and $arguments provide optional configuration for the task.

Executing a task

You need to activate a task first

wire('modules')->Tasker->activateTask($task);

then Tasker will automatically execute it using one of its schedulers: Unix cron, LazyCron or TaskerAdmin's REST API + JS client.

Getting the job done

Your method that performs the task looks like

public function longTask($page, &$taskData, $params) {
  ...
}

where $taskData is a persistent storage and $params are run-time options for the task.

Monitoring progress, management

The TaskerAdmin module provides a Javascript-based front-end to list tasks, to change their state and to monitor their progress (using a JQuery progressbar and a debug log area). It also allows the on-line execution of tasks using periodic HTTP calls performed by Javascript.

TaskerAdmin.png.e535554a45673edd5dd1049b910585fe.png

 

Monitoring task progress (and log messages if debug mode is active)

TaskMonitor.png.912e5949fab4d8a61f39262e2e682ddd.png

 

Task data and log

TaskData.png.e5bd86b403dad7b2718cf4fdd39ec175.pngTaskLog.png.1795f8a454cba4d9756877e77b5ed820.png

Detailed info (setup, task dependencies, time limits, REST API etc.) and examples can be found on GitHub.

This is my first public PW module. I'm sure it needs improvement :)

 

  • Like 16
Link to comment
Share on other sites

hi @mtwebit welcome to the forum and thanks for that module! i've created something similar for one of my projects but it's custom and not built into a module so i think there's potential for what you are doing :)

some ideas/questions/feedback:

  • use InputfieldMarkup to structure your custom code and make it look&feel more native. see my new tutorial here: 
  • do you know the wirequeue module?
  • would be great to have an easy way to integrate this into other modules or custom pages. maybe it is already possible - i'll have a look when i have more time :)

This is how my progressbar looks like using InputfieldMarkup:

2017-11-09 16_20_28-Edit Page_.png

  • Like 4
Link to comment
Share on other sites

Thanks for the feedback. I'll definitely have a look at InputfieldMarkup and wirequeue.

It is possible to integrate the UI elements into other pages. There's a renderTaskList() method to perform this:

    if(wire('modules')->isInstalled("TaskerAdmin")) {
      $out .= '<h3>Tasks</h3>';
      $out .= wire('modules')->get('TaskerAdmin')->renderTaskList($selector);
    }

It can render a task list or a detailed task monitoring div including JS code to show the progressbar or even to execute the task when displaying the page.
It needs improvement, however, as its links point to the TaskerAdmin page atm and the list UI is not really customizable.

  • Like 1
Link to comment
Share on other sites

I'm doing something similar (far from being as awesome as your module) using only some hobbled up pages and templates, and it's getting out of hand. This looks worthwhile to dig around in. I'll try it out and try to give helpful feedback. Thanks for sharing. :) 

Link to comment
Share on other sites

  • 11 months later...

Thank you @mtwebit for that module! 

But I have a trouble with processwire setup with two site folders: "site" and "site-users". Tasker module is installed  in site-users/modules folder.

How to force processwire to load that modules instead of site/modules? Because runByCron.sh can't find Tasker module:

require "index.php";

if (!wire('modules')->isInstalled('Tasker')) {
echo 'Tasker module is missing.';
exit;
}

 

 

Nikolay.

 

 

SOLVED: By addition of $_SERVER['HTTP_HOST']=$siteDomain; before index.php

Edited by nicolant
Link to comment
Share on other sites

  • 2 months later...

I tested this module sometime ago before the release of DataSet and I had to fix the hardcoded admin url. I will sent you the new pull-request this afternoon concerning the following fix/change, feel free to merge it or not.

 

  • install / uninstall written: fields and template created on installation / removed on uninstall
  • fix: corrected admin page link in module and js file
  • process module link moved under setup (can be moved later by the user without messing the admin url)
  • about, tips markup improvements

 

@mtwebit do you have an idea from where I could start to begin to port this module under Windows as the PCNTL extension isn't supported by this last OS ? 

I have already in mind implementing pthread to consume a Messaging Queueing System or something like that.

 

And for people who wonder how it works with DataSet, there are a small screencast (shortened) importing a CSV DataSet :

 

tasker.thumb.gif.e4a2d8b1b19b83b1f23cb546418c8570.gif

 

Edited by flydev
Question about Windows
  • Like 4
Link to comment
Share on other sites

On 1/6/2019 at 9:59 AM, flydev said:

 

@mtwebit do you have an idea from where I could start to begin to port this module under Windows as the PCNTL extension isn't supported by this last OS ? 

I have already in mind implementing pthread to consume a Messaging Queueing System or something like that.

Thanks for the screencast, @flydev. I really appreciate it.

Regarding the pcntl on Windows... Unfortunately, I don't have experience in this field. Windows has no signals (as far as I know) so I'd use a virtualization tool to overcome this limitation. You can try an API/kernel virtualization (e.g. Linux subsys for Windows) or use Docker.

Also thanks for the pull req. I'll check your suggestions.

Link to comment
Share on other sites

16 minutes ago, mtwebit said:

You can try an API/kernel virtualization (e.g. Linux subsys for Windows) or use Docker.

Ok that's an idea, I will take a look.

 

About the PR, I didn't had the time the other day, will try to re sent it today.

Thanks again for this great module.

Link to comment
Share on other sites

  • 11 months later...

I've uploaded a new version (0.9.5) to GitHub.

It tries to handle DB connection loss errors and it has a basic profiler to optimize your import routines. See the wiki for more details.

Note: TaskerAdmin needs some fixes in its JS-based task executor. Don't use this feature atm. (Cron is always the preferred task execution method.)
 

  • Like 6
Link to comment
Share on other sites

  • 1 year later...
On 12/30/2019 at 3:52 AM, mtwebit said:

Note: TaskerAdmin needs some fixes in its JS-based task executor. Don't use this feature atm. (Cron is always the preferred task execution method.)

I was just playing around with the module and this feature - I am correct in assuming it's still broken? I wasn't sure if I was doing something wrong. I started fixing things, but no real luck yet - it just keeps reporting a failure. And when I tried to emulate the PDF module's approach of redirecting to the tasker admin page, it seems like it never actually executes. Anyway, I'll go try the cron approach and see if that works.

Link to comment
Share on other sites

@mtwebit - I think the bootstrap line in the runByCron.php file should be:

require_once(__DIR__ . '/../../../index.php');

It didn't work for me the way you had it.

Unfortunately I am also having issues with wanting to create and execute a task via my AdminActions module - because the method that is being called belong to the action's class and not the ProcessAdminActions parent class, things aren't working. I think I'll end up building a custom approach to doing what I need but just wanted to let you know of a possible issue that others might also have.

Link to comment
Share on other sites

  • 2 weeks later...
On 1/19/2021 at 12:57 AM, adrian said:

I was just playing around with the module and this feature - I am correct in assuming it's still broken? I wasn't sure if I was doing something wrong. I started fixing things, but no real luck yet - it just keeps reporting a failure. And when I tried to emulate the PDF module's approach of redirecting to the tasker admin page, it seems like it never actually executes. Anyway, I'll go try the cron approach and see if that works.

I forgot to mention it here, but it was fixed in November, last year. I completely rewrote this part of the code. There's no redirect anymore. The module will display an embedded progress bar if you run a task via the Web browser:

Tasker_pbar.thumb.png.756ea356e6485c6272cc79ea8ca8d4d3.png

Link to comment
Share on other sites

On 1/19/2021 at 2:03 AM, adrian said:

@mtwebit - I think the bootstrap line in the runByCron.php file should be:


require_once(__DIR__ . '/../../../index.php');

It didn't work for me the way you had it.

Unfortunately I am also having issues with wanting to create and execute a task via my AdminActions module - because the method that is being called belong to the action's class and not the ProcessAdminActions parent class, things aren't working. I think I'll end up building a custom approach to doing what I need but just wanted to let you know of a possible issue that others might also have.

I guess you forgot to set the working directory in your crontab.

You should invoke Tasker using cron this way:

*/2 * * * * docker exec --user nginx phpweb bash -c "cd /var/www/html/site/modules/Tasker && LD_PRELOAD=/usr/lib/preloadable_libiconv.so /usr/bin/php runByCron.php"

You can skip the docker and LD_PRELOAD parts but you need to set the working directory (cd ......./Tasker).

I don't really understand the other problem, sorry. Do you have trouble with the createTask() method called from your own module?

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...