- Saved searches
- Use saved searches to filter your results more quickly
- License
- caseyamcl/tasktracker
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- How to Create Task Manager App with PHP
- What is Task Manager App?
- What is in a Task Manager App?
- Technologies Used in this project
- Step by Step Tutorials
- 1. Design Homepage and Manage Lists Page
- 2. Insert and Display Lists
- 3. Update and Delete List
- 4. Insert and Display Tasks
- 5. Update and Delete Task
- 6. Add Lists on Menu and Display Tasks by List
- 7. Adding CSS in our project to make it more Beautiful
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
A library for tracking long-running tasks in PHP (when a simple progress bar isn’t enough)
License
caseyamcl/tasktracker
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
A library for tracking long-running tasks in PHP (when a simple progress bar isn’t enough)
At a Glance:
- Reports on memory usage and a number of progress statistics during long-running tasks
- Useful for long-running processes where a large number of small jobs are executed
- Event-driven architecture using the Symfony Event-Dispatcher Component
- Can report on task progress to any EventSubscriberInterface
- Symfony Console Progress Bar
- Symfony Console Running Log of Task Messages
- Sending Task Progress to PSR-3 Compatible Loggers
For example, you may want to display a progress bar on the console during execution of a task, but also send periodic snapshots of the state of the system to Monolog while a task is executing. Using a single Tracker object, you can accomplish both of these goals:
use TaskTracker\Tracker, TaskTracker\Tick; use TaskTracker\Subscriber\SymfonyConsoleProgress, TaskTracker\Subscriber\Psr3Logger; use Symfony\Console\Output\ConsoleOutput; use Monolog\Logger as MonologLogger; // Setup subscribers $subscribers = [ new SymfonyConsoleProgress(new ConsoleOutput()), new Psr3Logger(new MonologLogger()) ]; // Setup a tracker for a job with 100 items $tracker = Tracker::build(100, $subscribers); $tracker->start("Let's go"); for ($i = 0; $i < 100; $i++) < // Do some work of some sort. $tracker->tick(); > $tracker->finish("All done");
composer require caseyamcl/tasktracker:~2.0
- Download the source from http://github.com/caseyamcl/tasktracker.
- Include the src/TaskTracker folder in your code using a PSR-4 compatible autoloader.
To track a task, create an instance of the Tracker class:
use TaskTracker\Tracker; // Instantiate a tracker to track 100 items $tracker = new Tracker(100);
You can omit the number of items if you are working with an unknown number:
The Tracker class creates its own EventDispatcher , but you can optionally inject your own if you need to:
$dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); $tracker = new Tracker(100, $dispatcher); // ..or.. $tracker = new Tracker(Tracker::UNKNOWN, $dispatcher);
To start tracking, simply call the Tracker::start() method:
// Start the tracker $tracker->start('optional starting message');
For every element you process, call the Tracker::tick() method until you are done:
There are three types of Ticks: Success (default), Fail, and Skip:
use Tracker\Tick; $tracker->tick(Tick::SUCCESS); $tracker->tick(Tick::FAIL); $tracker->tick(Tick::SKIP);
You can also supply an optional message:
$tracker->tick(Tick::SUCCESS, 'Things are going well.'); $tracker->tick(Tick::FAIL, 'Crud. Something went wrong'); $tracker->tick(Tick::SKIP, 'Skipping this record for whatever reason');
You can add custom data to the Tick in the form of an array:
$tracker->tick(Tick::SUCCESS, 'Things are going well.', ['foo' => 'bar', 'baz' => 'biz]);
And, you can increment by more than one item at a time:
// Increment by 5 items $tracker->tick(Tick::SUCCESS, '', [], 5); // Three items failed $tracker->tick(Tick::FAIL, 'Something went wrong', [], 3);
When you are done, call the Tracker::finish() method:
$tracker->finish('Optional finish manage');
Or, if things go wrong during processing, you can abort:
$tracker->abort('Optional abort message');
The class contains a few helper methods, too:
// Have we started processing yet? $tracker->isRunning(); // Get the last tick (instance of \Tracker\Tick class) $tracker->getLastTick(); // Get the status of the process as an int (Tracker::NOT_STARTED, Tracker::RUNNING, Tracker::FINISHED, or Tracker::ABORTED) $tracker->getStatus(); // Get the number of items processed thus far $tracker->getNumProcessedItems(); // Get only the number of failed items (works with SUCCESS and SKIP too) $tracker->getNumProcessedItems(Tick::FAIL); // Get the time started (in microseconds) $tracker->getStartTime();
You can use the Tracker:run($iterator, $callback) method for cleaner syntax.
The $iterator value must be an instance of \Traversable ; arrays are not accepted, but ArrayIterator objects will work:$iterator = new \ArrayIterator(['a', 'b', 'c']); // This code is the equivalent of. $tracker->run($iterator, function(Tracker $tracker, $item) < // work on a single item $tracker->tick(); >); //. this code: $tracker->start(); foreach ($iterator as $item) < // work on a single item $tracker->tick(); > $tracker->finish();
The Tracker class isn’t very useful on its own without event subscribers to listen for tracker tick events. There are a few subscribers bundled with this library:
- TaskTracker\Subscriber\Psr3Logger — Logs Tracker events to any PSR-3 Logger
- TaskTracker\Subscriber\SymfonyConsoleLog — Logs Tracker events to a Symfony console, each event on its own line.
- TaskTracker\Subscriber\SymfonyConsoleProgress — Logs tracker events to a Symfony console progress bar indicator.
You can add event subscribers to the Tracker by calling the Tracker::addSubscriber() method:
$tacker = new Tracker(100); $tracker->addSubscriber(new SymfonyConsoleLog($output));
If you know what subscribers you will use ahead of time, you can use the Tracker::build() method for convenience:
$subscribers = [new SymfonyConsoleLog($output), new SomeOtherSubscriber()]; $tracker = Tracker::build(100, $subscribers);
As an example, suppose you are creating a Symfony Console Command, and you want to show a progress bar for some task and also log events as they occur:
use TaskTracker\Tracker; use TaskTracker\Tick; use TaskTracker\Subscriber\SymfonyConsoleProgress; use Symfony\Component\Console\Command\Command; /** * My Symfony Command */ class MyCommand extends Command < protected function configure() < $this->setName('example'); $this->setDescription("Demonstrate TaskTracker"); > protected function execute(InputInterface $input, OutputInterface $output) < $numItems = 10; // Build Task Tracker with Symfony Console Progress Bar subscriber $tracker = Tracker::build([new SymfonyConsoleProgress($output)], $numItems); // Add a Monolog Listener after Tracker construction $monolog = new \Monolog\Logger(/* some handlers */); $tracker->addSubscriber(new Psr3Logger($monolog)); // You can also add Event Listeners directly $tracker->getDispatcher()->addListener(\Tracker\Events::TRACKER_TICK, function(\Tracker\Tick $tick) < // do something. >); // Tracker::start() is technically optional; if not called, it will automatically // be called upon the first Tick $tracker->start("Let's go!"); // The SymfonyConsoleProgress listener will output a progress bar while the logger will log events for ($i = 0; $i < 10; $i++) < $tracker->tick(\Tick::SUCCESS, "On item: " . $i); sleep(1); > // Tracker::start(), Tracker::tick(), Tracker::abort(), and Tracker::finish() all return // a \Tracker\Report object. $report = $tracker->finish('All done!'); $output->writeln(sprintf("All Done!
%s items processed", $report->getNumTotalItems())); > >TaskTracker uses the Symfony EventDispatcher library, so any Symfony-compatible event listener can be used.
There are four events you can listen for:
- TaskTracker\Events::TRACKER_START
- TaskTracker\Events::TRACKER_TICK
- TaskTracker\Events::TRACKER_FINISH
- TaskTracker\Events::TRACKER_ABORT
All four events dispatch an instance of the TaskTracker\Tick class. Your subscribers/listeners should accept an object of that class as its parameter:
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use TaskTracker\Tick; /** * Listen for Tracker Events */ class MyEventSubscriber implements EventSubscriberInterface < public static function getSubscribedEvents() < return [ TaskTracker\Events::TRACKER_START => 'handle', TaskTracker\Events::TRACKER_TICK => 'handle', TaskTracker\Events::TRACKER_FINISH => 'handle', ]; > public static function handle(Tick $tickEvent) < // See all of the information about the progress of that tick var_dump($tickEvent->getReport()->toArray()); > >
Every Tracker event emits a \Tracker\Report object with a snapshot of the process and some system information present at the point in time that the event occurred:
$report = $tracker->tick(); $report->getTimeStarted(); $report->getTotalItemCount(); $report->getTick(); $report->getNumItemsProcessed(); $report->getTimeElapsed(); $report->getNumItemsSuccess(); $report->getNumItemsFail(); $report->getNumItemsSkip(); $report->getItemTime(); $report->getMaxItemTime(); $report->getMinItemTime(); $report->getAvgItemTime(); $report->getMemUsage(); $report->getMemPeakUsage(); $report->getMessage(); $report->getTimestamp(); $report->getStatus(); $report->getIncrementBy(); $report->getReport(); $report->getExtraInfo();
In your subscribers, you can access the report from the Tick object by calling Tick::getReport() .
About
A library for tracking long-running tasks in PHP (when a simple progress bar isn’t enough)
How to Create Task Manager App with PHP
Trying to find a course or a tutor to learn any programming language or a technical course is daunting task.
Though there are lots of resources available for FREE, it’s really difficult to self-motivate and start the learning process. And it’s based on my own experience. For me, learning any programming language was easier by doing a project.
It doesn’t matter how good you’re in theory, if you can’t make a real world project out of your knowledge. But if you start doing real projects with the knowledge you currently have, you’ll develop the necessary skills and knowledge as your projects start to be big and complex. So, with the intention of Making the Learning Fun, Here’s a PHP Course for Beginners where you’ll create a TASK MANAGER App.What is Task Manager App?
Whether in a daily life or in a workplace, you’ve lots of tasks throughout the day. It’s easier to miss a task on a busy day. And sometimes it becomes horrible when we miss the most important task of the day. So, TASK MANAGER is a simple web app where You can Manage Tasks, group them by Categories, set the Priority and Deadline. And in this course, you will create your own Task Manager App Step-by-Step with PHP programming language and MySQL Database.
What is in a Task Manager App?
- Manage Lists (Create/Update/Delete)
- Manage Tasks (Create/Update/Delete)
- Display Lists on Menu
- View Tasks and Sort by Lists
Technologies Used in this project
Step by Step Tutorials
1. Design Homepage and Manage Lists Page
2. Insert and Display Lists
3. Update and Delete List
4. Insert and Display Tasks
5. Update and Delete Task
6. Add Lists on Menu and Display Tasks by List
7. Adding CSS in our project to make it more Beautiful
Thank You So Much for following the article till the end.
If you’re more interested to create more PHP projects, Follow my next article to create «Food Order Website with PHP and MySQL»