404 Error 404 Error - 3 months ago 5
PHP Question

CakePHP Shell Error Plugin could not be found

I wrote a Shell file named SyncVeevaShell.php (which was basically a copy/paste of some Controller functions) in /app/Console/Command which is supposed to be run via a cronjob every 10 minutes but its not working. When I try to manually run the shell via the command "sh cake SyncVeevaShell.php" I get an error saying plugin SyncVeevaShell could not be found. I don't understand why it would mention a plugin since I'm not trying to use one.

SyncVeevaShell.php:

<?php

App::uses('AppShell', 'Console/Command');

class SyncVeevaShell extends AppShell {
public $uses = array('Asset', 'VeevaVault');

public function main()
{
$this->autoRender = false;
App::import('Model', 'Segment');
$Segment = new Segment();
App::import('Vendor', 'phpVeeva', array('file' => 'veeva' . DS . 'veeva.php'));
$veeva = new phpVeeva();
$veevaList = $veeva->getDocList();

$this->remove_veeva_asset($veevaList);

$i = 0;
while ($i < count($veevaList))
{
$obj = $veevaList[$i];
$filename = $obj->{$veeva::LIST_VALUES}[8];
$major_ver = $obj->{$veeva::LIST_VALUES}[6];
$minor_ver = $obj->{$veeva::LIST_VALUES}[7];

$result = $this->Asset->VeevaVault->find('count', array(
'conditions' => array(
'VeevaVault.title' => $filename,
)
));
if ($result == 0) {

$id = $obj->{$veeva::LIST_VALUES}[0]; //get document ID
$info = $veeva->getInfo($id);
$ext = $veeva->extensions[$info['format__v']];
$size = $info['size__v'];
$mime = $info['format__v'];
$image = 0;
if (strpos($mime, 'image') !== false) {
$image = 1;
}
$product = $info['product__v'][0];
$brand = $Segment->getVeevaBrand($product);
$message_description = $info['approved_content_description__c'];
$message_title = $info['title__v'];
$message_expire = $info['expiration_date__vs'] . ' 12:00:00';
$message_status = $info['status__v'];
$url = 'https://frx.veevavault.com/api/v7.0/objects/documents/' . $id . '/file';

$this->create_veeva_asset($id, $filename, $ext, $size, $mime, $image, $brand, $message_description, $message_title, $message_expire, $message_status, $url, $major_ver, $minor_ver);
}
else {

$result = $this->Asset->VeevaVault->find('count', array(
'conditions' => array(
'VeevaVault.title' => $filename,
'VeevaVault.major_ver' => $major_ver,
'VeevaVault.minor_ver' => $minor_ver
)
));
if ($result == 0) {

$id = $obj->{$veeva::LIST_VALUES}[0];
$info = $veeva->getInfo($id);
$ext = $veeva->extensions[$info['format__v']];
$size = $info['size__v'];
$mime = $info['format__v'];
$image = 0;
if (strpos($mime, 'image') !== false) {
$image = 1;
}
$product = $info['product__v'][0];
$brand = $Segment->getVeevaBrand($product);
$message_description = $info['approved_content_description__c'];
$message_title = $info['title__v'];
$message_expire = $info['expiration_date__vs'] . ' 12:00:00';
$message_status = $info['status__v'];
$url = 'https://frx.veevavault.com/api/v7.0/objects/documents/' . $id . '/file';

$this->update_veeva_asset($id, $filename, $ext, $size, $mime, $image, $brand, $message_description, $message_title, $message_expire, $message_status, $url, $major_ver, $minor_ver);
}
}
$i++;
}
}

public function remove_veeva_asset($remoteVeevaList)
{
$this->autoRender = false;
App::import('Vendor', 'phpVeeva', array('file' => 'veeva' . DS . 'veeva.php'));
$veeva = new phpVeeva();

if (!empty($remoteVeevaList)) {

$db = ConnectionManager::getDataSource('default');
$i = 0;
while ($i < count($remoteVeevaList))
{
$obj = $remoteVeevaList[$i];
$filename = $obj->{$veeva::LIST_VALUES}[8];
$remoteVeevaFilenames[] = $filename;
$i++;
}

$localVeevaList = array();
$query = $this->Asset->VeevaVault->find('all');
foreach($query as $veeva) {
$localVeevaList[] = $veeva['VeevaVault'];
}

if (!empty($localVeevaList)) {
foreach($localVeevaList as $localVeeva) {
if (in_array($localVeeva['title'], $remoteVeevaFilenames) == false) {

$id = $localVeeva['id'];
$asset_id = $localVeeva['asset_id'];

$sql = "DELETE FROM veeva_vaults WHERE id LIKE '" . $id . "'";
$db->rawQuery($sql);

$sql = "DELETE FROM assets WHERE id LIKE '" . $asset_id . "'";
$db->rawQuery($sql);
}
}
}
}
}

public function create_veeva_asset($veeva_id, $filename, $ext, $size, $mime, $image, $brand, $message_description, $message_title, $message_expire, $message_status, $url, $major_ver, $minor_ver)
{
$this->autoRender = false;

$uuid = String::uuid();
$db = ConnectionManager::getDataSource('default');

$sql = "INSERT INTO assets (id, user_id, original_name, name, ext, size, mime_type, is_image, created, modified, is_veeva, message_title, message_description, brand, status, expire_date) VALUES ('" . $uuid . "', null, '" . mysql_real_escape_string($filename) . "', '" . $uuid . "', '" . mysql_real_escape_string($ext) . "', " . mysql_real_escape_string($size) . ", '" . mysql_real_escape_string($mime) . "', " . mysql_real_escape_string($image) . ", now(), now(), 1, '" . mysql_real_escape_string($message_title) . "', '" . mysql_real_escape_string($message_description) . "', '" . mysql_real_escape_string($brand) . "', '" . mysql_real_escape_string($message_status) . "', '" . mysql_real_escape_string($message_expire) . "')";
$db->rawQuery($sql);

$sql = "INSERT INTO veeva_vaults (id, asset_id, veeva_id, title, description, url, created, size, major_ver, minor_ver, ext) VALUES ('" . String::uuid() . "', '" . $uuid . "', '" . $veeva_id . "', '" . mysql_real_escape_string($filename) . "', '" . mysql_real_escape_string($message_description) . "', '" . $url . "', now(), " . mysql_real_escape_string($size) . ", '" . mysql_real_escape_string($major_ver) . "', '" . mysql_real_escape_string($minor_ver) . "', '" . mysql_real_escape_string($ext) . "')";
$db->rawQuery($sql);
}

public function update_veeva_asset($id, $filename, $ext, $size, $mime, $image, $brand, $message_description, $message_title, $message_expire, $message_status, $url, $major_ver, $minor_ver)
{
$this->autoRender = false;

$db = ConnectionManager::getDataSource('default');

$sql = "UPDATE assets SET ext = '," . mysql_real_escape_string($ext) . "', size = " . mysql_real_escape_string($size) . ", mime_type = '" . mysql_real_escape_string($mime) . "', modified = now(), messages_title = '" . mysql_real_escape_string($message_title) . "', message_description = '" . mysql_real_escape_string($message_description) . "', brand = '" . mysql_real_escape_string($brand) . "', status = '" . mysql_real_escape_string($message_status) . "' WHERE original_name = '" . mysql_real_escape_string($filename) . "'";
$db->rawQuery($sql);

$sql = "UPDATE veeva_vaults SET veeva_id = '" . $id . "', description = '" . mysql_real_escape_string($message_description) . "', url = '" . $url . "', size = " . mysql_real_escape_string($size) . ", major_ver = " . mysql_real_escape_string($major_ver) . ", minor_ver = " . mysql_real_escape_string($minor_ver) . ", ext = " . mysql_real_escape_string($ext) . " WHERE title = '" . mysql_real_escape_string($filename) . "'";
$db->rawQuery($sql);
}
}
?>


cronjob:

*/10 * * * * cd /var/www/SalesGuru/content/app/Console/Command; /usr/bin/php -q SyncVeevaShell.php > /dev/null ;


Error message:

Error: Plugin SyncVeevaShell could not be found.
#0 /var/www/SalesGuru/content/lib/Cake/Core/App.php(364): CakePlugin::path('SyncVeevaShell')
#1 /var/www/SalesGuru/content/lib/Cake/Core/App.php(225): App::pluginPath('SyncVeevaShell')
#2 /var/www/SalesGuru/content/lib/Cake/Core/App.php(542): App::path('Console/Command', 'SyncVeevaShell')
#3 [internal function]: App::load('PhpShell')
#4 [internal function]: spl_autoload_call('PhpShell')
#5 /var/www/SalesGuru/content/lib/Cake/Console/ShellDispatcher.php(241): class_exists('PhpShell')
#6 /var/www/SalesGuru/content/lib/Cake/Console/ShellDispatcher.php(191): ShellDispatcher->_getShell('SyncVeevaShell....')
#7 /var/www/SalesGuru/content/lib/Cake/Console/ShellDispatcher.php(69): ShellDispatcher->dispatch()
#8 /var/www/SalesGuru/content/app/Console/cake.php(33): ShellDispatcher::run(Array)
#9 {main}


I'm fairly new to CakePHP and completely new to Shells and cronjobs.

Answer

There is an exact example of this very same use case in the book:

In your case:

*/10 * * * * cd /var/www/SalesGuru/content/app && Console/cake SyncVeeva > /dev/null

You

  • navigate into your APP dir
  • call Console/cake [ShellName without suffix] [optional command name]

The official documentation should always be your very first stop for information. Then google, then forums like this.

Note that you should be able to silence your shell with -q (--quit) param instead of using > /dev/null.