每天按时间排列web动画的日程安排数 [英] schedule number of web dynos by time of day

查看:107
本文介绍了每天按时间排列web动画的日程安排数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用Heroku调度程序来启动和停止web dynos在一天的特定时期?就像在营业时间里说的2个dynos和晚上只有1个dyno?



我真的希望避免将普通的用户/通行证件放入应用程序本身, m寻求一个安全的方式来做到这一点(除了每天为每个应用程序手动)。直接使用heroku ps:scale web = 2自然会很好,但据我所知,这是不受支持的。



感谢任何反馈意见。

解决方案

您可以通过创建使用Heroku API的脚本来在时间表上扩展heroku dynos。然后,在Procfile中输入一个条目,并通过Heroku Scheduler加载项调用它。以下是如何:



首先,您需要将herokupython模块添加到 requirements.txt 中:


heroku == 0.1.2


接下来,创建一个包含你的API密钥的config var,所以你的脚本可以使用API​​。


heroku config:add HEROKU_API_KEY = your_api_key_string


您可以找到您的API密钥



瞧瞧!你的动作现在将在一天中的指定时间向上或向下扩展。



-



注意一次您已经在Scheduler网页上创建了一个计划任务,无法编辑它运行的时间,但如果创建一个新任务,您可以设置时间段,然后删除旧任务。



注意2:如果您尝试缩小到0个dynos,或者如果您尝试扩展,如果0 web dynos目前存在,那么heroku python API似乎会抛出一个KeyError 。为了避免这两种情况,只需不要缩小到0 dynos。


Is there a way to use the Heroku scheduler to start and stop web dynos for specific periods of the day? Like say during business hours 2 dynos and at night only 1 dyno?

I really would like to avoid putting the normal user/pass credentials into the app itself, so I'm looking for a secure way to do this (apart from doing it manually each day for each app). Using the "heroku ps:scale web=2" directly would naturally be nice but as far as I know this is not supported.

Thanks for any feedback in advance...

解决方案

You can scale heroku dynos on a schedule by creating a script that uses the Heroku API. You then make an entry in your Procfile and call it via the Heroku Scheduler add-on. Here's how:

First you'll need to add the 'heroku' python module to your requirements.txt:

heroku==0.1.2

Next, create a config var that contains your API key, so your script can use the API.

heroku config:add HEROKU_API_KEY=your_api_key_string

You can find your API key on your heroku account page.

Now you'll be able to write a python script that scales your dynos. Here's a very basic script that accepts the number of dynos as a command-line argument.

import os
import sys

import heroku

"""Scale heroku web processes using the heroku python API."""

# you may want to add better argument processing, use argparse, etc.
dynos = int(sys.argv[1])
cloud = heroku.from_key(os.environ.get('HEROKU_API_KEY'))
app = cloud.apps['your_app_name']

try:
    # you may want to add a maximum dyno check here to prevent costly mistakes ;)
    webproc = app.processes['web']  
    webproc.scale(dynos)

except KeyError:
    # note: scaling to 0 dynos or attempting to scale up if 0 web dynos exist
    # both throw this error. Make sure you have at least one dyno.
    print >> sys.stderr, "Could not scale web processes - are there 0 web dynos running?"

Then you can either define your entire task inside the Heroku Scheduler web page, or define it inside your Procfile and call the Procfile process name from the web page. I prefer the latter, as it makes it easy to update or change the process without having to log in to heroku's website.

So, create entries in your Procfile:

scale_up: python scale.py 2
scale_down: python scale.py 1

And then schedule them:

And voila! Your dynos will now scale up or down at the specified time of day.

--

Note that once you have created a scheduled task on the Scheduler web page you can't edit the time of day it runs, but if you create a new task you can set the time day and then delete your old task.

Note 2: The heroku python API seems to throw a KeyError if you try to scale down to 0 dynos, or if you try to scale up if 0 web dynos currently exist. To avoid both, just don't scale down to 0 dynos.

这篇关于每天按时间排列web动画的日程安排数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆