drbe16008 2016-03-04 08:32
浏览 22
已采纳

计算当前时间是否是终止我的EC2实例的好时机?

Amazon bills you for the full hour of EC2 usage even if you have only run the instance for 10 minutes. So before terminating any unused EC2 instances I want to check if the launch time - current time is under 10 minutes.

Let me explain this to you by an example:

Let's say I launch my EC2 instance at: 2016-Feb-01 13:02:22. Then I would like to terminate it only when the time is 10 minutes from the launch time (regardless of the date). So if it is 16:58:54 then it means that current time is a good time to terminate it because I will be billed at 17:02:22 again (in about 4 minutes as per this example)? On the other hand if the time is 13:03:01 then it is not a good time since I'm already billed for the hour.

Here is what I could come up with, but it's not working when the current time is 58 and launch time is 02, etc

<?php

$launch = '2016-03-03 13:02:22';
$now = '2016-03-04 12:58:22';

$epochLaunch = strtotime(sprintf('1970-01-01 00:%02d:00', date('i', strtotime($launch))));
$epochNow = strtotime(sprintf('1970-01-01 00:%02d:00', date('i', strtotime($now))));

echo $epochLaunch - $epochNow;
  • 写回答

1条回答 默认 最新

  • dongyu8664 2016-03-04 09:00
    关注

    What you need is something like this:

    function shouldTerminate($launch, $minsToTerminate, $current = null)
    {
        if (!is_null($current)) {
            $current = strtotime($current);
        }
        else {
            $current = time();
        }
        $launchEpoch = strtotime($launch);  
    
        return (($current - $launchEpoch) % 3600) > (3600 - $minsToTerminate * 60);
    }
    
    echo shouldTerminate('2016-03-03 13:02:22', 4, '2016-03-03 13:50:25') ? 'Terminate': 'Do not terminate', PHP_EOL;
    echo shouldTerminate('2016-03-03 13:02:22', 4, '2016-03-03 13:59:25') ? 'Terminate': 'Do not terminate', PHP_EOL;
    echo shouldTerminate('2016-03-03 13:02:22', 4, '2016-03-05 13:58:25') ? 'Terminate': 'Do not terminate', PHP_EOL;
    echo shouldTerminate('2016-03-03 13:02:22', 4, '2016-03-05 13:03:25') ? 'Terminate': 'Do not terminate', PHP_EOL;
    echo shouldTerminate('2016-03-03 13:02:22', 4, '2016-03-05 13:51:25') ? 'Terminate': 'Do not terminate', PHP_EOL;
    

    The output of the above will be:

    Do not terminate
    Terminate
    Terminate
    Do not terminate
    Do not terminate
    

    USAGE: The shouldTerminate() function is straightforward to use, supply it with the string of launch time, the minutes before the whole hour to allow for termination and an optional datetime string parameter to test against - or the function will use the current time if you do not supply any.

    See the code live at https://eval.in/530108

    EXPLANATION: The idea is that you are interested in the remainder of minutes in a whole hour. That is why you get the remainder of seconds in an hour from the difference between the two epoch time, i.e.

    $current - $launchEPoch % 3600
    

    The above will give you how many seconds that do not fit in a whole hour (remaining seconds) they have passed and when those seconds are greater than the remaining hour minus the threshold you have set, i.e. > 3600 - $minsToTerminate * 60 then you can terminate the instance according to your rule.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。