PHP code example of gyselroth / mongodb-php-task-scheduler
1. Go to this page and download the library: Download gyselroth/mongodb-php-task-scheduler 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/ */
gyselroth / mongodb-php-task-scheduler example snippets
class MailJob extends TaskScheduler\AbstractJob
{
/**
* {@inheritdoc}
*/
public function start(): bool
{
$transport = new Zend\Mail\Transport\Sendmail();
$mail = Message::fromString($this->data);
$this->transport->send($mail);
return true;
}
}
$mongodb = new MongoDB\Client('mongodb://localhost:27017');
$logger = new \A\Psr4\Compatible\Logger();
$scheduler = new TaskScheduler\Scheduler($mongodb->mydb, $logger);
$mail = new Message();
$mail->setSubject('Hello...');
$mail->setBody('World');
$mail->setFrom('root@localhost', 'root');
$scheduler->addJob(MailJob::class, $mail->toString());
class WorkerFactory extends TaskScheduler\WorkerFactoryInterface
{
/**
* {@inheritdoc}
*/
public function buildWorker(MongoDB\BSON\ObjectId $id): TaskScheduler\Worker
{
$mongodb = new MongoDB\Client('mongodb://localhost:27017');
$logger = new \A\Psr4\Compatible\Logger();
$scheduler = new TaskScheduler\Scheduler($mongodb->mydb, $logger);
return new TaskScheduler\Worker($id, $scheduler, $mongodb->mydb, $logger);
}
/**
* {@inheritdoc}
*/
public function buildManager(): TaskScheduler\WorkerManager
{
$logger = new \A\Psr4\Compatible\Logger();
return new TaskScheduler\WorkerManager($this, $logger);
}
}
$mongodb = new MongoDB\Client('mongodb://localhost:27017');
$logger = new \A\Psr4\Compatible\Logger();
$scheduler = new TaskScheduler\Scheduler($mongodb->mydb, $logger);
$worker_factory = My\App\WorkerFactory(); #An instance of our previously created worker factory
$queue = new TaskScheduler\Queue($scheduler, $mongodb, $worker_factory, $logger);
$queue->process();
$scheduler = new TaskScheduler\Scheduler($mongodb->mydb, $logger);
$scheduler->getJobs();
$scheduler = new TaskScheduler\Scheduler($mongodb->mydb, $logger);
$scheduler->cancelJob(MongoDB\BSON\ObjectId $job_id);
$scheduler = new TaskScheduler\Scheduler($mongodb->mydb, $logger);
$scheduler->flush();
class MailJob extends TaskScheduler\AbstractJob
{
/**
* {@inheritdoc}
*/
public function start(): bool
{
$transport = new Zend\Mail\Transport\Sendmail();
$mail = Message::fromString($this->data);
$this->transport->send($mail);
throw new \Exception('i am an exception');
return true;
}
}