Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHPでもserverless framework!?

B51ca7a51ae1fd06bc536fe83e6113e2?s=47 Kaz Watanabe
October 09, 2017

PHPでもserverless framework!?

B51ca7a51ae1fd06bc536fe83e6113e2?s=128

Kaz Watanabe

October 09, 2017
Tweet

Transcript

  1. 1)1Ͱ΋TFSWFSMFTT GSBNFXPSL 1)1ΧϯϑΝϨϯε50,:0 ߹ಉձࣾEFDS୅දࣾһ ౉ลҰ޺ !LB[@

  2. WHO!? ౉ลҰ޺(@kaz_29) ߹ಉձࣾ decr ୅දࣾһ WebΞϓϦέʔγϣϯͷ։ൃ iOSΞϓϦέʔγϣϯͷ։ൃ Ϋϥ΢υΠϯϑϥͷߏஙɾӡ༻

  3. ຊ೔ͷ಺༰ w αʔόʔϨεΞʔΩςΫνϟʔ w "[VSF'VODUJPOTͷ֓ཁ w 1)1Ͱ"[VSF'VODUJPOT w 4FSWFSMFTT'SBNFXPSL

  4. αʔόʔϨεΞʔΩςΫνϟʔ

  5. None
  6. None
  7. r4FSWFSMFTT$POG50,:0
 IUUQUPLZPTFSWFSMFTTDPOGJP lαʔόʔϨεΞʔΩςΫνϟ͸ɺ։ൃऀͷ૑଄ੑΛ ૉૣ͘8FC΍ϞόΠϧΞϓϦέʔγϣϯͱ࣮ͯ͠ݱ ͠ɺεέʔϥϏϦςΟ΍ηΩϡϦςΟɺΠϯϑϥͷอ कͱ͍ͬͨଟ਺ͷྗ࢓ࣄ͔Βղ์͞ΕΔͨΊͷ৽ͨͳ ύϥμΠϜγϑτͰ͢ɻz

  8. αʔόʔ؀ڥͷมԽ w Ϋϥ΢υͷొ৔Ͱॊೈʹੜ੒ɾഁغ͕Ͱ͖Δ༷ʹͳͬͨɻ w ελϯυΞϩϯΞϓϦ͔Βͷ୤٫ϚΠΫϩαʔϏεͷོ੝ w αʔόʔ͕ͳ͘ͳΔͱ͍͏͜ͱͰ͸ͳ͍ɻ։ൃऀ͕αʔόʔʹ͍ͭͯ୔ࢁͷ ͜ͱΛߟ͑ͳͯ͘ྑ͘ͳΔͱ͍͏͜ͱɻ IUUQTSFBEXSJUFDPNXIZUIFGVUVSFPGTPGUXBSFBOEBQQTJTTFSWFSMFTT

  9. αʔόʔϨεͱ͸ʁ w αʔόʔΛҙࣝ͠ͳ͍Ͱྑ͍ w ؆୯ʹεέʔϧͰ͖Δ w ར༻ྔʹΑΔ՝ۚ

  10. Α͘ޠΒΕΔϢʔεέʔε w 8FCΞϓϦέʔγϣϯ w ϞόΠϧόοΫΤϯυ w *P5όοΫΤϯυ w ετϦʔϜϓϩηογϯά΍&5- w

    νϟοτϘοτɺίάχςΟϒαʔϏε w όονॲཧ w ӡ༻ͷࣗಈԽ
  11. ࣄྫ೔ܦ৽ฉ IUUQTBXTBNB[PODPNKQTPMVUJPOTDBTFTUVEJFTOJLLFJ

  12. None
  13. ࣄྫGBVMUMJOF IUUQLMPXIBUFOBCMPHDPNFOUSZ 1)1ΧϯϑΝϨϯε෱ԬͰGBVMUMJOFʹ͍ͭͯൃද͖ͯ͠·ͨ͠QIQDPOGVL

  14. Α͘ޠΒΕΔϢʔεέʔε w 8FCΞϓϦέʔγϣϯ w ϞόΠϧόοΫΤϯυ w *P5όοΫΤϯυ w ετϦʔϜϓϩηογϯά΍&5- w

    νϟοτϘοτɺίάχςΟϒαʔϏε w όονॲཧ w ӡ༻ͷࣗಈԽ ࠷ॳʹࢼ͢ͷʹΦεεϝʂ
  15. Lambda

  16. rIUUQEPDTBXTBNB[PODPNKB@KQMBNCEBMBUFTUEHXFMDPNFIUNM lඞཁͳૢ࡞͸ɺ"84-BNCEB͕αϙʔτ͢Δ͍ͣ Ε͔ͷݴޠ ݱࡏ͸/PEFKTɺ+BWBɺ$͓Αͼ 1ZUIPO ͰίʔυΛࢦఆ͢Δ͚ͩͰ͢ɻ z

  17. lඞཁͳૢ࡞͸ɺ"84-BNCEB͕αϙʔτ͢Δ͍ͣ Ε͔ͷݴޠ ݱࡏ͸/PEFKTɺ+BWBɺ $͓Αͼ1ZUIPO ͰίʔυΛࢦఆ͢Δͩ ͚Ͱ͢ɻ z "84-BNCEB rIUUQEPDTBXTBNB[PODPNKB@KQMBNCEBMBUFTUEHXFMDPNFIUNM ͸ͳ͍

  18. "[VSF'VODUJPOT

  19. rIUUQTEPDTNJDSPTPGUDPNKBKQB[VSFB[VSFGVODUJPOTGVODUJPOTPWFSWJFX l$ɺ'ɺ/PEFKTɺ1ZUIPOɺ1)1ɺ#BUDIɺ #BTIɺͦͷଞ࣮ߦՄೳͳݴޠΛ࢖ͬͯؔ਺Λهड़Ͱ ͖·͢ɻz Ͱॻ͚Δ

  20. "[VSF'VODUJPO

  21. "[VSF'VODUJPOT w ΊͬͪΌ͍҆ w ೖྗɾग़ྗͷ࿈ܞػೳ͕๛෋ τϦΨʔɾόΠϯσΟϯά  w 7474$PEFΛ࢖ͬͯͷσόοά ϩʔΧϧɾϦϞʔτʣ

    w ΦϯϓϨͰ΋͍͚Δ "[VSF4UBDL  w ϥϯλΠϜ͕Φʔϓϯ IUUQTHJUIVCDPN"[VSF"[VSF'VODUJPOT  w (JUIVC͔ΒσϓϩΠͰ͖Δ
  22. τϦΨʔͱόΠϯσΟϯά IUUQTEPDTNJDSPTPGUDPNKBKQB[VSFB[VSFGVODUJPOTGVODUJPOTUSJHHFSTCJOEJOHT

  23. -PHJD"QQT

  24. -PHJD"QQ

  25. .JDSPTPGU'MPX IUUQTqPXNJDSPTPGUDPN

  26. "[VSF'VODUJPOͷػೳ

  27. "[VSF'VODUJPO

  28. Ϟχλʔ

  29. 'VODUJPO1SPYZ QSFWJFX w ෳ਺ͷ'VODUJPOΛ౷߹Ͱ͖Δ w "1*.BOBHFNFOUͷ୅ΘΓʹ࢖͑Δ w 'VODUJPOҎ֎΁ͷϦΫΤετ΋ઃఆͰ͖Δ "[VSF"1*NBOBHFNFOU "[VSF'VODUJPOT

    "[VSF'VODUJPOT1SPYZ "[VSF'VODUJPOT ͪΐͬͱ ͓ߴ͍
  30. σϓϩΠϝϯτεϩοτ QSFWJFX #MVF(SFFO%FQMPZNFOU͕؆୯ʹͰ͖Δ

  31. $POTPMF

  32. "QQMJDBUJPO*OTJUF QSFWJFX ೖΕΔ͔ʜ IUUQTB[VSFNJDSPTPGUDPNKBKQVQEBUFTB[VSFGVODUJPOTOPXJOUFHSBUFEXJUIBQQMJDBUJPOJOTJHIUT

  33. %VSBCMF'VODUJPOT Ћ൛ w ෳ਺ͷ'VODUJPOͷ࿈ܞΛൺֱత؆୯ʹ࣮૷Ͱ͖Δ w 'VODUJPODIBJOJOH w 'BOPVU'BOJO w "TZOD)551"1*T

    IUUQTB[VSFHJUIVCJPB[VSFGVODUJPOTEVSBCMFFYUFOTJPOJOEFYIUNM IUUQRJJUBDPN5TVZPTIJ6TIJP!HJUIVCJUFNTFBDCCCGEC
  34. 'VODUJPO$IBJOJOH public static async Task<object> Run(DurableOrchestrationContext ctx) { try {

    var x = await ctx.CallFunctionAsync<object>("F1"); var y = await ctx.CallFunctionAsync<object>("F2", x); var z = await ctx.CallFunctionAsync<object>("F3", y); return await ctx.CallFunctionAsync<object>("F4", z); } catch (Exception) { // error handling/compensation goes here } }
  35. public static async Task Run(DurableOrchestrationContext ctx) { var parallelTasks =

    new List<Task<int>>(); // get a list of N work items to process in parallel object[] workBatch = await ctx.CallFunctionAsync<object[]>("F1"); for (int i = 0; i < workBatch.Length; i++) { Task<int> task = ctx.CallFunctionAsync<int>("F2", workBatch[i]); parallelTasks.Add(task); } await Task.WhenAll(parallelTasks); // aggregate all N outputs and send result to F3 int sum = parallelTasks.Sum(t => t.Result); await ctx.CallFunctionAsync("F3", sum); } 'BOPVU 'BOJO
  36. "TZOD)551"1*T public static async Task<HttpResponseMessage> Run( HttpRequestMessage req, DurableOrchestrationClient starter,

    string functionName, TraceWriter log) { // Function name comes from the request URL. // Function input comes from the request content. dynamic eventData = await req.Content.ReadAsAsync<object>(); string instanceId = await starter.StartNewAsync(functionName, eventData); log.Info($"Started orchestration with ID = '{instanceId}'."); return starter.CreateCheckStatusResponse(req, instanceId); }
  37. -JHIUXFJHIU"DUPST public static async Task Run(DurableOrchestrationContext ctx) { int counterState

    = ctx.GetInput<int>(); string operation = await ctx.WaitForExternalEvent<string>("operation"); if (operation == "incr") { counterState++; } else if (operation == "decr") { counterState--; } ctx.ContinueAsNew(counterState); }
  38. )VNBO*OUFSBDUJPOBOE5JNFPVUT public static async Task Run(DurableOrchestrationContext ctx) { await ctx.CallFunctionAsync("RequestApproval");

    using (var timeoutCts = new CancellationTokenSource()) { DateTime dueTime = ctx.CurrentUtcDateTime.AddHours(72); Task durableTimeout = ctx.CreateTimer(dueTime, timeoutCts.Token); Task<bool> approvalEvent = ctx.WaitForExternalEvent<bool>("ApprovalEvent"); if (approvalEvent == await Task.WhenAny(approvalEvent, durableTimeout)) { timeoutCts.Cancel(); await ctx.CallFunctionAsync("HandleApproval", approvalEvent.Result); } else { await ctx.CallFunctionAsync("Escalate"); } } }
  39. -PDBM։ൃ؀ڥ w 7JTVBM4UVEJP74$PEFΛ࢖ͬͯϩʔΧϧ։ൃ͕Մೳ w 7FSTJPO /&5'SBNFXPSLΛར༻ ͔Β͸ɺ8JOEPXT Ҏ֎΋αϙʔτ

  40. -PDBM։ൃ؀ڥ IUUQLB[IBUFOBCMPHDPNFOUSZ

  41. 1)1Ͱ"[VSF'VODUJPOT

  42. όΠϯσΟϯάͷ࢖͍ํ <?php // ೖྗόΠϯσΟϯάͷಡࠐΈ $data = file_get_contents(getenv(‘req’)); // ग़ྗόΠϯσΟϯά΁ͷॻࠐΈ file_put_contents(getenv(‘res’),

    $data); <?php var_dump(getenv('res')); string(72) “D: \local\Temp\Functions\Binding\[UUID]\res” ੲջ͔͍͠$(*෩
  43. "[VSF1PSUBMͰ࡞੒

  44. 1PSUBMͰ௚઀ 1SPEVDUJPO ίʔυमਖ਼⁉

  45. None
  46. 6OJU5FTUͷ αϯϓϧ

  47. 0VUHPJOH8FCIPPLT IUUQ5SJHHFS IUUQPVUCJOEJOH

  48. 'VODUJPOͷߏ੒  ᵓᴷᴷIPTUKTPO ᵓᴷᴷGVOD ᴹᵓᴷᴷGVODUJPOKTPO ᴹᵋᴷᴷSVOQIQ ᵋᴷᴷGVOD ᵓᴷᴷGVODUJPOKTPO ᵋᴷᴷSVOQIQ EJSFDUPSJFT

    pMFT 'VODUJPOຖʹσΟϨΫτϦ τϦΨʔɾόΠϯσΟϯάͳͲͷఆٛ 'VODUJPOͷΤϯτϦϙΠϯτ ϥϯλΠϜݻ༗ͷߏ੒ IUUQTEPDTNJDSPTPGUDPNKBKQB[VSFB[VSFGVODUJPOTGVODUJPOTUSJHHFSTCJOEJOHT
  49. αϯϓϧ'VODUJPOͷߏ੒ . ├── composer.json ├── composer.lock ├── slackbot │ ├──

    function.json │ └── run.php ├── src │ ├── Functions │ │ └── SlackBot.php │ └── Lib │ └── AzureFunction.php ├── tests │ ├── Lib │ │ └── TestCase.php │ └── TestCase │ └── Functions │ └── SlackBotTest.php ├── tmp └── vendor ├── autoload.php … 'VODUJPOͷσΟϨΫτϦ τϦΨʔɾόΠϯσΟϯάͳͲͷఆٛ 'VODUJPOͷΤϯτϦϙΠϯτ 'VODUJPOຊମ ؆୯ͳϥούʔΫϥε ςετΫϥε IUUQTHJUIVCDPNLB[QIQDPOLBOTBJB[VSFGVODUJPO
  50. GVODUJPOKTPO { "bindings": [ { "type": "http", "direction": "out", "name":

    "res" }, { "type": "httpTrigger", "name": "req", "authLevel": "function", "methods": [ "post" ], "direction": "in" } ], "disabled": false }
  51. SVOQIQ <?php require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR

    . 'autoload.php'; use App\Functions\SlackBot; $proc = new SlackBot(); $proc->run();
  52. 4MBDL#PUQIQ <?php namespace App\Functions; use App\Lib\AzureFunction; class SlackBot extends AzureFunction

    { public function __construct($req = 'req', $res = 'res') { $this->init(getenv($req), getenv($res)); } … }
  53. 4MBDL#PUQIQ public function run() { $params = $this->httpParams(); if (!isset($params['user_name'])

    || $params['user_name'] === 'slackbot') { return; } $response = [ 'text' => '', 'username' => 'phpconkansai2017demo', 'icon_emoji' => ':phpkansai:', 'attachments' => [[ "color" => '#00BFFF', // blue "title" => 'ϝοηʔδΛड৴͠·ͨ͠', "fields" => [[ … ]] ]] ]; $this->writeResponse(json_encode($response)); }
  54. 4-BDL#PU5FTUQIQ /** * @test */ public function ௨ৗͷॻ͖ࠐΈͷςετ() { $params

    = [ 'token' => 'DUMMY', 'user_name' => 'userA', 'text' => 'Hello Azure Function' ]; $this->makeHttpRequest($params); $this->makeResponse(); …
  55. 4-BDL#PU5FTUQIQ $this->slackBot = new SlackBot(); $this->slackBot->run(); $result = json_decode($this->getResponse(), true);

    $expected = [ 'username' => 'phpconkansai2017demo', 'icon_emoji' => ':phpkansai:', 'text' => '', 'attachments' => [[ "color" => '#00BFFF', "title" => 'ϝοηʔδΛड৴͠·ͨ͠', "fields" => [[ // লུ ]] ]] ]; $this->assertEquals($expected, $result); }
  56. 1)16OJUͰςετ

  57. 4FSWFSMFTT'SBNFXPSL

  58. IUUQTTFSWFSMFTTDPNGSBNFXPSL

  59. rIUUQTTFSWFSMFTTDPNGSBNFXPSL l4FSWFSMFTT'SBNFXPSLJTZPVSTJOHMFUPPMLJU GPSEFQMPZJOHTFSWFSMFTTBSDIJUFDUVSFTUP BOZQSPWJEFS:PVCVJMEUIFGFBUVSFT XF DPOpHVSFUIFJOGSBTUSVDUVSF%POFz

  60. rIUUQTTFSWFSMFTTDPNGSBNFXPSL l4FSWFSMFTT'SBNFXPSLJTZPVSTJOHMFUPPMLJU GPSEFQMPZJOHTFSWFSMFTTBSDIJUFDUVSFTUP BOZQSPWJEFS:PVCVJMEUIFGFBUVSFT XF DPOpHVSFUIFJOGSBTUSVDUVSF%POFz

  61. 4FSWFSMFTT'SBNFXPSL w OPEFKT੡ w "84-BNCEB w "[VSF'VODUJPOTରԠ w ͨͩ͠OPEFKTͷΈʜ

  62. "[VSFͰࢼͯ͠ΈΔ ϑΝΠϧΛݻΊΔ "[VSFʹϩάΠϯ GVODUJPO࡞Δ CJOEJOHͷઃఆ GVODUJPOΛݻΊΔ VQMPBE QBDLBHFKTPOΛ্͛Δ OQNJOTUBMMΛ࣮ߦ

  63. None
  64. "[VSFͰࢼͯ͠ΈΔ ϑΝΠϧΛݻΊΔ "[VSFʹϩάΠϯ GVODUJPO࡞Δ CJOEJOHͷઃఆ GVODUJPOΛݻΊΔ VQMPBE QBDLBHFKTPOΛ্͛Δ OQNJOTUBMMΛ࣮ߦ ͜ΕΛʜ

  65. "[VSFͰࢼͯ͠ΈΔ ϑΝΠϧΛݻΊΔ "[VSFʹϩάΠϯ GVODUJPO࡞Δ CJOEJOHͷઃఆ GVODUJPOΛݻΊΔ VQMPBE QBDLBHFKTPOΛ্͛Δ OQNJOTUBMMΛ࣮ߦ DPNQPTFSKTPOΛ্͛Δ

    DPNQPTFSJOTUBMMΛ࣮ߦ ͜͏͢Δʜ
  66. Ͱ͖ͨ‼

  67. Ͱ͖ͨ‼ DVSM)$POUFOU5ZQFBQQMJDBUJPOKTPOE\aOBNFaa,B[a^IUUQTTMT QIQFYBNQMFB[VSFXFCTJUFTOFUBQJIUUQQIQ )FMMP,B[

  68. Ͱ͖ͨ‼ TMTMPHTGIUUQQIQ

  69. 1)1Ͱ΋ 4FSWFSMFTT'SBNFXPSL ࢖͑ͦ͏ IUUQTHJUIVCDPNLB[TFSWFSMFTTB[VSFQIQFYBNQMF

  70. ՝୊ w ࠷৽ͷTFSWFSMFTTB[VSFGVODUJPOTΛऔΓࠐΉ w B[VSFQIQςϯϓϨʔτΛ࡞Δ w QSPYZ΍TMPUͷૢ࡞ΛTMT͔Β΍Γ͍ͨ

  71. "[VSF'VODUJPO݁ߏ͍͍Ͱ͢ IUUQTGVODUJPOTB[VSFDPNUSZ

  72. ͓ΘΓ