symfony 写定时任务

最近工作上遇到了定时任务,刚好玩的是symfony,看到symfony中有commands这个东西。

我在上次已经说过怎么建立一个command了。

这次刚好结合起来一块做定时脚本。

直接新建立一个命令。

command中代码直接贴出来。

<?php

namespace GamesBundle\Command;

use GamesBundle\Entity\GameNperRecord;
use GamesBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class GetLotteryCommand extends ContainerAwareCommand
{
    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            ->setName('games:curl_get_lottery')
            ->setDescription('采集最新一条数据的脚本');
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln([
            '采集数据中...',// A line
            '============',// Another line
            '',// Empty line
        ]);

        $url   = $this->getContainer()->getParameter('caipiao_url');
        $data = array(
            'name' => $this->getContainer()->getParameter('caipiao_name'),
            'format' => $this->getContainer()->getParameter('caipiao_format'),
            'uid' => $this->getContainer()->getParameter('caipiao_uid'),
            'token' => $this->getContainer()->getParameter('caipiao_token'),
            'num'   =>  $this->getContainer()->getParameter('caipiao_limit'),
        );
        $lotteylist = json_decode($this->getContainer()->get('curl_data')->curl($url, 'get',$data),true);

        $doctrine = $this->getContainer()->get('doctrine');
        $em = $doctrine->getManager();
        foreach ($lotteylist as $k=>$v)
        {
            $gamenper = new GameNperRecord();
            $gamenper->setNper($k);
            $gamenper->getOpenAt(strtotime($v['dateline']));
            $lotteyNumArr = explode(',',$v['number']);
            $gamenper->setLotteryNumberA($lotteyNumArr[0]);
            $gamenper->setLotteryNumberB($lotteyNumArr[1]);
            $gamenper->setLotteryNumberC($lotteyNumArr[2]);
            $gamenper->setLotteryNumberD($lotteyNumArr[3]);
            $gamenper->setLotteryNumberE($lotteyNumArr[4]);
            $gamenper->setLotteryNumberF($lotteyNumArr[5]);
            $gamenper->setLotteryNumberG($lotteyNumArr[6]);
            $gamenper->setLotteryNumberH($lotteyNumArr[7]);
            $gamenper->setLotteryNumberI($lotteyNumArr[8]);
            $gamenper->setLotteryNumberj($lotteyNumArr[9]);
            $em->persist($gamenper);
            $em->flush($gamenper);
        }

        // Now you can get repositories
        // $usersRepo = $em->getRepository("myBundle:Users");
        // $user = $usersRepo->find(1);

        // outputs multiple lines to the console (adding "\n" at the end of each line)

        $output->writeln('执行成功!');
    }
}

然后 php bin/console 看看

symfony写定时任务

发现多了一条命令。

我们尝试执行以后,如果程序写的没问题。

symfony写定时任务

symfony写定时任务

于是命令算是这样就好了,然后我们去操作定时任务。

我的操作是mac

于是命令行就是这样了

php /home/wwwroot/gametest/game/bin/console games:clear_chat_msg

sudo crontab -e

*/1 * * * * php /Applications/MAMP/htdocs/games/bin/console games:curl_get_lottery

然后这样就好了。

每分钟执行一次的程序就自动执行了。

讨论数量: 3

Please format your article ,

so happy you can open a channel to write symfony article ,

but your code can more elegant.

suggest : You can use the jobby package to add the crojob.

4年前 评论

图片崩了老铁

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!