BackgroundJobInfo.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.ComponentModel.DataAnnotations.Schema;
  4. using Abp.Domain.Entities.Auditing;
  5. using Abp.MultiTenancy;
  6. using Abp.Timing;
  7. namespace Abp.BackgroundJobs
  8. {
  9. /// <summary>
  10. /// Represents a background job info that is used to persist jobs.
  11. /// </summary>
  12. [Table("AbpBackgroundJobs")]
  13. [MultiTenancySide(MultiTenancySides.Host)]
  14. public class BackgroundJobInfo : CreationAuditedEntity<long>
  15. {
  16. /// <summary>
  17. /// Maximum length of <see cref="JobType"/>.
  18. /// Value: 512.
  19. /// </summary>
  20. public const int MaxJobTypeLength = 512;
  21. /// <summary>
  22. /// Maximum length of <see cref="JobArgs"/>.
  23. /// Value: 1 MB (1,048,576 bytes).
  24. /// </summary>
  25. public const int MaxJobArgsLength = 1024 * 1024;
  26. /// <summary>
  27. /// Default duration (as seconds) for the first wait on a failure.
  28. /// Default value: 60 (1 minutes).
  29. /// </summary>
  30. public static int DefaultFirstWaitDuration { get; set; }
  31. /// <summary>
  32. /// Default timeout value (as seconds) for a job before it's abandoned (<see cref="IsAbandoned"/>).
  33. /// Default value: 172,800 (2 days).
  34. /// </summary>
  35. public static int DefaultTimeout { get; set; }
  36. /// <summary>
  37. /// Default wait factor for execution failures.
  38. /// This amount is multiplated by last wait time to calculate next wait time.
  39. /// Default value: 2.0.
  40. /// </summary>
  41. public static double DefaultWaitFactor { get; set; }
  42. /// <summary>
  43. /// Type of the job.
  44. /// It's AssemblyQualifiedName of job type.
  45. /// </summary>
  46. [Required]
  47. [StringLength(MaxJobTypeLength)]
  48. public virtual string JobType { get; set; }
  49. /// <summary>
  50. /// Job arguments as JSON string.
  51. /// </summary>
  52. [Required]
  53. [StringLength(MaxJobArgsLength)]
  54. public virtual string JobArgs { get; set; }
  55. /// <summary>
  56. /// Try count of this job.
  57. /// A job is re-tried if it fails.
  58. /// </summary>
  59. public virtual short TryCount { get; set; }
  60. /// <summary>
  61. /// Next try time of this job.
  62. /// </summary>
  63. //[Index("IX_IsAbandoned_NextTryTime", 2)]
  64. public virtual DateTime NextTryTime { get; set; }
  65. /// <summary>
  66. /// Last try time of this job.
  67. /// </summary>
  68. public virtual DateTime? LastTryTime { get; set; }
  69. /// <summary>
  70. /// This is true if this job is continously failed and will not be executed again.
  71. /// </summary>
  72. //[Index("IX_IsAbandoned_NextTryTime", 1)]
  73. public virtual bool IsAbandoned { get; set; }
  74. /// <summary>
  75. /// Priority of this job.
  76. /// </summary>
  77. public virtual BackgroundJobPriority Priority { get; set; }
  78. static BackgroundJobInfo()
  79. {
  80. DefaultFirstWaitDuration = 60;
  81. DefaultTimeout = 172800;
  82. DefaultWaitFactor = 2.0;
  83. }
  84. /// <summary>
  85. /// Initializes a new instance of the <see cref="BackgroundJobInfo"/> class.
  86. /// </summary>
  87. public BackgroundJobInfo()
  88. {
  89. NextTryTime = Clock.Now;
  90. Priority = BackgroundJobPriority.Normal;
  91. }
  92. /// <summary>
  93. /// Calculates next try time if a job fails.
  94. /// Returns null if it will not wait anymore and job should be abandoned.
  95. /// </summary>
  96. /// <returns></returns>
  97. public virtual DateTime? CalculateNextTryTime()
  98. {
  99. var nextWaitDuration = DefaultFirstWaitDuration * (Math.Pow(DefaultWaitFactor, TryCount - 1));
  100. var nextTryDate = LastTryTime.HasValue
  101. ? LastTryTime.Value.AddSeconds(nextWaitDuration)
  102. : Clock.Now.AddSeconds(nextWaitDuration);
  103. if (nextTryDate.Subtract(CreationTime).TotalSeconds > DefaultTimeout)
  104. {
  105. return null;
  106. }
  107. return nextTryDate;
  108. }
  109. }
  110. }