承接 switcher-io/switcher-php 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

switcher-io/switcher-php

最新稳定版本:2.1

Composer 安装命令:

composer require switcher-io/switcher-php

包简介

PHP wrapper for switcher.io API

README 文档

README

Build Status

Installation

Using composer:

$ composer require switcher-io/switcher-php

Dead Man Switch example

$urlId = 'url identifier of the switch, e.g. "abc123" in https://dmsr.io/abc123.';
$key = 'switch key';

//initialize the api
$sw = new \SwitcherIO\DeadManSwitch($urlId, $key);

//call the /start endpoint to signal your job started (optional - only used if your switch has a max run time set)
$sw->start();

//your job code goes here

//call /complete to notifiy Switcher.io the job has finished
$sw->complete();

//you can also pause the switch
$sw->pause();

Error handling

A \SwitcherIO\SwitcherException will be thrown if an error occurs, or if Switcher.io does respond that the call is ok. SwitcherException has a getStatusCode method that you should use to determine why an exception was thrown. The status code will match a constant of \SwitcherIO\DeadManSwitch.

Make sure to use error handling! If you don't, a call to $sw->start() could prevent your job from finishing.

use \SwitcherIO\DeadManSwitch;

$sw = new DeadManSwitch('url id', 'key');

try {
    $sw->start();
} catch (\SwitcherIO\SwitcherException $e) {

    if ($e->getStatusCode() === DeadManSwitch::STATUS_ERROR_404) {
        //oops, you either got the url id or key wrong...
    } else if ($e->getStatusCode() === DeadManSwitch::STATUS_ERROR_START_BEFORE_COMPLETE) {
        /*
         * You get this error if your switch is using a max runtime, and for some reason your job
         * starts a new run before the last run finished. If this is a problem for you, handle it here...
         */
    }

}

Dev environments

You probably don't want your local dev environment to ping a real switch. To make the library do dummy pings in a local or dev environment, set the url id to 'test' or 'test-error':

//in this case complete() act as if it ran succesfully, and will not actually ping a switcher.io url
$sw = new \SwitcherIO\DeadManSwitch('test', 'key-does-not-matter-for-test-url');
$sw->complete(); 

/*
 * In this case complete() will throw a \SwitcherIO\SwitcherException with a status code 
 * of \SwitcherIO\DeadManSwitch::STATUS_TEST_ERROR
 */
$sw = new \SwitcherIO\DeadManSwitch('test-error', 'key-does-not-matter-for-test-url');
$sw->complete(); 

统计信息

  • 总下载量: 2.11k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-08-29