diff --git a/README.md b/README.md index eb5cee8..bce0264 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,30 @@ $fcClient->createFunction( //Invoke function synchronously. $fcClient->invokeFunction('service_name', 'function_name'); +/* +Create function with initializer. +the current directory has a main.zip file (main.php which hava functions of my_handler and my_initializer) +set environment variables {'testKey': 'testValue'} +*/ +$fcClient->createFunction( + 'service_name_with_initializer', + array( + 'functionName' => $functionName, + 'handler' => 'index.handler', + 'initializer' => 'index.initializer', + 'runtime' => 'php7.2', + 'memorySize' => 128, + 'code' => array( + 'zipFile' => base64_encode(file_get_contents(__DIR__ . '/main.zip')), + ), + 'description' => "test function with initializer", + 'environmentVariables' => ['testKey' => 'testValue'], + ) + ); + +//Invoke function synchronously. +$fcClient->invokeFunction('service_name_with_initializer', 'function_name'); + //Create trigger, for example: oss trigger $prefix = 'pre'; $suffix = 'suf'; diff --git a/src/AliyunFC/Client.php b/src/AliyunFC/Client.php index 8808bec..4705c1c 100644 --- a/src/AliyunFC/Client.php +++ b/src/AliyunFC/Client.php @@ -92,7 +92,6 @@ private function doRequest($method, $path, $headers, $data = null, $query = []) if ($query) { $options['query'] = $query; } - $res = $client->request($method, $url, $options); $respStatusCode = $res->getStatusCode(); @@ -262,11 +261,13 @@ private function normalizeParams(&$opts) { if (!(isset($opts['functionName']) && isset($opts['runtime']) && isset($opts['handler']) && isset($opts['code']))) { throw new \Exception('functionName|handler|runtime|code parameters must be specified'); } - $opts['functionName'] = strval($opts['functionName']); - $opts['runtime'] = strval($opts['runtime']); - $opts['handler'] = strval($opts['handler']); - $opts['memorySize'] = isset($opts['memorySize']) ? intval($opts['memorySize']) : 256; - $opts['timeout'] = isset($opts['timeout']) ? intval($opts['timeout']) : 60; + $opts['functionName'] = strval($opts['functionName']); + $opts['runtime'] = strval($opts['runtime']); + $opts['handler'] = strval($opts['handler']); + $opts['initializer'] = isset($opts['initializer']) ? strval($opts['initializer']) : null; + $opts['memorySize'] = isset($opts['memorySize']) ? intval($opts['memorySize']) : 256; + $opts['timeout'] = isset($opts['timeout']) ? intval($opts['timeout']) : 60; + $opts['initializationTimeout'] = isset($opts['initializationTimeout']) ? intval($opts['initializationTimeout']) : 30; } public function createFunction($serviceName, $functionPayload, $headers = []) { diff --git a/test/client_test.php b/test/client_test.php index 9234de8..c70edac 100644 --- a/test/client_test.php +++ b/test/client_test.php @@ -245,13 +245,15 @@ private function subTestListServices() { $this->assertEquals($services[0]['serviceName'], $prefix . 'abd'); } - private function checkFunction($function, $functionName, $desc, $runtime = 'php7.2', $handler = 'index.handler', $envs = ['TestKey' => 'TestValue']) { + private function checkFunction($function, $functionName, $desc, $runtime = 'php7.2', + $handler = 'index.handler', $envs = ['TestKey' => 'TestValue'], $initializer=null) { $etag = $function['headers']['Etag'][0]; $this->assertTrue($etag != ''); $function = $function['data']; $this->assertEquals($function['functionName'], $functionName); $this->assertEquals($function['runtime'], $runtime); $this->assertEquals($function['handler'], $handler); + $this->assertEquals($function['initializer'], $initializer); $this->assertEquals($function['description'], $desc); $this->assertEquals($function['environmentVariables'], $envs); $this->assertTrue(isset($function['codeChecksum'])); @@ -261,6 +263,7 @@ private function checkFunction($function, $functionName, $desc, $runtime = 'php7 $this->assertTrue(isset($function['functionId'])); $this->assertTrue(isset($function['memorySize'])); $this->assertTrue(isset($function['timeout'])); + $this->assertTrue(isset($function['initializationTimeout'])); $serviceName = $this->serviceName; $checksum = $function['codeChecksum']; @@ -269,6 +272,7 @@ private function checkFunction($function, $functionName, $desc, $runtime = 'php7 $this->assertEquals($function['functionName'], $functionName); $this->assertEquals($function['runtime'], $runtime); $this->assertEquals($function['handler'], $handler); + $this->assertEquals($function['initializer'], $initializer); $this->assertEquals($function['description'], $desc); $code = $this->fcClient->getFunctionCode($serviceName, $functionName); @@ -285,11 +289,75 @@ private function checkFunction($function, $functionName, $desc, $runtime = 'php7 $this->assertContains('"ErrorMessage":"the resource being modified has been changed"', $err); } + public function testFunctionCRUDInitialize() { + $serviceName = $this->serviceName; + $serviceDesc = "测试的service, php sdk 创建"; + $this->fcClient->createService($serviceName, $serviceDesc); + $functionName = "test_initialize"; + + $ret = $this->fcClient->createFunction( + $serviceName, + array( + 'functionName' => $functionName, + 'handler' => 'counter.handler', + 'initializer' => 'counter.initializer', + 'runtime' => 'php7.2', + 'memorySize' => 128, + 'code' => array( + 'zipFile' => base64_encode(file_get_contents(__DIR__ . '/counter.zip')), + ), + 'description' => "test function", + 'environmentVariables' => ['TestKey' => 'TestValue'], + ) + ); + $this->checkFunction($ret, $functionName, 'test function', $runtime = 'php7.2', $handler='counter.handler', $envs=['TestKey' => 'TestValue'], $initializer='counter.initializer'); + + $invkRet = $this->fcClient->invokeFunction($serviceName, $functionName); + $this->assertEquals($invkRet['data'], '2'); + + $invkRet = $this->fcClient->invokeFunction($serviceName, $functionName, '', ['x-fc-invocation-type' => 'Async']); + $this->assertEquals($invkRet['data'], ''); + $this->assertTrue(isset($invkRet['headers']['X-Fc-Request-Id'])); + + $ret = $this->fcClient->updateFunction( + $serviceName, + $functionName, + array( + 'functionName' => $functionName, + 'handler' => 'counter.handler', + 'initializer' => 'counter.initializer', + 'runtime' => 'php7.2', + 'memorySize' => 128, + 'code' => array( + 'zipFile' => base64_encode(file_get_contents(__DIR__ . '/counter.zip')), + ), + 'description' => "test update function", + 'environmentVariables' => ['newTestKey' => 'newTestValue'], + ) + ); + $this->checkFunction($ret, $functionName, 'test update function', $runtime = 'php7.2', $handler='counter.handler', $envs=['newTestKey' => 'newTestValue'], $initializer='counter.initializer'); + $etag = $ret['headers']['Etag'][0]; + # now success with valid etag. + $this->fcClient->deleteFunction($serviceName, $functionName, $headers = ['if-match' => $etag]); + + $err = ''; + try { + $this->fcClient->getFunction($serviceName, $functionName); + } catch (Exception $e) { + $err = $e->getMessage(); + } + $this->assertTrue($err != ''); + + $this->subTestListFunctions($serviceName); + + $this->clearAllTestRes(); + } + public function testFunctionCRUDInvoke() { $serviceName = $this->serviceName; $serviceDesc = "测试的service, php sdk 创建"; $this->fcClient->createService($serviceName, $serviceDesc); - $functionName = "test"; + $functionName = "test_invoke"; $ret = $this->fcClient->createFunction( $serviceName, diff --git a/test/counter.zip b/test/counter.zip new file mode 100644 index 0000000..01215ce Binary files /dev/null and b/test/counter.zip differ