I am using laravel 5. What I need is to call python gearman worker.
I have created a gearman worker in python.
And I have created a gearman client in laravel 5.
I have directly added gearman client code in my controller like:
$client = new GearmanClient();
$client->addServer();
$job_data = array("func_name" => "searchTweets", "query" => "query_to_search");
$result = $client->doNormal("php_twitter_worker", json_decode($job_data));
if ($result) {
print_r($result);
}
But it throws error :
Class 'App\Http\Controllers\GearmanClient' not found
I know what is the error because I don't have GearmanClient Class in Controllers. Actually I don't know how to use it.
After doing some R&D , I found a package using gearman in laravel5 but not getting how to use it as Gearman Client and how it make a call to my python gearman worker.
Any help on this ?
Because your class is namespaced, your controller there, PHP will look for a class inside of this namespace.
Therefore, to use a class from the root namespace, you should either use a use statement or prefix the classname with \, \ being the root namespace.
use GearmanClient;
$client = new \GearmanClient();
Use one or the other, not both, my code above is actually quite confusing...
Related
Now i'm doing some project use laravel framework. do i able to run Symfony Process function inside a queue jobs?
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
right now i want to run some commend using Symfony Process function for this
process = new Process("facebook-scraper --filename public/data/nintendo.csv --pages 5 nintendo");
if outside the queue. this code can run succesful. but when i want to make it run inside the queue jobs. it can't.
how do i able to run symfony Process function inside queue on jobs laravel.
I think the problem is the paths. Replace the --filename option value with the absolute path (from /):
$path = public_path('data/nintendo.csv');
$process = new Process("facebook-scraper --filename {$path} --pages 5 nintendo");
...
...
And try to use full path to executable (facebook-scraper).
You can use which to find it.
Example:
$ which facebook-scraper
/usr/bin/facebook-scraper
I am trying to run python files in Laravel Project.Therefore, i use the Symfony\Process package.My code looks like this:
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
$process = new Process(["python","C:\xampp\htdocs\backend\Scrape\AnnouncementsScrape.py"]);
$process->run();
When i use this line:
throw new ProcessFailedException($process);
my laravel project returns error (I use laravel framework for my backend in my mobile app, so i can not see the exactly error; i can only see that there is problem with my backend)
Το sum up, i run the code above, but the Process is not successfull.
I check it with the condition:
if ($process->isSuccessful())
Anyone any idea?
Although the process may not be successful, it still produces an output, which you can access.
You can use $process->getOutput() and $process->getErrorOutput() for that
My existing Laravel project is such that all the tasks are executed sequentially. I have identified part of the code which can be run in parallel using PHP threads and can reduce the response time.
Instead of using pthreads, there was suggestion given that why not use appserver.io - which is fully multithreaded php server itself. One can use its MessageQueue feature, add all your job to this queue, and it will automatically fork worker threads. You don't have to manage anything.
I have already deployed existing Laravel app on appserver.io (copied project under /opt/appserver/webapps/ folder) but now I don't know how to use appserver's MessageQueue. My project uses psr-4, where as appserver is psr-0. Laravel has it's own DI and so does appserver.
All I want to do is, use appserver's MessageQueue to get more workers executing one function in parallel. I'm new to appserver and not sure how the directory structure should look like or what configuration I have do it. Any pointers will be helpful.
you can connect and send to the MessageQueue from within your Laravel application. First you've to install the client library appserver-io/messaging by adding "appserver-io/messaging" : "~1.0" to your composer.json. Then you can send a message with
$queue = MessageQueue::createQueue('pms/myQueue');
$connection = QueueConnectionFactory::createQueueConnection('my-laravel-app');
$session = $connection->createQueueSession();
$sender = $session->createSender($queue);
$sender->send(new StringMessage('Some String'));
assuming you've an application named my-laravel-app, that resides in the folder /opt/appserver/webapps/my-laravel-app and a MessageQueue called pms/myQueue, defined in a file META-INF/message-queues.xml. The file would look like
<?xml version="1.0" encoding="UTF-8"?>
<message-queues xmlns="http://www.appserver.io/appserver">
<message-queue type="MyReceiverClass">
<destination>pms/myQueue</destination>
</message-queue>
</message-queues>
In that example, the receiver class MyReceiverClass has to be available under /opt/appserver/webapps/my-laravel-app/META-INF/classes/MyLaravelApp/Receivers/MyReceiverClass for example.
A good start is the example application that comes with simple MessageQueue example running some simple import functionality.
Original Post
Good evening folks. I have a laravel setup and I'm trying to have a cronjob execute a php function to a file within the laravel project directory.
I am getting class and name space errors when I try to do something like this:
<?php
require_once('../laravel/app/Http/Controllers/NotificationsController.php');
and then calling the processQueuedNotifications() function.
This of course gives me errors, what is the correct way to call my function within the laravel directory? I need to call this function as this function has all the correct namespaces and extended controllers necessary to execute the function properly.
Update 1:
Thanks to #michael, I've been made aware of a component in Laravel called commands.
So I ran this code:
php artisan make:console processQueuedNotifications
and it created some files in the console directory.
Currently exploring on what to do next.
After checking out the Events class which the kernel.php file makes use of, I noticed that this class provides an easy to use interface for me to create cron jobs on the fly. Am I correct in think so?
I notice there is not function to run a cron job every minute, is it safe to edit the Events class file without it being overwritten by future make:console commands, or laravel updates?
I saw this code in the kernel.php file:
$schedule->command('inspire')
->hourly();
So is this the place you wanted me to add my function? as I notice that the inspire function is something automatically created for me to understand what's going on?
So I would write,
$schedule->command('processQueuedNotifications')
->everyMinute();
//Providing it's safe to edit the Event's class or figure out a clean way of doing so without my code being deleted in the future on Laravel updates.
A very convenient way is to use laravels console component. You can create a new command by issuing
php artisan make:console
And find it thereafter in your app/console directory. Make sure to enable the command in the Kernel.php file once created.
Simply call your class or whatever you want to run via cron from inside the command. The console command itself is callable via cli just as you would run one of laravels php artisan ... commands. You can set this in the file created for you. For example, you can then call the file from everywhere you want with
/usr/bin/php /path/to/file/artisan my:command
You can set options and arguments if you need to.
Here's the documentation: http://laravel.com/docs/5.0/commands / http://symfony.com/doc/current/components/console/introduction.html
There's an array in kernel.php you need to register your class (include the namespace) in. After that it is callable via cli. For a start, have a look on arguments and options you can initialize in case you need to make different requests on your controller class. (The filename you have chosen for your console command, is an argument. You can make them required or optional for your own commands. )
Within your file, you can create them by simply creating an array in the appropriate method with these values:
[$name, $mode, $description, $defaultValue]
have a look at the docs or Jeffrey's laracasts, they are very good.
To only call your class from the console command, it's enough to name your command in the above section of the file and call you controller like
(new namespace\controller)->method();
What you can do in your code, after your update, 2 choices :
Dispatching directly the command from your code using the Bus facade
first import it using the
use Illuminate\Support\Facades\Bus;
then in your code
Bus::dispatchNow(new YourCommandClass);
(don't forget to import your command class)
Dispatch it for queue process using the same bus facade:
(still importing the same way)
Bus::dispatch(new YourCommandClass);
(Note that in that case, you'll need to have the following command run by your cron job :
php artisan queue:listen
it can handle several options such as the --tries=X where is is the number of tries etc
Generally speaking, you can get more info from commands typing php artisan my:command -h
Is it possible to use phalcon in cli applications to handle requests with argv parameters?
I want to use argv parameters to understand command that should be executed, e.g.
./script.php robot/create --color=red --feature=weapon
and to get this inside my application with controllers, actions, etc in this way:
controller: robot
action: create
GET params: color=red,feature=weapon
Is it possible using CLI classes like
Phalcon\ClI\Dispatcher
http://docs.phalconphp.com/en/latest/api/Phalcon_CLI_Dispatcher.html
Phalcon\CLI\Console http://docs.phalconphp.com/en/latest/api/Phalcon_CLI_Console.html
Phalcon\CLI\Task http://docs.phalconphp.com/en/latest/api/Phalcon_CLI_Task.html
and other similar?
There are no docs and how-to manuals... Perhaps somebody has experience or just an idea.
I understand that we have to define DI and initialize application, but how to make this in a more native way I just don't have any ideas.
Also, one more question: can phalcon handle argv parameters automatically?
As I understand, we should start Phalcon\CLI\Console object as application and pass to it DI. But the whole process/scenario... I just can't get it :)
So, i have following folders structure:
~/www
~/www/app
~/www/app/models
~/www/app/controllers - controllers for web
~/www/app/tasks - task for cli
~/www/public/app.php - web application
~/www/cli/app.php - console application
In cli/app.php i have following code:
#!/usr/bin/php
<?php
/**
* This makes our life easier when dealing with paths.
* Everything is relative to the application root now.
*/
chdir(dirname(__DIR__));
/**
* Init loader
*/
$loader = new \Phalcon\Loader();
$loader->registerDirs(['app/tasks/'])
->register();
/**
* Setup dependency injection
*/
$di = new Phalcon\DI();
// Router
$di->setShared('router', function() {
return new Phalcon\CLI\Router();
});
// Dispatcher
$di->setShared('dispatcher', function() {
return new Phalcon\CLI\Dispatcher();
});
/**
* Run application
*/
$app = new Phalcon\CLI\Console();
$app->setDI($di);
$app->handle($argv);
then i put my tasks classes in app/tasks folder.. and it just works.
perhaps this will help somebody ;)
I'm a Phalcon enthusiast, but actually tasks seem to me something to create cron jobs. They are something different from commands. I don't think tasks are the right way to create a complete cli application. There are many limitation. I really suggest you to use Symfony Console instead: it's well documented, it manages multi arguments and params, it provides automatically a command line help and you can install it via Composer.
If you are creating cron jobs you can go with Phalcon tasks, but if your intention is to create a cli application, take a look at Symfony Console.
can phalcon handle argv parameters automatically?
yes, PHP will pass command line parameters to $argv in any framework.