1. Go to this page and download the library: Download guycalledseven/job-queue library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
guycalledseven / job-queue example snippets
use guycalledseven\JobQueue\{SqliteQueue, CsvQueue, CsvImporter, CsvExporter, CsvProfile};
// SQLite engine (recommended)
$engine = SqliteQueue::open(__DIR__.'/data/queue.sqlite');
// CSV engine (small files only)
$engine = CsvQueue::load(__DIR__.'/data/queue.csv', ';');
// Import/Export adapters bind to the chosen engine
$importer = new CsvImporter($engine);
$exporter = new CsvExporter($engine);
use guycalledseven\JobQueue\SqliteQueue;
use guycalledseven\JobQueue\CsvProfile;
use guycalledseven\JobQueue\CsvImporter;
use guycalledseven\JobQueue\CsvExporter;
$csv = './tests/simple.csv';
$db = './tests/' . basename(($csv)) . '-queue.sqlite';
$q = SqliteQueue::open($db);
$importer = new CsvImporter($q);
$exporter = new CsvExporter($q);
// clean up DB file size and stats
$q->dropAndRecreate(true);
// import minimal set from csv
$profileIn = new CsvProfile();
$profileIn->aliases = [
'id' => ['id'] // only column we expect
];
$importer->import($csv, $profileIn, ';');
$total = $q->totalItems();
// Loop until empty
while ($job = $q->fetchNext()) {
$id = $job['id'];
$this->logger->info("Processing $id");
try {
// your long work using $id
$ok = true;
// create additional field data
$q->updateById($id, [
'segment' => 1,
'note' => 'bla',
]);
// $ok = false; // or true based on your logic
$ok ? $q->markSuccess($id)
: $q->markFailure($id, 'Processor returned false');
} catch (Throwable $e) {
$q->markFailure($id, $e->getMessage());
}
}
$p = $q->progress();
$this->logger->info("Done {$p['done']} / {$p['total']}");
// export minimal data set to csv
$profileOut = new CsvProfile();
$profileOut->export_columns = [
'id',
'status',
'result',
'segment',
'note' // pulled from extras
];
$profileOut->labels = [ // optional pretty headers
'status' => 'Status',
'segment' => 'Segment data'
];
$exporter->export($csv, $profileOut, ';');