Long running timers that need to survive shutdowns

What is the best way to go about creating an app that waits for a long time before running a task? Let’s say I need my application to wait for 1 year and be precise to the second, what would be the best way to achieve this in ASP.NET?

I know about background services but if the I’m not sure they’re intended for these cases.

11

Assuming that these tasks run very infrequently, it probably isn’t worth having a process run all the time. Instead I would suggest using an OS feature such as Scheduled tasks or at to start the process shortly before (typically seconds) it is required.

As the application starts up it should open database connections, prefetch any data it needs do any pre-calculations required. Once initialized, the app can check the current time and sleep for however long required until the task needs to run.

Note: Even system sleep calls can be a little imprecise, but that should be good enough for “second” precision

Once the job is complete the process can simply exit.


Per gnasher729’s comment, the computer does have to be on in order to run a program and you may want to ensure it’s running NTP to ensure that the clock is correct.

If it is absolutely critical that the process runs at the specific time you may need to look into HA solutions running across geographically diverse data centers.

You should also investigate solutions for distributed scheduling since you will likely run into CAP restrictions.

TL;DR – You probably can’t absolutely guarantee it runs exactly once at the precise time you are looking for – you will need to make some compromises / assumptions.

1

Are we there yet?

No.

Are we there yet?

No.

This is called polling. It lives at the heart of every system. One thing is very true about polling. The fewer processes doing it the better.

That’s why we invented scheduling. Rather than running every random thing that decides it needs to ask if we’re there yet over and over we run one thing that calls things that want to be woken up once we’re there.

Every system has a solution for this. Please use it.

1

1 year is an extreme wait, but I think the best design is to have an always running process which continually checks for or delays between runs. For example a windows service

The benefit of this over a scheduler is that you can give it a health check and alerting for when it goes down or fails to run.

Too often with schedule tasks they don’t run for some reason and you don’t notice. It’s hard to take an alerting action when nothing is running, and to check every year that the once a year process has run is just scheduled tasks all the way down.

A windows service (or k8s console app with a loop) can

  • expose a http health check endpoint
  • which can be constantly monitored by the same altering tool you use for the rest of your stuff.
  • save the last run date to disk
  • restart if it crashes
  • auto start on reboot
  • return a bad healthcheck
  • if the run did not occur as expected
  • if the run took too long to complete
  • if the service hangs in some non fatal way

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật