Resolve "Rider app timeout" #1554
7 changed files with 147 additions and 3 deletions
135
src/Command/UpdateUnacceptedJobOrdersCommand.php
Normal file
135
src/Command/UpdateUnacceptedJobOrdersCommand.php
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use App\Service\MQTTClient;
|
||||
|
||||
use App\Entity\JobOrder;
|
||||
use App\Entity\Rider;
|
||||
|
||||
use PDO;
|
||||
|
||||
class UpdateUnacceptedJobOrdersCommand extends Command
|
||||
{
|
||||
protected $em;
|
||||
protected $mclient;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, MQTTClient $mclient)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->mclient = $mclient;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('joborder:reassignunaccepted')
|
||||
->setDescription('Requeue for rider assignment assigned but unaccepted job orders that have been assigned for more than 3 mins.')
|
||||
->setHelp('Requeue for rider assignment assigned but unaccepted job orders that have been assigned for more than 3 mins.');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$em = $this->em;
|
||||
$mclient = $this->mclient;
|
||||
|
||||
// TODO: get the timeout limit from .env
|
||||
$timeout = 3;
|
||||
$current_status = 'assigned';
|
||||
$new_status = 'rider_assign';
|
||||
|
||||
// pdo connection
|
||||
$db = $em->getConnection();
|
||||
|
||||
// since we need the actual job orders for mqtt events, we need to get the ids of the job orders
|
||||
// that will be updated
|
||||
// need rider id to set rider to available since rider becomes unavailable
|
||||
// the minute JO is assigned to rider
|
||||
$query_sql = 'SELECT id FROM job_order WHERE status = :current_status and TIMESTAMPDIFF(MINUTE, date_assign, NOW()) >= :timeout';
|
||||
|
||||
$query_stmt = $db->prepare($query_sql);
|
||||
$query_stmt->execute([
|
||||
'current_status' => $current_status,
|
||||
'timeout' => $timeout,
|
||||
]);
|
||||
|
||||
// go through rows
|
||||
$requeued_jos = [];
|
||||
while ($row = $query_stmt->fetch(PDO::FETCH_NUM))
|
||||
{
|
||||
// $row[0] is the jo id
|
||||
// store the jos for now for the event sending after update of JOs
|
||||
// and the updating of rider's availability
|
||||
$jo_id = $row[0];
|
||||
|
||||
$requeued_jo = $em->getRepository(JobOrder::class)->find($jo_id);
|
||||
|
||||
$requeued_jos[] = [
|
||||
'jo' => $requeued_jo,
|
||||
];
|
||||
|
||||
$update_sql = 'UPDATE job_order SET status = :new_status, rider_id = null WHERE id = :jo_id';
|
||||
$update_stmt = $db->prepare($update_sql);
|
||||
$update_stmt->execute([
|
||||
'new_status' => $new_status,
|
||||
'jo_id' => $jo_id,
|
||||
]);
|
||||
}
|
||||
|
||||
foreach ($requeued_jos as $jo_info)
|
||||
{
|
||||
$jo = $jo_info['jo'];
|
||||
if ($jo != null)
|
||||
{
|
||||
// $output->writeln('Requeuing for rider assignment ' . $jo->getID());
|
||||
$id = $jo->getID();
|
||||
|
||||
// send notifications to rider app, telling rider that jo has been requeued
|
||||
$rider_payload = [
|
||||
'event' => 'cancelled',
|
||||
'reason' => 'Reassigned',
|
||||
'jo_id' => $id,
|
||||
];
|
||||
$mclient->sendRiderEvent($jo, $rider_payload);
|
||||
|
||||
|
||||
// send outlet assign since order should go back to hub and await reassignment to another rider
|
||||
$payload = [
|
||||
'event' => 'outlet_assign',
|
||||
'jo_id' => $id,
|
||||
];
|
||||
$mclient->sendEvent($jo, $payload);
|
||||
}
|
||||
|
||||
$rider = $jo->getRider();
|
||||
if ($rider != null)
|
||||
{
|
||||
// check rider's current job order before changing rider's availability
|
||||
// since rider's current job order is set when JO is assigned to rider
|
||||
if ($rider->getCurrentJobOrder() != null)
|
||||
{
|
||||
if ($rider->getCurrentJobOrder()->getID() == $jo->getID())
|
||||
{
|
||||
// reset rider's availability to true
|
||||
$rider->setAvailable(true);
|
||||
|
||||
// set rider's current job order to null
|
||||
$rider->setCurrentJobOrder();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -2957,6 +2957,9 @@ class APIController extends Controller implements LoggedController
|
|||
$jo->setStatusAutoAssign(AutoAssignStatus::HUB_AND_RIDER_ASSIGNED);
|
||||
$jo->setDeliveryStatus(DeliveryStatus::RIDER_ASSIGN);
|
||||
|
||||
// set date_assigned for job order
|
||||
$jo->setDateAssign(new DateTime());
|
||||
|
||||
$assigned_rider->setAvailable(false);
|
||||
|
||||
// set rider's current job order
|
||||
|
|
|
|||
|
|
@ -565,6 +565,8 @@ class RiderController extends Controller
|
|||
public function riderActiveJO(EntityManagerInterface $em, MQTTClient $mclient, Rider $rider, $jo_id)
|
||||
{
|
||||
$jo = $em->getRepository(JobOrder::class)->find($jo_id);
|
||||
// TODO: change this to setCurrentJobOrder since this is what is being used to set rider's
|
||||
// job order. It is no longer using setActiveJobOrder.
|
||||
$rider->setActiveJobOrder($jo);
|
||||
$em->flush();
|
||||
|
||||
|
|
|
|||
|
|
@ -21,7 +21,8 @@ use App\Ramcar\WillingToWaitContent;
|
|||
* @ORM\Index(name="plate_number_idx", columns={"plate_number"}),
|
||||
* @ORM\Index(name="first_name_idx", columns={"first_name"}),
|
||||
* @ORM\Index(name="last_name_idx", columns={"last_name"}),
|
||||
* @ORM\Index(name="phone_mobile_idx", columns={"phone_mobile"})
|
||||
* @ORM\Index(name="phone_mobile_idx", columns={"phone_mobile"}),
|
||||
* @ORM\Index(name="status_idx", columns={"status"}),
|
||||
* })
|
||||
*/
|
||||
class JobOrder
|
||||
|
|
|
|||
|
|
@ -35,8 +35,8 @@ class MQTTClient
|
|||
|
||||
public function sendEvent(JobOrder $job_order, $payload)
|
||||
{
|
||||
error_log('sending mqtt event: ');
|
||||
error_log(print_r($payload, true));
|
||||
//error_log('sending mqtt event: ');
|
||||
//error_log(print_r($payload, true));
|
||||
|
||||
$sessions = $job_order->getCustomer()->getMobileSessions();
|
||||
if (count($sessions) == 0)
|
||||
|
|
|
|||
|
|
@ -50,6 +50,8 @@ class ResqRiderAssignmentHandler implements RiderAssignmentHandlerInterface
|
|||
|
||||
// send push notification
|
||||
$this->aclient->sendPush($obj, "A RESQ rider is on his way to you.");
|
||||
|
||||
$this->em->flush();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
1
utils/update_jo_date_assign/update_jo_date_assign.sql
Normal file
1
utils/update_jo_date_assign/update_jo_date_assign.sql
Normal file
|
|
@ -0,0 +1 @@
|
|||
UPDATE job_order SET date_assign = date_create WHERE date_assign is null;
|
||||
Loading…
Reference in a new issue