Code Monkey home page Code Monkey logo

hangfire.httpjob's Introduction

Hangfire.HttpJob for .netcore

Hangfire.HttpJob for Hangfire

  1. add delay background job by [http post] or on dashbord

  2. add recurring job by [http post] or on dashbord

  3. search job by jobname on dashbord

  4. stop or start job on dashbord

  5. cron generator on dashbord

  6. use Hangfire.HttpJob.Agent extention to quick develop job program

    6.1 Make your webjob very convenient to support scheduling execution

    6.2 Visualizing the execution process of webjob by logs and progress on hangfire dashbord

    6.3 Variety of webjob types with different life cycles

    6.3.1 Singleton

    6.3.2 Transient

    6.3.3 Hang up until stop command

wiki

00.QickStart DockerQuickStart

01.how to create backgroud httpjob

02.how to create recurringHttpJob

03.how to use HttpJob.Agent

04.how to use in sqlserver

05.how to config mail service to report job result

https://github.com/yuzd/Hangfire.HttpJob/wiki

Installation

This library is available as a NuGet Package:

Install-Package Hangfire.HttpJob

Install-Package Hangfire.HttpJob.Agent

Install-Package Hangfire.HttpJob.Client

Usage

	//StartUp.cs
 
	public virtual void ConfigureServices(IServiceCollection services)
	{
		services.AddHangfire(Configuration);//Configuration是下面的方法
	}

	private void Configuration(IGlobalConfiguration globalConfiguration)
	{
		globalConfiguration.UseStorage(
				new MySqlStorage(
					"Server=localhost;Port=3306;Database=hangfire;Uid=root;Pwd=123456;charset=utf8;SslMode=none;Allow User Variables=True",
					new MySqlStorageOptions
					{
						TransactionIsolationLevel = IsolationLevel.ReadCommitted,
						QueuePollInterval = TimeSpan.FromSeconds(15),
						JobExpirationCheckInterval = TimeSpan.FromHours(1),
						CountersAggregateInterval = TimeSpan.FromMinutes(5),
						PrepareSchemaIfNecessary = false,
						DashboardJobListLimit = 50000,
						TransactionTimeout = TimeSpan.FromMinutes(1),
					}))
			.UseConsole()
			.UseHangfireHttpJob();
	}

	public void Configure(IApplicationBuilder app)
	{
		app.UseHangfireServer();
		app.UseHangfireDashboard("/hangfire",new DashboardOptions
		{
			Authorization = new[] { new BasicAuthAuthorizationFilter(new BasicAuthAuthorizationFilterOptions
			{
				RequireSsl = false,
				SslRedirect = false,
				LoginCaseSensitive = true,
				Users = new []
				{
					new BasicAuthAuthorizationUser
					{
						Login = "admin",
						PasswordClear =  "test"
					} 
				}

			}) }
		});
	}

add Hangfire HttpJob by client

    Install-Package Hangfire.HttpJob.Client

    var serverUrl = "http://localhost:5000/job";
    var result = HangfireJobClient.AddBackgroundJob(serverUrl, new BackgroundJob
    {
	JobName = "测试api",
	Method = "Get",
	Url = "http://localhost:5000/testaaa",
	Mail = new List<string> {"[email protected]"},
	SendSucMail = true,
	DelayFromMinutes = 1
    }, new HangfireServerPostOption
    {
	BasicUserName = "admin",
	BasicPassword = "test"
    });
    
    var result = HangfireJobClient.AddRecurringJob(serverUrl, new RecurringJob()
    {
	JobName = "测试5点40执行",
	Method = "Post",
	Data = new {name = "aaa",age = 10},
	Url = "http://localhost:5000/testpost",
	Mail = new List<string> { "[email protected]" },
	SendSucMail = true,
	Cron = "40 17 * * *"
    }, new HangfireServerPostOption
    {
	BasicUserName = "admin",
	BasicPassword = "test"
    });

How to add Hangfire.HttpJob by restful api

1.add backgroundjob

url:http://{hangfireserver}/hangfire/httpjob?op=backgroundjob
method:post
data:
{
  "Method": "POST",
  "ContentType": "application/json",
  "Url": "http://XXXXXXX",
  "DelayFromMinutes": 1,
  "Data": "{\"userName\":\"test\"}",
  "Timeout": 5000,
  "BasicUserName": "",// 如果你希望hangfire执行http的时候带basic认证的话 就设置这2个参数
  "BasicPassword": "",
  "JobName": "test_backgroundjob"
}

2.add recurringjob

url:http://{hangfireserver}/hangfire/httpjob?op=recurringjob
method:post
data:
{
  "Method": "POST",
  "ContentType": "application/json",
  "Url": "http://XXXXXXX",
  "Data": "{\"userName\":\"test\"}",
  "Timeout": 5000,
  "Corn": "0 12 * */2",
  "BasicUserName": "",// 如果你希望hangfire执行http的时候带basic认证的话 就设置这2个参数
  "BasicPassword": "",
  "JobName": "test_recurringjob"
}

How to add Hangfire.HttpJob in Dashbord

image image image image image image image

Email notify

image

Thanks for the Rider IDE provided by JetBrains

hangfire.httpjob's People

Contributors

1257960069 avatar dependabot[bot] avatar githubike avatar hueifeng avatar ijlynivfhp avatar m344739968 avatar visuallogin avatar windnight avatar xqueezeme avatar yuzd avatar zhujiancc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hangfire.httpjob's Issues

是否支持job分组化管理

您好,我现在看到只有两种权限的面板,只读和管理员,目前是否有基于用户角色管理job的解决方案,若无,后续是否考虑支持?
eg:3个业务部门均只能看到自己部门下的job

Ability to specify http headers for httpJob

Hi, thanks for the great library. It's neatly designed, clean and very useful. Here's my question. How can we specify http headers for the RecurringJob? Sometimes, we need to pass a bearer token as part of the header during http post request. (e.g. bearer token in Authorization header). I'm not sure if there's an easy way to accomplish this, what do you think?

作业的Timeout 参数,设置较大值时失效

比如周期性作业,在json编辑器中设置 "Timeout": 180000
作业启动后,在1分40秒会出现异常,导致作业失败
+1m 40.598s
System.Threading.Tasks.TaskCanceledException: The operation was canceled. ---> System.IO.IOException: Unable to read data from the transport connection: 由于线程退出或应用程序请求,已中止 I/O 操作。. ---> System.Net.Sockets.SocketException: 由于线程退出或应用程序请求,已中止 I/O 操作。

作业调用的web api有做超时配置
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup()
.ConfigureKestrel((context, options) =>
{
options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(40);
options.Limits.RequestHeadersTimeout = TimeSpan.FromMinutes(40);
});

HttpJob.Client Forbidden bug

hi, i dont know why, but when i try to make a post adding a background job, it returns 403 Forbiden, but when i register by the dashboard it works.
Is there any thing i am missing?
image

一个job顺序调用多个api

首先表示感谢,非常棒的库!
一个应用场景:
厂商A拥有api1,厂商二拥有api2,因其功能,我作为调用者必须依次调用。这在数据中心的数据处理中很常见。

所以,能否实现这样的功能,在option或面板配置时支持列表(或者说json数组),类似
{
"JobName": "job1",
"同步或异步":"同步是为了顺序执行,异步则跟目前的功能没多大区别,只是把需要在一个时间段要干的活打包了一下"
"services": [
{
"Method": "GET",
"ContentType": "application/json",
"Url": "http://",
"Headers": {},
"Data": {}
},
{
"Method": "GET",
"ContentType": "application/json",
"Url": "http://",
"Headers": {},
"Data": {}
}
]
}

Http Code 400 not reported as failed task

Hi
We noticed that if the service invoked returns an HTTP 400 error, the job is still classifed as successfull. Is there a way to get these reported as failed (and triggering an error notification)? Thanks

提问:调度没有准时执行?

我新增了一个周期性调度,设置为每日00:00(cron:0 0 0 * * ?)执行。但是并没有在00:00执行调度,有时候是在在06:30执行了,有时候是在我打开dashboard后立即执行的。请问是不是需要一直用浏览器打开dashboard才能正常运行呢?

Using sending emails from httpjob

What do I need to use the sending of emails when an httpjob task was completed, either with errors or successfully.

Attached capture of task settings.
Could you give me a help?
Thank you
Regards

image

空值异常

var excuteDataList = filterContext.Connection.GetAllEntriesFromHash(hashKey);
excuteDataList应该判空

新增循环任务时一直提示Forbidden

没有设置basic验证,但是在调用HangfireJobClient.AddRecurringJob方法时返回结果一直是Forbidden
把相同的参数复制到界面操作却是可以的

部分代码

var result = HangfireJobClient.AddRecurringJob("http://localhost:52500/job", new RecurringJob() { JobName = taskname, Method = "Get", Url = "http://localhost:52500/hangfire/addprocedurejob", SendSucMail = false, Cron = corn });

Docker方式还是挺麻烦,能否提供Redis作为存储的一键安装的docker方式

Docker方式还是挺麻烦,能否提供Redis作为存储的一键安装的docker方式。
因为这个类似我们的工具,我们如果有个想法,希望能非常流畅的开箱即用的方式来搭建一台docker环境。
另外很多场景都需要用到异步任务,比如:订单的处理,处理过程可以分为强依赖和弱依赖的,而弱依赖
一般都可以作为异步操作的,这时候就会将订单处理切分成两个或者多个请求,除了强依赖的第一个请求之后,
其他的都希望用这个组件来帮忙处理,而现在引入这个组件需要额外的数据库,太麻烦了,如果有Redis快速
搭建一套,那就可以让开发人员只聚焦于业务逻辑了。

邮件客户端密码是否可以改为非必输项?

您好,邮件客户端密码是否可以改为非必输项?即
if (string.IsNullOrWhiteSpace(this.Server) || string.IsNullOrWhiteSpace(this.User) || string.IsNullOrWhiteSpace(this.Password)) return null;
这里返回null与try catch后return null好像没有多大区别

延迟任务失败,提示Http状态码:Unauthorized ===> CheckResult: Fail

延迟任务失败,提示Http状态码:Unauthorized ===> CheckResult: Fail。业务接口正常,业务接口无身份验证,debug的时候发现没有请求发送到业务接口来。不明白问题出在哪里。尝试了BasicUserName和BasicPassword赋值,依旧还是出现问题。

Hangfire.HttpJob.Support.HttpStatusCodeException: Http状态码:Unauthorized ===> CheckResult: Fail
at Hangfire.HttpJob.Server.HttpJob.Run(HttpJobItem item, PerformContext context, List`1 logList, HttpJobItem parentJob)
at Hangfire.HttpJob.Server.HttpJob.Excute(HttpJobItem item, String jobName, String queuename, Boolean isretry, PerformContext context)

Invoking custom header from dashboard

Dear @yuzd , thanks again for adding custom header support. I have 2 questions.

  1. How should we send custom headers from dashboard as plain string? At the backend you deserialize to dictionary, but UI fails to add the job when sending as delimited key value pairs. Here's an example:
    image

  2. When the httpJob is executed, there's no way to see the actual result of http response that the invoked service sends, correct? Right now, for example every http request is marked as successful even though actually it got 401 from the server.

Thanks!

如何指定任务的执行队列

hangfire启动的时候创建了多个队列,而目前提交的计划任务都是在DEFAULT队列中执行,请问如何实现指定队列进行执行?

Concurrent Execute problem

Cause of hangfire distributelock mechanism as below

  private static string GetResource(Job job)
  {         
        return $"{job.Type.ToGenericTypeString()}.{job.Method.Name}";
  }

if we put DisableConcurrentExecution attribute to the method we want to method as below

[AutomaticRetry(Attempts = 3)]
[DisplayName("HttpJob:{1}")]
[DisableConcurrentExecution(1000)]
public static void Excute(HttpJobItem item, string jobName = null, PerformContext context = null)
       {
           try
           {
               context.WriteLine(jobName);
               context.WriteLine(JsonConvert.SerializeObject(item));
               var client = GetHttpClient(item);
               var httpMesage = PrepareHttpRequestMessage(item);
               var httpResponse = client.SendAsync(httpMesage).GetAwaiter().GetResult();
               HttpContent content = httpResponse.Content;
               string result = content.ReadAsStringAsync().GetAwaiter().GetResult();
               context.WriteLine(result);
           }
           catch (Exception ex)
           {
               Logger.ErrorException("HttpJob.Excute", ex);
               context.WriteLine(ex.Message);
           }
}

and we create many httpjob to the hangfire,we only got one distributelock("HttpJob.Execute"),that our different jobs become execute one by one .not what we expected ,cause we only have one method to execute differenent jobs.

json参数是否支持中文字符

JobName、Data是否中文字符,运行项目执行带参数的job提示错误“Request headers must contain only ASCII characters.”

Problem with recurring task frequency

According to the cron expression generated every two minutes, it is 2 * * * *, but when registering a new task, a value greater than two minutes appears in the next execution column.
Is there a problem with the generated cron expression?
Thanks for your help forever
Regards
1

怎样集成HangfireDashboard?

好像文档并不是很明确,只需要引用 Hangfire.HttpJob?
但我发现,这个包依赖也太多了吧!不支持.net framework?
image

部署在Linux环境下因时区问题会使任务无法正常运行

大牛您好,我在试用中又发现一个时区问题,在windows环境下正常但在linux环境下却报错,找了一个相应的解决方案,仅供参考 。
[root@Centos7 BisJob]# dotnet BisJobSqlserver.dll
Hosting environment: Production
Content root path: /web/BisJob
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
fail: Hangfire.Server.RecurringJobScheduler[0]
Recurring job '接口测试' can't be scheduled due to an error and will be disabled.
System.TimeZoneNotFoundException: The time zone ID 'China Standard Time' was not found on the local computer.
---> System.IO.FileNotFoundException: Could not find file '/usr/share/zoneinfo/China Standard Time'.
File name: '/usr/share/zoneinfo/China Standard Time'
at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func2 errorRewriter) at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode) at System.IO.FileStream.OpenHandle(FileMode mode, FileShare share, FileOptions options) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at Internal.IO.File.ReadAllBytes(String path) at System.TimeZoneInfo.TryGetTimeZoneFromLocalMachine(String id, TimeZoneInfo& value, Exception& e) --- End of inner exception stack trace --- at System.TimeZoneInfo.FindSystemTimeZoneById(String id) at Hangfire.DefaultTimeZoneResolver.GetTimeZoneById(String timeZoneId) at Hangfire.RecurringJobEntity..ctor(String recurringJobId, IDictionary2 recurringJob, ITimeZoneResolver timeZoneResolver, DateTime now)
解决方案:建议使用TimeZoneConverter开源项目进行转换

//在不同操作系统上使用统一的方式区解决时区的问题
TimeZoneInfo timeZoneInfo = TZConvert.GetTimeZoneInfo("China Standard Time");
try
{
//支持添加一个 只能手动出发的
if (string.IsNullOrEmpty(jobItem.Cron))
{
RecurringJob.AddOrUpdate(jobItem.JobName, () => HttpJob.Excute(jobItem, jobItem.JobName, queueName, jobItem.EnableRetry, null), Cron.Never, timeZoneInfo, jobItem.QueueName.ToLower());
return true;
}

            RecurringJob.AddOrUpdate(jobItem.JobName, () => HttpJob.Excute(jobItem, jobItem.JobName, queueName, jobItem.EnableRetry, null), jobItem.Cron, timeZoneInfo, jobItem.QueueName.ToLower());
            return true;
        }
        catch (Exception ex)
        {
            Logger.ErrorException("HttpJobDispatcher.AddHttprecurringjob", ex);
            return false;
        }

Set表数据过大如何处理?

老哥,问个问题,Set表数据过大如何处理,跑了几个月,居然有7000w的数据,控制台(key是console开头的)似乎可以配置自动删除,有些key是tags开头的数据,可以配置自动删除嘛?多谢

HttpJob.Agent中AddJobAgent添加Job不成功

  1. 使用我自己的MyJobAgent 继承自JobAgent
    public abstract class MyJobAgent : JobAgent { // 我自己的一些共通实现 }
  2. 具体的job实现:
    public class Job1 : MyJobAgent { // 具体Job实现 }

这时候会出现:
Job1 is not registered!

其中源码\Hangfire.HttpJob.Agent\Config\JobAgentServiceConfigurer.cs line123中:
var agengList = (from t in types
where t.BaseType = typeof(JobAgent) &&
!t.IsAbstract &&
!t.IsInterface
select t).ToList();

我上述实现中t.BaseType和typeof(JobAgent)是永远不相等的,是否可以改成(不清楚会不会对其他实现有影响):
var agengList = (from t in types
where typeof(JobAgent).IsAssignableFrom(t) &&
!t.IsAbstract &&
!t.IsInterface
select t).ToList();

mysql添加周期性作业的问题

用mysql添加周期性作业的时间,到了指定时间执行之后,就会一直不停的往表hangfire_set插数据,而且不删除任务就会一直往里面插数据

job失败重试

Hi,你好,我学习了下你的代码发现job如果失败重试次数、间隔是不可以配置的,[AutomaticRetrySet(Attempts = 3, DelaysInSeconds = new[] { 20, 30, 60 }, LogEvents = true, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
有时api挂了不一定可以在这个时间段重启好,请问下有什么办法可以修改吗?谢谢你的解答。

403错误

通过restful api调用hangfire回传403错误,没有权限

并发高(1分钟600次),出现错误JobResult:System.NullReferenceException: Object reference not set to an instance of an object.

错误如下所示:
+43ms HttpStatusCode:OK
+47ms JobResult:System.NullReferenceException: Object reference not set to an instance of an object.
at Hangfire.HttpJob.Agent.JobAgentMiddleware.<>c.b__5_1(JobAgent r)
at System.Linq.Enumerable.WhereListIterator1.ToList() at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)
at Hangfire.HttpJob.Agent.JobAgentMiddleware.InvokeAsync(HttpContext httpContext, RequestDelegate next)
+54msJobEnd:2019-07-06 22:00:18

BackgroundJob.ContinueJobWith in HttpJobs ?

Is it possible to use BackgroundJob.ContinueJobWith with the extension HttpJobs?

I have developed an api website where I have 3 methods, and these must be executed synchronously

Thank you very much for your help
regards

你好,我有一个疑问,关于队列名称的。

1.默认的队列名称是Default.假如我新增一个周期性作业queuename="default001",这个任务是不会执行的,需要在

            app.UseHangfireServer(new BackgroundJobServerOptions()
            {
               // 写死了
                Queues = new[] { "default", "default001" }
            });

有没有其他办法?或者添加任务的时候队列不存在,则添加?

代码添加任务的方式貌似无效?

你好
我按照示例,用代码方式添加任务,但实际并未添加,而且我Basic密码特意输入错误都没有异常出现。

api接口返回成功,但是任务列表中没有新增这个任务

图片

图片

运行报错

[10:01:14 ERR] HttpJobDispatcher.GetJobItem
System.ArgumentException: Context argument should be of type AspNetCoreDashboardContext!
参数名: context
在 Hangfire.Dashboard.AspNetCoreDashboardContextExtensions.GetHttpContext(DashboardContext context)
在 Hangfire.HttpJob.Server.HttpJobDispatcher.GetJobItem(DashboardContext _context)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.