Symfony2 background process - symfony

How can I run a Symfony2 process in background?
My code:
class Start extends Symfony\Component\Console\Command\Command {
protected function configure() {
$this->setName('start');
}
protected function execute(InputInterface $input, OutputInterface $output) {
$process = new Process('java -jar ./selenium-server-standalone-2.42.2.jar');
$process->setTimeout(null);
$process->start();
$pid = $process->getPid();
$output->writeLn(sprintf('Started selenium with PID %d', $pid));
$process->wait();
}
}
How can I run this process in background? I guess I should redirect STDOUT of the new process to /dev/null but how can I do that?

Oh, I found it is possible in Symfony 2.5+, so I just updated it and added
$process->disableOutput();
before $process->start() and it works fine.
UPD.
Unfortunately, it does not work on Windows. I also tried to run 'START /B command' and
'CALL command > nul 2>&1'
but both of these failed: process was running but console was still blocked.

Related

Run a batch script before uninstallation using QT Installer Framework

I have a batch script that does some clean up, which is installed along the main application. However, I cannot seem to execute the batch file before uninstallation. Here is my install script:
function Component() {
}
function Controller() {
installer.setDefaultPageVisible(QTInstaller.TargetDirectory, false);
}
Controller.prototype.uninstallationStartedFunction = function() {
if(systemInfo.productType == "windows") {
installer.gainAdminRights();
installer.execute(installer.value("TargetDir") + "/disable.bat");
}
}
Any help is appreciated.
It depends if this is your component script or controller script. You can use the controller script and should connect to the installer signals handle to have a callback function to execute if the uninstallation is running. For example
function Controller() {
installer.uninstallationStarted.connect(this,Component.prototype.onUninstallationStarted);
}
Component.prototype.onUninstallationStarted = function()
{
// do your stuff
}

How to set up gmail push notification

I tried to understand how this works since more than a day, and I'm totally confused right now.
Here is my (very simple) goal: make a GET request on a URL when I receive a new email.
I have created a topic as they asked (named new-email)
I have a subscription in that topic (named new-email-inbox), with delivery type push, and setting my endpoint URL.
I gave my subscription authorization to this account: gmail-api-push#system.gserviceaccount.com (Editor & Editor Pub/Sub)
How do I set up the rest ? I don't understand what to do now..
I did a Symfony4 command, executed everyday like a cron:
class WatchGoogleClient extends Command {
private $kernel;
private $gmailService;
public function __construct(GmailService $gmailService, Kernel $kernel)
{
parent::__construct();
$this->gmailService = $gmailService;
$this->kernel = $kernel;
}
protected function configure()
{
$this->setName('app:watch-google-client')
->setDescription('watch-google-client')
->setHelp('Reset the google watch timer');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// This getClient function is basically what is given by the google API tutorial
$client = $this->gmailService->getClient($this->kernel);
$service = new \Google_Service_Gmail($client);
$watchreq = new \Google_Service_Gmail_WatchRequest();
$watchreq->setLabelIds(array('INBOX'));
$watchreq->setTopicName('YOUR_TOPIC_NAME');
$msg = $service->users->watch('me', $watchreq);
var_dump($msg);
// DO WHAT YOU WANT WITH THIS RESPONSE BUT THE WATCH REQUEST IS SET
}
}

Symfony mail on Command class - get undefined method named "get"

I try to send mail (swiftmail) via command of symfony.
Here is my code :
class CommandMail extends Command
{
protected static $defaultName = 'app:send-daily-mail';
protected function configure() {
$this
->setDescription('Send automatic reminders mail everyday.')
->setHelp('This command allows you to send automatic reminder mail to Rhys, everyday...');
}
protected function execute(InputInterface $input, OutputInterface $output) {
$message = (new \Swift_Message('test auto mail cron 12 24 TEST'))
->setFrom('xxxxxx.xxxxxxx#gmail.com')
->setTo('wwwww.wwwwwww#gmail.com')
->setBody('test body');
$this->get('mailer')->send($message);
}
}
I have following error :
In CommandMail.php line 54:
Attempted to call an undefined method named "get" of class
"AppBundle\Command\CommandMail".
Did you mean to call e.g. "getAliases", "getApplication",
"getDefaultName", "getDefinition", "getDescription", "getHelp",
"getHelper", "getHelperSet", "getName", "getNativeDefin ition",
"getProcessedHelp", "getSynopsis" or "getUsages"?
I try many things (getContainer() ie and many others) but nothing is working.
Thanks for your help !
(Symfony 3, SMTP gmail)
If you are using Symfony 4, inject the dependency by constructor:
private $swiftMailerService;
public function __construct(\Swift_Mailer $swiftMailerService)
{
parent::__construct();
$this->swiftMailerService = $swiftMailerService;
}
protected function execute(InputInterface $input, OutputInterface $output) {
$message = (new \Swift_Message('test auto mail cron 12 24 TEST'))
->setFrom('xxxxxx.xxxxxxx#gmail.com')
->setTo('wwwww.wwwwwww#gmail.com')
->setBody('test body');
$this->swiftMailerService->send($message);
}

Suppress symfony 4 response body in phpunit test

I am migrating a Silex app to Symfony Flex and everything is working so far, except that when I run the phpunit tests I get the response body output into the phpunit output.
ie.
> bin/phpunit
#!/usr/bin/env php
PHPUnit 6.5.13 by Sebastian Bergmann and contributors.
Testing unit
.......<http://data.nobelprize.org/resource/laureate/914> a <http://data.nobelprize.org/terms/Laureate> , <http://xmlns.com/foaf/0.1/Person> ;
<http://www.w3.org/2000/01/rdf-schema#label> "Malala Yousafzai" ;
<http://data.nobelprize.org/terms/laureateAward> <http://data.nobelprize.org/resource/laureateaward/974> ;
<http://data.nobelprize.org/terms/nobelPrize> <http://data.nobelprize.org/resource/nobelprize/Peace/2014> ;
the entire RDF document then
. 8 / 8 (100%)
Time: 1.07 seconds, Memory: 14.00MB
OK (8 tests, 71 assertions)
Generating code coverage report in Clover XML format ... done
So it is working fine, but I can't figure out how to disable this output?
The request is simply
$this->client->request('GET', "/nobel_914.ttl", [], [], ['HTTP_ACCEPT' => $request_mime]);
$this->assertEquals(200, $this->client->getResponse()->getStatusCode(), "GET should be allowed.");
$response = $this->client->getResponse();
$charset = $response->getCharset();
etc.
and the client is setup in a base class like this
class MyAppTestBase extends WebTestCase
{
/**
* #var \Symfony\Component\BrowserKit\Client
*/
protected $client;
/**
* {#inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->client = static::createClient();
$this->client->catchExceptions(false);
}
I'm sure I'm missing something obvious but this is new to me. I am running in the 'test' environment and with 'debug' == false.
Any help appreciated.
So this was probably a problem all along but just started being exposed in the switch from Silex to Symfony Flex.
We were streaming responses via
$filename = $this->path;
$stream = function () use ($filename) {
readfile($filename);
};
return new StreamedResponse($stream, 200, $res->headers->all());
and the readfile was throwing the content to the output buffer. Switching the readfile to file_get_contents resolved this
$filename = $this->path;
$stream = function () use ($filename) {
file_get_contents($filename);
};
return new StreamedResponse($stream, 200, $res->headers->all());

How to set session flash message in commad symfony2?

I've a file in testBundle>Command>ReportCommand.php where I want to set flash message like below but it's not working. I've also added this namespace but it didn't work too:-use Symfony\Component\HttpFoundation\Request;
$this->get('session')->getFlashBag()->add(
'notice', sprintf('%s email sent!', str_replace('_', ' ', ucfirst($type)))
);
You cannot use sessions from command line, you can only use them with the HTTP way. Try to store your message in a différent way :
In a file
In your MySQL database
In a RAM cache (E.g. redis)
etc...
You can use outer interface to show the message on command prompt.
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class classname extends ContainerAwareCommand {
protected function configure()
{
// command details
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// your script code
$output->writeln("Your message");
}
}

Resources