HOR-935 Retirar las notificaciones Mobile del proceso de derivación y ejecutarlas asincronamente

up observations by QA
This commit is contained in:
Ronald Q
2016-06-02 11:27:55 -04:00
parent 012fa791f2
commit f20b5d0788
13 changed files with 1840 additions and 36 deletions

View File

@@ -8,7 +8,8 @@ try {
'cron' => ['title' => 'CRON'],
'ldapcron' => ['title' => 'LDAP Advanced CRON'],
'messageeventcron' => ['title' => 'Message-Event CRON'],
'timereventcron' => ['title' => 'Timer-Event CRON']
'timereventcron' => ['title' => 'Timer-Event CRON'],
'sendnotificationscron' => ['title' => 'Send-Notifications CRON']
];
//Define constants

View File

@@ -304,6 +304,9 @@ try {
$timerEvent->startContinueCaseByTimerEvent(date('Y-m-d H:i:s'), true);
break;
case 'sendnotificationscron':
sendNotifications();
break;
}
} catch (Exception $e) {
echo $e->getMessage() . "\n";
@@ -1036,3 +1039,50 @@ function synchronizeGmailLabels()
}
}
/*----------------------------------********---------------------------------*/
function sendNotifications()
{
try {
global $argvx;
if ($argvx != "" && strpos($argvx, "send-notifications") === false) {
return false;
}
setExecutionMessage("Resending Notifications");
setExecutionResultMessage("PROCESSING");
$notQueue = new \NotificationQueue();
$notificationsAndroid = $notQueue->loadStatusDeviceType('pending', 'android');
if ($notificationsAndroid) {
setExecutionMessage("|-- Send Android's Notifications");
$n = 0;
foreach ($notificationsAndroid as $key => $item) {
$oNotification = new \ProcessMaker\BusinessModel\Light\PushMessageAndroid();
$oNotification->setSettingNotification();
$oNotification->setDevices(unserialize($item['DEV_UID']));
$response['android'] = $oNotification->send($item['NOT_MSG'], unserialize($item['NOT_DATA']));
$notQueue = new \NotificationQueue();
$notQueue->changeStatusSent($item['NOT_UID']);
$n += $oNotification->getNumberDevices();
}
setExecutionResultMessage("Processed $n");
}
$notificationsApple = $notQueue->loadStatusDeviceType('pending', 'apple');
if($notificationsApple) {
setExecutionMessage("|-- Send Apple Notifications");
$n = 0;
foreach ($notificationsApple as $key => $item) {
$oNotification = new \ProcessMaker\BusinessModel\Light\PushMessageIOS();
$oNotification->setSettingNotification();
$oNotification->setDevices(unserialize($item['DEV_UID']));
$response['apple'] = $oNotification->send($item['NOT_MSG'], unserialize($item['NOT_DATA']));
$notQueue = new \NotificationQueue();
$notQueue->changeStatusSent($item['NOT_UID']);
$n += $oNotification->getNumberDevices();
}
setExecutionResultMessage("Processed $n");
}
} catch (Exception $e) {
setExecutionResultMessage("WITH ERRORS", "error");
eprintln(" '-" . $e->getMessage(), "red");
saveLog("ExecuteSendNotifications", "error", "Error when sending notifications " . $e->getMessage());
}
}

View File

@@ -0,0 +1,2 @@
<?php
require_once("cron.php");

View File

@@ -0,0 +1,107 @@
<?php
require_once 'classes/model/om/BaseNotificationQueue.php';
/**
* Skeleton subclass for representing a row from the 'NOTIFICATION_QUEUE' table.
*
*
*
* You should add additional methods to this class to meet the
* application requirements. This class will only be generated as
* long as it does not already exist in the output directory.
*
* @package classes.model
*/
class NotificationQueue extends BaseNotificationQueue
{
public function create(array $arrayData)
{
$cnn = Propel::getConnection(NotificationDevicePeer::DATABASE_NAME);
try {
$this->setNotUid(G::generateUniqueID());
$this->setDevType($arrayData['DEV_TYPE']);
$this->setDevUid($arrayData['DEV_UID']);
$this->setNotMsg($arrayData['NOT_MSG']);
$this->setNotData($arrayData['NOT_DATA']);
$this->setNotStatus($arrayData['NOT_STATUS']);
$this->setNotSendDate('now');
if ($this->validate()) {
$cnn->begin();
$result = $this->save();
$cnn->commit();
} else {
throw new Exception(G::LoadTranslation("ID_RECORD_CANNOT_BE_CREATED"));
}
} catch (Exception $e) {
$cnn->rollback();
throw $e;
}
return $result;
}
public function loadStatus($status)
{
try {
$criteria = new Criteria();
$criteria->clearSelectColumns();
$criteria->add(NotificationQueuePeer::NOT_STATUS, $status, Criteria::EQUAL);
$rs = NotificationQueuePeer::doSelectRS($criteria);
$rs->setFetchmode(ResultSet::FETCHMODE_ASSOC);
$notifications = array();
while ($rs->next()) {
$row = $rs->getRow();
$notifications[] = $row;
}
} catch (Exception $error) {
throw $error;
}
return $notifications;
}
public function loadStatusDeviceType($status, $devType)
{
try {
$criteria = new Criteria();
$criteria->clearSelectColumns();
$criteria->add(NotificationQueuePeer::NOT_STATUS, $status, Criteria::EQUAL);
$criteria->add(NotificationQueuePeer::DEV_TYPE, $devType, Criteria::EQUAL);
$rs = NotificationQueuePeer::doSelectRS($criteria);
$rs->setFetchmode(ResultSet::FETCHMODE_ASSOC);
$notifications = array();
while ($rs->next()) {
$row = $rs->getRow();
$notifications[] = $row;
}
} catch (Exception $error) {
throw $error;
}
return $notifications;
}
public function changeStatusSent($not_uid)
{
$cnn = Propel::getConnection(NotificationDevicePeer::DATABASE_NAME);
$rs = NotificationQueuePeer::retrieveByPK($not_uid);
try {
$arrayData['NOT_STATUS'] = "sent";
$arrayData['NOT_SEND_DATE'] = date('Y-m-d H:i:s');
$rs->fromArray($arrayData, BasePeer::TYPE_FIELDNAME);
if ($this->validate()) {
$cnn->begin();
$result = $rs->save();
$cnn->commit();
} else {
throw new Exception(G::LoadTranslation("ID_RECORD_CANNOT_BE_CREATED"));
}
} catch (Exception $e) {
$cnn->rollback();
throw $e;
}
return $result;
}
} // NotificationQueue

View File

@@ -0,0 +1,23 @@
<?php
// include base peer class
require_once 'classes/model/om/BaseNotificationQueuePeer.php';
// include object class
include_once 'classes/model/NotificationQueue.php';
/**
* Skeleton subclass for performing query and update operations on the 'NOTIFICATION_QUEUE' table.
*
*
*
* You should add additional methods to this class to meet the
* application requirements. This class will only be generated as
* long as it does not already exist in the output directory.
*
* @package classes.model
*/
class NotificationQueuePeer extends BaseNotificationQueuePeer {
} // NotificationQueuePeer

View File

@@ -0,0 +1,84 @@
<?php
require_once 'propel/map/MapBuilder.php';
include_once 'creole/CreoleTypes.php';
/**
* This class adds structure of 'NOTIFICATION_QUEUE' table to 'workflow' DatabaseMap object.
*
*
*
* These statically-built map classes are used by Propel to do runtime db structure discovery.
* For example, the createSelectSql() method checks the type of a given column used in an
* ORDER BY clause to know whether it needs to apply SQL to make the ORDER BY case-insensitive
* (i.e. if it's a text column type).
*
* @package workflow.classes.model.map
*/
class NotificationQueueMapBuilder
{
/**
* The (dot-path) name of this class
*/
const CLASS_NAME = 'classes.model.map.NotificationQueueMapBuilder';
/**
* The database map.
*/
private $dbMap;
/**
* Tells us if this DatabaseMapBuilder is built so that we
* don't have to re-build it every time.
*
* @return boolean true if this DatabaseMapBuilder is built, false otherwise.
*/
public function isBuilt()
{
return ($this->dbMap !== null);
}
/**
* Gets the databasemap this map builder built.
*
* @return the databasemap
*/
public function getDatabaseMap()
{
return $this->dbMap;
}
/**
* The doBuild() method builds the DatabaseMap
*
* @return void
* @throws PropelException
*/
public function doBuild()
{
$this->dbMap = Propel::getDatabaseMap('workflow');
$tMap = $this->dbMap->addTable('NOTIFICATION_QUEUE');
$tMap->setPhpName('NotificationQueue');
$tMap->setUseIdGenerator(false);
$tMap->addPrimaryKey('NOT_UID', 'NotUid', 'string', CreoleTypes::VARCHAR, true, 32);
$tMap->addColumn('DEV_TYPE', 'DevType', 'string', CreoleTypes::VARCHAR, true, 50);
$tMap->addColumn('DEV_UID', 'DevUid', 'string', CreoleTypes::LONGVARCHAR, true, null);
$tMap->addColumn('NOT_MSG', 'NotMsg', 'string', CreoleTypes::LONGVARCHAR, true, null);
$tMap->addColumn('NOT_DATA', 'NotData', 'string', CreoleTypes::LONGVARCHAR, true, null);
$tMap->addColumn('NOT_STATUS', 'NotStatus', 'string', CreoleTypes::VARCHAR, true, 150);
$tMap->addColumn('NOT_SEND_DATE', 'NotSendDate', 'int', CreoleTypes::TIMESTAMP, true, null);
} // doBuild()
} // NotificationQueueMapBuilder

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -5324,4 +5324,17 @@
<index-column name="STATUS"/>
</index>
</table>
<table name="NOTIFICATION_QUEUE" idMethod="native">
<column name="NOT_UID" type="VARCHAR" size="32" required="true" primaryKey="true"/>
<column name="DEV_TYPE" type="VARCHAR" size="50" required="true" />
<column name="DEV_UID" type="LONGVARCHAR" required="true" />
<column name="NOT_MSG" type="LONGVARCHAR" required="true" />
<column name="NOT_DATA" type="LONGVARCHAR" required="true" />
<column name="NOT_STATUS" type="VARCHAR" size="150" required="true"/>
<column name="NOT_SEND_DATE" type="TIMESTAMP" required="true" />
<index name="indexNotStatus">
<index-column name="NOT_STATUS"/>
</index>
</table>
</database>

View File

@@ -2971,4 +2971,19 @@ CREATE TABLE `GMAIL_RELABELING` (
# This restores the fkey checks, after having unset them earlier
DROP TABLE IF EXISTS `NOTIFICATION_QUEUE`;
CREATE TABLE `NOTIFICATION_QUEUE`
(
`NOT_UID` VARCHAR(32) NOT NULL,
`DEV_TYPE` VARCHAR(50) NOT NULL,
`DEV_UID` MEDIUMTEXT NOT NULL,
`NOT_MSG` MEDIUMTEXT NOT NULL,
`NOT_DATA` MEDIUMTEXT NOT NULL,
`NOT_STATUS` VARCHAR(150) NOT NULL,
`NOT_SEND_DATE` DATETIME NOT NULL,
PRIMARY KEY (`NOT_UID`),
KEY `indexNotStatus`(`NOT_STATUS`)
)ENGINE=InnoDB ;
# This restores the fkey checks, after having unset them earlier
SET FOREIGN_KEY_CHECKS = 1;

View File

@@ -228,16 +228,26 @@ class NotificationDevice
$isExistNextNotifications = $oNoti->isExistNextNotification($appFields['APP_UID'],
$currentDelIndex);
if (count($devicesAppleIds) > 0 && $isExistNextNotifications) {
$oNotification = new PushMessageIOS();
$oNotification->setSettingNotification();
$oNotification->setDevices($devicesAppleIds);
$response['apple'] = $oNotification->send($message, $data);
$arrayData = array();
$arrayData['NOT_FROM'] = $currentUserId;
$arrayData['DEV_TYPE'] = 'apple';
$arrayData['DEV_UID'] = serialize($devicesAppleIds);
$arrayData['NOT_MSG'] = $message;
$arrayData['NOT_DATA'] = serialize($data);
$arrayData['NOT_STATUS'] = "pending";
$notQueue = new \NotificationQueue();
$notQueue->create($arrayData);
}
if (count($devicesAndroidIds) > 0 && $isExistNextNotifications) {
$oNotification = new PushMessageAndroid();
$oNotification->setSettingNotification();
$oNotification->setDevices($devicesAndroidIds);
$response['android'] = $oNotification->send($message, $data);
$arrayData = array();
$arrayData['NOT_FROM'] = $currentUserId;
$arrayData['DEV_TYPE'] = 'android';
$arrayData['DEV_UID'] = serialize($devicesAndroidIds);
$arrayData['NOT_MSG'] = $message;
$arrayData['NOT_DATA'] = serialize($data);
$arrayData['NOT_STATUS'] = "pending";
$notQueue = new \NotificationQueue();
$notQueue->create($arrayData);
}
}
}

View File

@@ -15,31 +15,34 @@ namespace ProcessMaker\BusinessModel\Light;
class PushMessageAndroid
{
var $url = 'https://android.googleapis.com/gcm/send';
var $serverApiKey = "AIzaSyBO-VLXGhjf0PPlwmPFTPQEKIBfVDydLAk";
var $devices = array();
private $url = 'https://android.googleapis.com/gcm/send';
private $serverApiKey = "AIzaSyBO-VLXGhjf0PPlwmPFTPQEKIBfVDydLAk";
private $devices = array();
private $numberDevices = 0;
/**
* @param $url string the url server
*/
function setUrl($url){
public function setUrl($url)
{
$this->$url = $url;
}
/**
* @param $apiKeyIn string the server API key
*/
function setKey($apiKeyIn){
public function setKey($apiKeyIn)
{
$this->serverApiKey = $apiKeyIn;
}
/**
* Set the devices to send to
* @param $deviceIds string or array of device tokens to send
* @param $deviceIds string or array of device tokens to send
*/
function setDevices($deviceIds)
public function setDevices($deviceIds)
{
if(is_array($deviceIds)){
if (is_array($deviceIds)) {
$this->devices = $deviceIds;
} else {
$this->devices = array($deviceIds);
@@ -51,7 +54,7 @@ class PushMessageAndroid
*/
public function setSettingNotification()
{
$conf = \System::getSystemConfiguration( PATH_CONFIG . 'mobile.ini' );
$conf = \System::getSystemConfiguration(PATH_CONFIG . 'mobile.ini');
$this->setUrl($conf['android']['url']);
$this->setKey($conf['android']['serverApiKey']);
}
@@ -62,27 +65,28 @@ class PushMessageAndroid
* @param $data array send extra information for app
* @return mixed
*/
function send($message, $data)
public function send($message, $data)
{
if(!is_array($this->devices) || count($this->devices) == 0){
$this->numberDevices = count($this->devices);
if (!is_array($this->devices) || $this->numberDevices == 0) {
$this->error("No devices set");
}
if(strlen($this->serverApiKey) < 8){
if (strlen($this->serverApiKey) < 8) {
$this->error("Server API Key not set");
}
if (!is_null($data)) {
$fields = array(
'registration_ids' => $this->devices,
'data' => array(
'data' => array(
"message" => $message,
"data" => $data
),
);
} else {
$fields = array(
'registration_ids' => $this->devices,
'data' => array( "message" => $message ),
'registration_ids' => $this->devices,
'data' => array("message" => $message),
);
}
@@ -94,17 +98,17 @@ class PushMessageAndroid
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $this->url );
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Avoids problem with https certificate
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Execute post
$result = curl_exec($ch);
@@ -114,7 +118,12 @@ class PushMessageAndroid
return $result;
}
function error($msg)
public function getNumberDevices()
{
return $this->numberDevices;
}
public function error($msg)
{
echo "Android send notification failed with error:";
echo "\t" . $msg;

View File

@@ -19,6 +19,7 @@ class PushMessageIOS
private $pemFile = 'mobileios.pem';
private $devices = array();
private $response = array();
private $numberDevices = 0;
/**
* Sete server notification Ios
@@ -44,13 +45,13 @@ class PushMessageIOS
*/
public function setPemFile($file)
{
$file = file_exists(PATH_CONFIG . $file)?$file:'mobileios.pem';
$file = file_exists(PATH_CONFIG . $file) ? $file : 'mobileios.pem';
$this->pemFile = $file;
}
/**
* Set the devices token to send to
* @param array $devicesToken of device tokens to send to
* @param array $devicesToken of device tokens to send to
*/
public function setDevices($devicesToken)
{
@@ -81,7 +82,8 @@ class PushMessageIOS
*/
public function send($message, $data)
{
if (!is_array($this->devices) || count($this->devices) == 0) {
$this->numberDevices = count($this->devices);
if (!is_array($this->devices) || $this->numberDevices == 0) {
$this->error("No devices set");
}
if (strlen($this->passphrase) < 8) {
@@ -154,9 +156,14 @@ class PushMessageIOS
return $this->response;
}
public function getNumberDevices()
{
return $this->numberDevices;
}
public function error($msg)
{
echo "Android send notification failed with error:";
echo "\t" . $msg;
}
}
}