如何使用Azure Scheduler配置和执行测试
我是Azure调度器的新手,希望使用控制台实现演示应用程序 由于我已经从中阅读了帮助内容,但是我不知道我必须在哪里进行更改 如果以前有人做过,请帮我做这件事如何使用Azure Scheduler配置和执行测试,azure,scheduled-tasks,scheduler,job-scheduling,azure-scheduler,Azure,Scheduled Tasks,Scheduler,Job Scheduling,Azure Scheduler,我是Azure调度器的新手,希望使用控制台实现演示应用程序 由于我已经从中阅读了帮助内容,但是我不知道我必须在哪里进行更改 如果以前有人做过,请帮我做这件事 提前感谢:)以下是使用Azure Scheduler SDK创建和管理作业的代码示例,供您参考: namespace SchedulerArmSDKTemplate { using Microsoft.Azure; using Microsoft.Azure.Common.Authentication.Models;
提前感谢:)以下是使用Azure Scheduler SDK创建和管理作业的代码示例,供您参考:
namespace SchedulerArmSDKTemplate
{
using Microsoft.Azure;
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.Azure.Management.Scheduler;
using Microsoft.Azure.Management.Scheduler.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Threading.Tasks;
class Program
{
private static AzureEnvironment _environment;
static void Main(string[] args)
{
// Set Environment - Choose between Azure public cloud, china cloud and US govt. cloud
_environment = AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud];
// Get the credentials
var tokenCloudCreds = GetCredsFromServicePrincipal();
var tokenCreds = new TokenCredentials(tokenCloudCreds.Token);
// Use credentials to create Scheduler managment client.
SchedulerManagementClient schedulerManagementClient = new SchedulerManagementClient(_environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager), tokenCreds)
{
SubscriptionId = ConfigurationManager.AppSettings["AzureSubscriptionId"]
};
CreateJobCollectionAndJobs(schedulerManagementClient);
}
private static TokenCloudCredentials GetCredsFromServicePrincipal()
{
var subscriptionId = ConfigurationManager.AppSettings["AzureSubscriptionId"];
var tenantId = ConfigurationManager.AppSettings["AzureTenantId"];
var clientId = ConfigurationManager.AppSettings["AzureClientId"];
var clientSecret = ConfigurationManager.AppSettings["AzureClientSecret"];
// Quick check to make sure we're not running with the default app.config
if (subscriptionId[0] == '[')
{
throw new Exception("You need to enter your appSettings in app.config to run this sample");
}
var authority = String.Format("{0}{1}", _environment.Endpoints[AzureEnvironment.Endpoint.ActiveDirectory], tenantId);
var authContext = new AuthenticationContext(authority);
var credential = new ClientCredential(clientId, clientSecret);
var authResult = authContext.AcquireToken(_environment.Endpoints[AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId], credential);
return new TokenCloudCredentials(subscriptionId, authResult.AccessToken);
}
private static JobCollectionDefinition BuildJobCollecionDefinition(string jobCollectionName, string location)
{
return new JobCollectionDefinition()
{
Name = jobCollectionName,
Location = location,
Properties = new JobCollectionProperties()
{
Sku = new Sku()
{
Name = SkuDefinition.Standard,
},
State = JobCollectionState.Enabled,
Quota = new JobCollectionQuota()
{
MaxRecurrence = new JobMaxRecurrence()
{
Frequency = RecurrenceFrequency.Minute,
Interval = 1,
},
MaxJobCount = 5
}
}
};
}
private static void CreateJobCollectionAndJobs(SchedulerManagementClient schedulerManagementClient)
{
var resourceGroup = ConfigurationManager.AppSettings["AzureResourceGroup"];
var location = ConfigurationManager.AppSettings["AzureLocation"];
var jobCollectionNamePrefix = "jc_";
var jobCollectionName = string.Format("{0}{1}", jobCollectionNamePrefix, Guid.NewGuid().ToString());
schedulerManagementClient.JobCollections.CreateOrUpdate(
resourceGroupName: resourceGroup,
jobCollectionName: jobCollectionName,
jobCollection: BuildJobCollecionDefinition(jobCollectionName, location));
CreateOrUpdateJob(schedulerManagementClient, resourceGroup, jobCollectionName, Guid.NewGuid().ToString(), RecurrenceFrequency.Week);
CreateOrUpdateJob(schedulerManagementClient, resourceGroup, jobCollectionName, Guid.NewGuid().ToString(), RecurrenceFrequency.Hour);
}
private static void CreateOrUpdateJob(
SchedulerManagementClient schedulerManagementClient,
string resourceGroupName,
string jobCollectionName,
string jobName,
RecurrenceFrequency recurrenceRequency)
{
var headers = new Dictionary<string, string>();
var andomHour = new Random();
var randomMinute = new Random();
var randomSecond = new Random();
schedulerManagementClient.Jobs.CreateOrUpdate(
resourceGroupName,
jobCollectionName,
jobName,
new JobDefinition()
{
Properties = new JobProperties()
{
StartTime = DateTime.UtcNow,
Action = new JobAction()
{
Type = JobActionType.Http,
Request = new HttpRequest()
{
Uri = ConfigurationManager.AppSettings["HttpActionUrl"],
Method = "GET",
},
RetryPolicy = new RetryPolicy()
{
RetryType = RetryType.None,
}
},
Recurrence = new JobRecurrence()
{
Frequency = recurrenceRequency,
Interval = 1,
Count = 10000,
},
State = JobState.Enabled,
}
});
}
}
}
namespace SchedulerArmSDKTemplate
{
使用Microsoft.Azure;
使用Microsoft.Azure.Common.Authentication.Models;
使用Microsoft.Azure.Management.Scheduler;
使用Microsoft.Azure.Management.Scheduler.Models;
使用Microsoft.IdentityModel.Clients.ActiveDirectory;
使用微软.Rest;
使用制度;
使用System.Collections.Generic;
使用系统配置;
使用System.Threading.Tasks;
班级计划
{
私有静态AzureEnvironment\u环境;
静态void Main(字符串[]参数)
{
//设置环境-在Azure公共云、中国云和美国政府云之间选择
_environment=AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud];
//获取凭证
var tokenCloudCreds=GetCredsFromServicePrincipal();
var tokenCreds=新的TokenCredentials(tokenCloudCreds.Token);
//使用凭据创建计划程序管理客户端。
SchedulerManagementClient SchedulerManagementClient=新SchedulerManagementClient(_environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),tokenCreds)
{
SubscriptionId=ConfigurationManager.AppSettings[“AzureSubscriptionId”]
};
CreateJobCollectionAndJobs(schedulerManagementClient);
}
私有静态令牌CloudCredentials GetCredFromServicePrincipal()
{
var subscriptionId=ConfigurationManager.AppSettings[“AzureSubscriptionId”];
var tenantId=ConfigurationManager.AppSettings[“AzureTenantId”];
var clientId=ConfigurationManager.AppSettings[“AzureClientId”];
var clientSecret=ConfigurationManager.AppSettings[“AzureClientSecret”];
//快速检查以确保我们没有使用默认的app.config运行
if(subscriptionId[0]='[')
{
抛出新异常(“您需要在app.config中输入appSettings才能运行此示例”);
}
var authority=String.Format(“{0}{1}”,_environment.Endpoints[AzureEnvironment.Endpoint.ActiveDirectory],tenantId);
var authContext=新的AuthenticationContext(授权);
var-credential=新的ClientCredential(clientId,clientSecret);
var authResult=authContext.AcquireToken(_environment.Endpoints[AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId],凭证);
返回新的TokenCloudCredentials(subscriptionId、authResult.AccessToken);
}
专用静态JobCollectionDefinition BuildJobCollectionIndefinition(字符串jobCollectionName,字符串位置)
{
返回新的JobCollectionDefinition()
{
Name=jobCollectionName,
位置=位置,
属性=新作业集合属性()
{
Sku=新Sku()
{
名称=SKU定义。标准,
},
状态=JobCollectionState.Enabled,
配额=新作业收集配额()
{
MaxRecurrence=新作业MaxRecurrence()
{
频率=复发频率。分钟,
间隔=1,
},
MaxJobCount=5
}
}
};
}
私有静态void CreateJobCollectionAndJobs(SchedulerManagementClient SchedulerManagementClient)
{
var resourceGroup=ConfigurationManager.AppSettings[“AzureResourceGroup”];
变量位置=ConfigurationManager.AppSettings[“AzureLocation”];
var jobCollectionNamePrefix=“jc_3;”;
var jobCollectionName=string.Format(“{0}{1}”,jobCollectionNamePrefix,Guid.NewGuid().ToString());
schedulerManagementClient.JobCollections.CreateOrUpdate(
resourceGroupName:resourceGroup,
jobCollectionName:jobCollectionName,
jobCollection:buildJobCollectionIndefinition(jobCollectionName,location));
CreateOrUpdateJob(schedulerManagementClient,resourceGroup,jobCollectionName,Guid.NewGuid().ToString(),RecurrenceFrequency.Week);
CreateOrUpdateJob(schedulerManagementClient,resourceGroup,jobCollectionName,Guid.NewGuid().ToString(),RecurrenceFrequency.Hour);
}
私有静态void CreateOrUpdateJob(
SchedulerManagementClient SchedulerManagementClient,
字符串resourceGroupName,
字符串jobCollectionName,
字符串jobName,
复发频率(复发)
{
var headers=newdictionary();
var andomHour=新随机数();
var randomMinute=新随机数();
var randomSecond=新随机数();
schedulerManagementClient.Jobs.CreateOrUpdate(
resourceGroupName,
jobCollectionName,
jobName,
新职位定义()
{
属性=新作业属性()
{
StartTime=DateTime.UtcNow,
操作=新作业操作()
{
Type=JobActionType.Http,
请求=新的HttpRequest()
{