hangfire 定时访问api接口

using Hangfire.Common;
using Hangfire.MySql;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Hangfire
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            SqlSugarHelper.MySql = Configuration["MySql"];
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var storage = new MySqlStorage(Configuration["MySql"]
                , new MySqlStorageOptions { PrepareSchemaIfNecessary = true, TablesPrefix = "Hangfire" });
            services.AddHangfire(p => p.UseStorage(storage));
            services.AddHangfireServer();
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("Hangfire", new OpenApiInfo
                {
                    Title = "HangfireTitle",
                    Version = "v1",
                    Description = "HangfireDes"
                });
            });
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/Hangfire/swagger.json", "Hangfire");
            });
            app.UseRouting();
            app.UseHangfireDashboard();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
using Hangfire.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace Hangfire.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HangfireController : ControllerBase
    {
        public HangfireController()
        {
        }
        /// <summary>
        /// 启动自动备份
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult AddPoolsBackup()
        {
            RecurringJob.AddOrUpdate("PoolsBackup", () => HttpHelper.GetHttp("http://192.168.0.159:16888/xxxxxx/System/TestAdd"), Cron.Minutely(), TimeZoneInfo.Local);
            return Ok("ok");
        }
        /// <summary>
        /// 删除自动备份任务
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DeletePoolsBackup()
        {
            RecurringJob.RemoveIfExists("PoolsBackup");
            return Ok("ok");
        }
    }
}

 

using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace Hangfire.Common
{
    public class HttpHelper
    {
        public static void GetHttp(string url)
        {
            var client = new RestClient(url);
            var request = new RestRequest(Method.GET);
            var response = client.Execute(request);
            var content = response.Content;
        }
    }
}

RestSharp,访问http的好插件

 

posted @ 2019-05-27 15:59  天天的蓝色  阅读(1441)  评论(0编辑  收藏  举报