azure_Windows Azure WebJobs简介

azure

azure

I'm currently running 16 web sites on Windows Azure. I have a few Virtual Machines, but I prefer to run things using "platform as a service" where I don't have to sweat the underlying Virtual Machine. That means, while I know I can run a Virtual Machine and put "cron" jobs on it, I'm less likely to because I don't want to mess with VMs or Worker Roles.

我目前在Windows Azure上运行16个网站。 我有一些虚拟机,但是我更喜欢使用“平台即服务”来运行东西,而不必费心底层的虚拟机。 这意味着,尽管我知道我可以运行虚拟机并在其上放置“ cron”作业,但我不太可能这样做,因为我不想惹恼VM或Worker Roles。

There are a few ways to run stuff on Azure, first, there's IAAS (Infrastructure as a Service) which is VMs. Then there's Cloud Applications (Cloud Services) where you can run anything in an Azure-managed VM. It's still a VM, but you have a lot of choice and can run Worker Roles and background stuff. However, there's a lot of ceremony if you just want to run your small "job" either on a regular basis or via a trigger.

有几种方法可以在Azure上运行内容,首先是IAAS(基础架构即服务)即VM。 然后是云应用程序(云服务),您可以在其中运行任何由Azure管理的VM。 它仍然是VM,但是您有很多选择,并且可以运行Worker Roles和背景材料。 但是,如果您只是想定期或通过触发器运行小的“作业”,则有很多仪式。

Azure Explained in one Image

Looking at this differently, platform as a service is like having your hotel room fixed up daily, while VMs is more like managing a house yourself.

从另一角度来看,平台即服务就像每天固定酒店房间,而虚拟机更像是自己管理房屋。

Azure Explained in one Image

As someone who likes to torch a hotel room as much as the next person, this is why I like Azure Web Sites (PAAS). You just deploy, and it's done. The VM is invisible and the site is always up.

正如喜欢在下一个房间中抢劫旅馆房间一样多的人,这就是为什么我喜欢Azure网站(PAAS)。 您只需要部署就可以了。 虚拟机不可见,站点始终处于运行状态。

However, there's not yet been a good solution under web sites for doing regular jobs and batch work in the background. Now Azure Web Sites support a thing  called "Azure WebJobs" to solve this problem simply.

但是,网站下还没有一个好的解决方案,可以在后台进行常规工作和批处理工作。 现在,Azure网站支持一种名为“ Azure WebJobs ”的东西,可以简单地解决此问题。

使用Azure WebJob扩展命令行应用程序 (Scaling a Command Line application with Azure WebJobs)

When I want to do something simple - like resize some images - I'll either write a script or a small .NET application. Things do get complex though when you want to take something simple and do it n times. Scaling a command line app to the cloud often involves a lot of yak shaving.

当我想做一些简单的事情(如调整一些图像的大小)时,我将编写脚本或小型.NET应用程序。 事情确实变得复杂,但是当您想做简单的事情并进行n次时。 将命令行应用程序扩展到云通常会涉及很多牛工作

Let's say I want to take this function that works fine at the command line and run it in the cloud at scale.

假设我想使用此功能,该功能在命令行上可以正常运行,并在云中大规模运行它。

public static void SquishNewlyUploadedPNGs(Stream input, Stream output)
{
var quantizer = new WuQuantizer();
using (var bitmap = new Bitmap(input))
{
using (var quantized = quantizer.QuantizeImage(bitmap))
{
quantized.Save(output, ImageFormat.Png);
}
}
}

WebJobs aims to make developing, running, and scaling this easier. They are built into Azure Websites and run in the same VM as your Web Sites.

WebJobs旨在简化开发,运行和扩展。 它们内置于Azure网站中,并在与您的网站相同的VM中运行。

Here's some typical scenarios that would be great for the Windows Azure WebJobs SDK:

以下是一些对Windows Azure WebJobs SDK有用的典型方案:

  • Image processing or other CPU-intensive work.

    图像处理或其他占用大量CPU的工作。
  • Queue processing.

    队列处理。
  • RSS aggregation.

    RSS聚合。
  • File maintenance, such as aggregating or cleaning up log files. 

    文件维护,例如聚合或清理日志文件。
  • Other long-running tasks that you want to run in a background thread, such as sending emails.

    您想要在后台线程中运行的其他长时间运行的任务,例如发送电子邮件。

WebJobs are invoked in two different ways, either they are triggered or they are continuously running. Triggered jobs happen on a schedule or when some event happens and Continuous jobs basically run a while loop.

WebJobs可以通过两种不同的方式调用,即触发它们还是连续运行。 触发的作业按计划执行或在某些事件发生时发生,而连续作业基本上运行一段时间循环。

WebJobs are deployed by copying them to the right place in the file-system (or using a designated API which will do the same). The following file types are accepted as runnable scripts that can be used as a job:

通过将WebJob复制到文件系统中的正确位置(或使用将执行相同操作的指定API)来进行部署。 可接受以下文件类型作为可以用作作业的可运行脚本:

  • .exe - .NET assemblies compiled with the WebJobs SDK

    .exe-使用WebJobs SDK编译的.NET程序集
  • .cmd, .bat, .exe (using windows cmd)

    .cmd,.bat,.exe(使用Windows cmd)
  • .sh (using bash)

    .sh(使用bash)
  • .php (using php)

    .php(使用php)
  • .py (using python)

    .py(使用python)
  • .js (using node)

    .js(使用节点)

After you deploy your WebJobs from the portal, you can start and stop jobs, delete them, upload jobs as ZIP files, etc. You've got full control.

从门户部署WebJob之后,您可以启动和停止作业,删除作业,将作业上传为ZIP文件等。您拥有完全的控制权。

A good thing to point out, though, is that Azure WebJobs are more than just scheduled scripts, you can also create WebJobs as .NET projects written in C# or whatever.

值得指出的是,Azure WebJobs不仅仅是计划的脚本,您还可以将WebJobs创建为用C#或其他语言编写的.NET项目

使用Windows Azure WebJobs SDK从命令行应用程序制作WebJob (Making a WebJob out of a command line app with the Windows Azure WebJobs SDK)

WebJobs can effectively take some command line C# application with a function and turn it into a scalable WebJob. I spoke about this over the last few years in presentations when it was codenamed "SimpleBatch." This lets you write a simple console app to, say, resize an image, then move it up to the cloud and resize millions. Jobs can be triggered by the appearance of new items on an Azure Queue, or by new binary Blobs showing up in Azure Storage.

WebJobs可以有效地使用带有功能的某些命令行C#应用程序并将其转变为可伸缩的WebJob。 在过去的几年中,我在代号为“ SimpleBatch ”的演示文稿中谈到了这一点。 这样,您就可以编写一个简单的控制台应用程序,例如调整图像的大小,然后将其移至云中并调整数百万的大小。 可以通过在Azure队列中出现新项目或通过在Azure存储中显示新的二进制Blob来触发作业。

NOTE: You don't have to use the WebJobs SDK with the WebJobs feature of Windows Azure Web Sites. As noted earlier, the WebJobs feature enables you to upload and run any executable or script, whether or not it uses the WebJobs SDK framework.

注意:您不必将WebJobs SDK与Windows Azure网站的WebJobs功能一起使用。 如前所述,无论是否使用WebJobs SDK框架,WebJobs功能都使您能够上载和运行任何可执行文件或脚本。

I wanted to make a Web Job that would losslessly squish PNGs as I upload them to Azure storage. When new PNGs show up, the job should automatically run on these new PNGs. This is easy as a Command Line app using the nQuant open source library as in the code above.

我想制作一个Web作业,将PNG上传到Azure存储时可以无损地压缩PNG。 当出现新的PNG时,作业应自动在这些新的PNG上运行。 作为使用nQuant开源库的命令行应用程序,这很容易,如上面的代码所示。

Now I'll add the WebJobs SDK NuGet package (it's prerelease) and Microsoft.WindowsAzure.Jobs namespace, then add [BlobInput] and [BlobOutput] attributes, then start the JobHost() from Main. That's it.

现在,我将添加WebJobs SDK NuGet包(预发行版)和Microsoft.WindowsAzure.Jobs命名空间,然后添加[BlobInput]和[BlobOutput]属性,然后从Main启动JobHost()。 而已。

using Microsoft.WindowsAzure.Jobs;
using nQuant;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
JobHost host = new JobHost();
host.RunAndBlock();
}

public static void SquishNewlyUploadedPNGs(
[BlobInput("input/{name}")] Stream input,
[BlobOutput("output/{name}")] Stream output)
{
var quantizer = new WuQuantizer();
using (var bitmap = new Bitmap(input))
{
using (var quantized = quantizer.QuantizeImage(bitmap))
{
quantized.Save(output, ImageFormat.Png);
}
}

}
}
}

CONTEXT: Let's just step back and process this for a second. All I had to do was spin up the JobHost and set up a few attributes. Minimal ceremony for maximum results. My console app is now processing information from Azure blob storage without ever referencing the Azure Blob Storage API!

上下文:让我们退后一步,再处理一秒钟。 我要做的就是旋转JobHost并设置一些属性。 最少的仪式,以取得最大的效果。 我的控制台应用程序现在正在处理Azure Blob存储中的信息,而无需引用Azure Blob存储API!

The function is automatically called when a new blob (in my case, a new PNG) shows up in the input container in storage and the Stream parameters are automatically "bound" (like Model Binding) for me by the WebJobs SDK.

当存储库中的输入容器中出现新的Blob(在我的情况下为新的PNG),并且WebJobs SDK为我自动“绑定”了Stream参数(例如模型绑定)时,将自动调用该函数。

To deploy, I zip up my app and upload it from the WebJobs section of my existing Azure Website in the Portal.

要进行部署,我压缩了我的应用程序,然后从门户中现有Azure网站的WebJobs部分上载了它。

image

Here it is in the Portal.

它在门户中。

image

I'm setting mine to continuous, but it can also run on a detailed schedule:

我将我的设置为连续,但也可以按详细的时间表运行:

12schdmonthsonpartweekdaysoccurences

I need my WebJob to be told about what Azure Storage account it's going to use, so from my Azure Web Site under the Connection Strings section I set up two strings, one for the AzureJobsRuntime (for logging) and one for AzureJobsData (what I'm accessing). 

我需要告知WebJob它将使用哪个Azure存储帐户,因此从我的Azure网站的“连接字符串”部分下,我设置了两个字符串,一个用于AzureJobsRuntime(用于日志记录),另一个用于AzureJobsData(我要m访问)。

image

For what I'm doing they are the same. The connection strings look like this:

对于我正在做的事情,它们是相同的。 连接字符串如下所示:

DefaultEndpointsProtocol=https;AccountName=hanselstorage;AccountKey=3exLzmagickey

The key here came from Manage Access Keys in my storage account, here:

此处的密钥来自我存储帐户中的“管理访问密钥”:

image

In my "Hanselstorage" Storage Container I made two areas, input and output. You can name yours whatever. You can also process from Queues, etc.

在“ Hanselstorage”存储容器中,我做了两个区域,输入和输出。 您可以随便命名。 您还可以从队列等进行处理。

image

Now, going back to the code, look at the parameters to the Attributes I'm using:

现在,回到代码,查看我正在使用的属性的参数:

public static void SquishNewlyUploadedPNGs(           
[BlobInput("input/{name}")] Stream input,
[BlobOutput("output/{name}")] Stream output)

There's the strings "input" and "output" pointing to specific containers in my Storage account. Again, the actual storage account (Hanselstorage) is part of the connection string. That lets you reuse WebJobs in multiple sites, just by changing the connection strings.

在我的存储帐户中有字符串“ input”和“ output”指向特定的容器。 同样,实际的存储帐户(Hanselstorage)是连接字符串的一部分。 这样,您只需更改连接字符串即可在多个站点中重用WebJob。

There is a link to get to the Azure Web Jobs Dashboard to the right of your job, but the format for the URL to access is this: https://YOURSITE.scm.azurewebsites.net/azurejobs. You'll need to enter your same credentials you've used for Azure deployment.

可以在您的作业右侧找到一个指向Azure Web Jobs仪表板的链接,但是要访问的URL格式为: https : //YOURSITE.scm.azurewebsites.net/azurejobs 。 您需要输入用于Azure部署的相同凭据。

Once you've uploaded your job, you'll see the registered function(s) here:

上传工作后,您将在此处看到已注册的功能:

image

I've installed the Azure SDK and can access my storage live within Visual Studio. You can also try 3rd party apps like Cloudberry Explorer. Here I've uploaded a file called scottha.png into the input container.

我已经安装了Azure SDK,可以在Visual Studio中实时访问我的存储。 您也可以尝试第三方应用程序,例如Cloudberry Explorer 。 在这里,我已经将一个名为scottha.png的文件上传到输入容器中。

image

After a few minutes the SDK will process the new blob (Queues are faster, but blobs are checked every 10 minutes), the job will run and either succeed or fail. If your app throws an exception you'll actually see it in the Invocation Details part of the site.

几分钟后,SDK将处理新的Blob(队列速度更快,但每10分钟检查一次Blob),该作业将运行,并且成功或失败。 如果您的应用引发异常,您实际上会在网站的“调用详细信息”部分看到它。

image

Here's a successful one. You can see it worked (it squished) because of the number of input bytes and the number of output bytes.

这是成功的。 由于输入字节数和输出字节数,您可以看到它有效(被压缩)。

image

You can see the full output of what happens in a WebJob within this Dashboard, or check the log files directly via FTP. For me, I can explore my output container in Azure Storage and download or use the now-squished images. Again, this can be used for any large job whether it be processing images, OCR, log file analysis, SQL server cleanup, whatever you can think of.

您可以在此仪表板内的WebJob中查看所发生情况的完整输出,或直接通过FTP查看日志文件。 对于我来说,我可以在Azure存储中浏览我的输出容器,然后下载或使用现在压缩的图像。 同样,此功能可用于任何大型任务,无论您是想处理图像,OCR,日志文件分析,SQL Server清理。

Azure WebJobs is in preview, so there will be bugs, changing documentation and updates to the SDK but the general idea is there and it's solid. I think you'll dig it.

Azure WebJobs处于预览阶段,因此会出现bug,更改文档和对SDK的更新,但总的思路就在这里,而且扎实。 我想你会去挖掘它。

相关链接 (Related Links)

翻译自: https://www.hanselman.com/blog/introducing-windows-azure-webjobs

azure

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值