Search code examples
phplaraveltwittercron

Ubuntu 15 proceed codebird-php stream by cron not works


I have a laravel project with codebird-php lib there. And I trying to set a cron job to start scheduling my streaming commands.
On my local machine with Ubuntu 16 it works perfectly, every minute creating a stream to twitter and on 55 second it dies.

But on the remoted VPS it not scheduled my command. I've tried it by cron, by tiping in terminal and have no efeect. Can anyone explain me where I've got a stupid mistake and what difference have in cron work Ubuntu 15.04 and Ubuntu 16.04? Here is the code of commands: connect command setting streams and start them

class ConnectToStreamingAPI extends Command
{

    protected $signature = 'connect';
    protected $run = [];

    protected $users;


    protected $description = 'Connect to the Twitter Streaming API to search tweets by keywords';

    protected $twitterStream;


    public function __construct(User $user)
    {
        $this->users = $user::where('id', '>', '1')->pluck('id')->toArray();
        parent::__construct();
    }


    public function handle()
    {
        //set_time_limit(59);
        $this->setRun();
        $this->runUserStreamCommand();

        return "Fired";

    }

    protected function setRun()
    {
        foreach ($this->users as $user) {
            $this->addToRun($user);
        }
    }

    protected function runUserStreamCommand()
    {
        foreach ($this->run as $process) {
            $process->setPty(true);
            $process->start();
        }
        $count = count($this->run);
        while (count($this->run) > 0) {
            foreach ($this->run as $key => $process) {
                $childProcess = $process->getPid() + $count;
                try {
                    $process->checkTimeout();
                } catch (ProcessTimedOutException $e) {
                    exec("kill -15 " . $childProcess);
                }
                if (!$process->isRunning()) {
                    unset($this->run[$key]);
                }
            }
        }

    }

    protected function addToRun($user_id)
    {
        $this->run[] = new Process("php /path/to/artisan stream $user_id", null, null, null, 50);
    }
}

And here is Stream command

class StreamingCommands extends Command
{

    protected $signature = 'stream {id}';


    protected $description = 'create stream to twitter for one user';


    public function __construct()
    {
        parent::__construct();
    }


    public function handle()
    {
        $id = $this->argument('id');
        $user = User::find($id);
        Codebird::setConsumerKey($user->consumer_key, $user->consumer_secret);
        $stream = new TwitterStream();
        $stream->setToken($user->twitterData->token, $user->twitterData->token_secret);
        $keywordArray = array();
        $ids = array();
        foreach ($user->campaigns as $campaign) {
            $ids[] = $campaign->id;
            $keywordArray = array_merge($keywordArray, Keyword::whereCampaignId($campaign->id)->pluck('keyword')->toArray());
        }
        $keywords = Keyword::whereIn('campaign_id', $ids)->where('rejected', 0)->get();
        $stream->setKeywords($keywords);
        $stream->setStreamingCallback('processTweet');
        $keywords = implode(',', $keywordArray);
        $this->info("start stream to user $id");
        $stream->statuses_filter('track=' . $keywords);

    }
}

And cron job * * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1 And schedule is just a $schedule->command('connect')->everyMinute();


Solution

  • As for the comments and the changes, you clearly have two different systems 16 and 15, with different software and dependencies installed. Unfortunately, there's no simple way of fixing this kind of glitch, you have to check everything, it can still be a not listed PHP dependency or even an operating system dependency and/or user acces rights (you are starting and killing processes, is it working or abending?), but your app is working fine in 16, so you can probably forget about Laravel and Composer and focus on PHP and the OS layers.

    The thing is, from here, without acces to your server, it's very hard to guess, so sorry if it doesn't help much.