如何在铁工作者任务中使用PHP的MongoDB驱动程序?

时间:2016-02-19 20:28:21

标签: php mongodb iron.io ironworker

我使用iron.io及其工作任务进行后台处理。 我的代码非常简单,就像:

<?php
require 'vendor/autoload.php';

$uri = "my_connection_string";
$client = new MongoClient($uri);
:
:

composer.json就像:

{
    "require": {
        "iron-io/iron_worker": "2.0.4",
        "wp-cli/php-cli-tools": "~0.10.3"
    }
}

然后我首先安装所有依赖项,

docker run --rm -it -v "$PWD":/worker -w /worker iron/php:dev composer install

压缩我的代码,

zip -r worker.zip .

上传,

iron --env dev worker upload --name task-name -zip ./worker.zip iron/php:dev php ./worker.php

然后输出错误。

PHP Fatal error:  Class 'MongoClient' not found in /mnt/task/worker.php on line xxx

我还尝试MongoDB\Driver\ManagerMongoDB\Driver\Client,发生了同样的错误。当我尝试运行php -m时,会显示:

[PHP Modules]
Core
curl
date
ereg
fileinfo
filter
hash
json
libxml
mbstring
mysqlnd
pcre
readline
Reflection
session
SimpleXML
SPL
standard
tokenizer
xmlwriter

[Zend Modules]

看起来甚至没有安装MongoDB扩展,而他们说它已安装。 http://dev.iron.io/worker/languages/php/

我错过了什么吗?如何在PHP中使用MongoDB驱动程序与铁工人一起使用?

1 个答案:

答案 0 :(得分:3)

对于那些可能与我处于同样情况的人来说,这是因为Iron.io现在集成了Docker,我们必须指定运行代码时使用的图像,但是铁/ php图像不包含任何其他图像他们曾经支持的中间件。

所以我们必须编写自己的DockerFile来安装MongoDB或MySQL或者任何必要的中间件&amp;我们需要的软件。

至于MongoDB,这是一个Dockerfile的例子。

Imports System.Net

Public Class Form1
    Private sw As New Stopwatch

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim req As HttpWebRequest = HttpWebRequest.Create("http://example.com")
        Dim state As New RequestState
        state.req = req
        req.BeginGetResponse(AddressOf respCallback, state)
    End Sub

    Private Sub respCallback(ar As IAsyncResult)
        Dim state As RequestState = ar.AsyncState
        state.resp = state.req.EndGetResponse(ar)
        state.respStream = state.resp.GetResponseStream
        state.respStream.BeginRead(state.buffer, 0, 1024, AddressOf readCallback, state)
    End Sub

    Private Sub readCallback(ar As IAsyncResult)
        Dim state As RequestState = ar.AsyncState
        Dim read As Integer = state.respStream.EndRead(ar)
        If read > 0 Then
            state.respBody += System.Text.ASCIIEncoding.ASCII.GetString(state.buffer, 0, read)
            state.respStream.BeginRead(state.buffer, 0, 1024, AddressOf readCallback, state)
        Else
            state.respStream.Close()
            MsgBox(state.respBody)
            state.Dispose()
        End If
    End Sub

Public Class RequestState
    Implements IDisposable

    Public req As HttpWebRequest
    Public resp As HttpWebResponse
    Public respStream As IO.Stream
    Public buffer(1024) As Byte
    Public respBody As String

#Region "IDisposable Support"
Protected Overridable Sub Dispose(disposing As Boolean)
    If Not disposedValue Then
        If disposing Then
            ' TODO: dispose managed state (managed objects).
            req = Nothing
            resp.Close()
            resp.Dispose()
            resp = Nothing
            respStream.Close()
            respStream.Dispose()
            respStream = Nothing
            buffer = Nothing
            respBody = Nothing
        End If

        ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
        ' TODO: set large fields to null.
    End If
    disposedValue = True
End Sub

    ' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
        Dispose(True)
        ' TODO: uncomment the following line if Finalize() is overridden above.
        ' GC.SuppressFinalize(Me)
    End Sub
#End Region
End Class

请注意,根据图像尺寸,这根本没有优化。

我认为,如果Iron.io提供此类图片,这将非常有用。