我在Azure上使用WebJobs运行Laravel后台任务时遇到了一个奇怪的问题。
我有这个代码在我的Laravel ExpireContactsFromDatabase命令的handle()方法
public function handle()
{
* Update contacts that are old and past their expiry date but that have not been deleted
* Set their deleted at to the current timestamp
*/
$contact = Contact::where('expiry_date', '<=', date('Y-m-d H:i:s'))
->whereNull('deleted_at')
->update(array('deleted_at' => date('Y-m-d H:i:s')));
Log::info("200" . " " . "Contacts clearing executed successfully, see response from db :::: " . serialize($contact));
}
我已经成功在app'Console'Kernel.php中注册了这个命令,如下所示:
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
Inspire::class,
ExpireContactsFromDatabase::class,
];
/**
* Define the application's command schedule.
*
* @param 'Illuminate'Console'Scheduling'Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')
->hourly();
$schedule->command('expire:contacts')
->everyMinute()
->sendOutputTo('expired_contacts_results_dbresponse.txt');
}
}
这在我的本地开发机器上运行得很好。我在我的笔记本电脑上配置了一个cron作业,它执行了这个命令,并按照预期使旧的联系人失效。
当我将应用程序从localhost迁移到Ms Azure时,问题出现了。我将WebJob配置为连续运行,为它提供了artisan的路径,并发布了调度运行。我使用.bat文件来完成此操作,如下所示:
php D:'home'site'wwwroot'artisan schedule:run > D:'home'site'wwwroot'expired_contacts_results_confirmation.txt
PHP在我的web应用程序路径上,所以不用担心。
挑战是当命令执行/当我运行php artisan schedule::run
并检查我的日志文件时,我看到数据库(登录到expired_contacts_results_dbresponse.txt)的响应如下:
[2016-10-20 12:21:43] local.ERROR: exception 'PDOException' with message 'SQLSTATE[HY000] [2002] An attempt was made to access a socket in a way forbidden by its access permissions.
' in D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Database'Connectors'Connector.php:55
我的猜测是laravel连接数据库的方式在这种情况下有问题。然而,我的应用程序的其他组件使用相同的连接代码,他们工作得很好,所以我很困惑。在互联网上搜索这个问题的精确解决方案没有奏效。
我在D1共享主机计划的Azure上运行我的应用程序,并使用方便的Azure MySQLInApp数据库作为我的数据库存储。
这是我的完整堆栈跟踪。
Stack trace:
#0 D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Database'Connectors'Connector.php(55): PDO->__construct('mysql:host=;dbn...', '', '', Array)
#1 D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Database'Connectors'MySqlConnector.php(22): Illuminate'Database'Connectors'Connector->createConnection('mysql:host=;dbn...', Array, Array)
#2 D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Database'Connectors'ConnectionFactory.php(60): Illuminate'Database'Connectors'MySqlConnector->connect(Array)
#3 D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Database'Connectors'ConnectionFactory.php(49): Illuminate'Database'Connectors'ConnectionFactory->createSingleConnection(Array)
#4 D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Database'DatabaseManager.php(175): Illuminate'Database'Connectors'ConnectionFactory->make(Array, 'mysql')
#5 D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Database'DatabaseManager.php(67): Illuminate'Database'DatabaseManager->makeConnection('mysql')
#6 D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Database'Eloquent'Model.php(3224): Illuminate'Database'DatabaseManager->connection(NULL)
#7 D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Database'Eloquent'Model.php(3190): Illuminate'Database'Eloquent'Model::resolveConnection(NULL)
#8 D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Database'Eloquent'Model.php(1870): Illuminate'Database'Eloquent'Model->getConnection()
#9 D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Database'Eloquent'Model.php(1813): Illuminate'Database'Eloquent'Model->newBaseQueryBuilder()
#10 D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Database'Eloquent'Model.php(1787): Illuminate'Database'Eloquent'Model->newQueryWithoutScopes()
#11 D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Database'Eloquent'Model.php(3442): Illuminate'Database'Eloquent'Model->newQuery()
#12 [internal function]: Illuminate'Database'Eloquent'Model->__call('where', Array)
#13 [internal function]: App'Contact->where('expiry_date', '<=', '2016-10-20 12:2...')
#14 D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Database'Eloquent'Model.php(3458): call_user_func_array(Array, Array)
#15 D:'home'site'wwwroot'app'Console'Commands'ExpireContactsFromDatabase.php(53): Illuminate'Database'Eloquent'Model::__callStatic('where', Array)
#16 D:'home'site'wwwroot'app'Console'Commands'ExpireContactsFromDatabase.php(53): App'Contact::where('expiry_date', '<=', '2016-10-20 12:2...')
#17 [internal function]: App'Console'Commands'ExpireContactsFromDatabase->handle()
#18 D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Container'Container.php(507): call_user_func_array(Array, Array)
#19 D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Console'Command.php(150): Illuminate'Container'Container->call(Array)
#20 D:'home'site'wwwroot'vendor'symfony'console'Command'Command.php(256): Illuminate'Console'Command->execute(Object(Symfony'Component'Console'Input'ArgvInput), Object(Symfony'Component'Console'Output'ConsoleOutput))
#21 D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Console'Command.php(136): Symfony'Component'Console'Command'Command->run(Object(Symfony'Component'Console'Input'ArgvInput), Object(Symfony'Component'Console'Output'ConsoleOutput))
#22 D:'home'site'wwwroot'vendor'symfony'console'Application.php(846): Illuminate'Console'Command->run(Object(Symfony'Component'Console'Input'ArgvInput), Object(Symfony'Component'Console'Output'ConsoleOutput))
#23 D:'home'site'wwwroot'vendor'symfony'console'Application.php(190): Symfony'Component'Console'Application->doRunCommand(Object(App'Console'Commands'ExpireContactsFromDatabase), Object(Symfony'Component'Console'Input'ArgvInput), Object(Symfony'Component'Console'Output'ConsoleOutput))
#24 D:'home'site'wwwroot'vendor'symfony'console'Application.php(121): Symfony'Component'Console'Application->doRun(Object(Symfony'Component'Console'Input'ArgvInput), Object(Symfony'Component'Console'Output'ConsoleOutput))
#25 D:'home'site'wwwroot'vendor'laravel'framework'src'Illuminate'Foundation'Console'Kernel.php(107): Symfony'Component'Console'Application->run(Object(Symfony'Component'Console'Input'ArgvInput), Object(Symfony'Component'Console'Output'ConsoleOutput))
#26 D:'home'site'wwwroot'artisan(36): Illuminate'Foundation'Console'Kernel->handle(Object(Symfony'Component'Console'Input'ArgvInput), Object(Symfony'Component'Console'Output'ConsoleOutput))
我将非常感谢任何帮助。
似乎Web应用程序中的内部PHP运行时没有足够的权限访问应用程序中的MySQL。在我的测试中,我甚至创建了一个测试脚本,利用测试连接代码获取数据库连接字符串,并在KUDU控制台站点执行PHP test.php
,我会得到同样的问题。
但它工作得很好,如果我通过HTTP请求访问它(这将通过IIS处理)。因此,请尝试以下解决方案。实现您的功能并将其公开为Restful API。在Webjob脚本中,您可以生成一个HTTP请求来调用该API。
如有任何问题,请随时与我联系。
支持该场景-请参阅此。我们正在努力使这个场景更好。