DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • The Impact of AI Agents on Modern Workflows
  • A Developer’s Guide to Multithreading and Swift Concurrency
  • Mastering AI Agents: How Agentic Design Patterns Make Agents Smarter
  • Deep Work for Site Reliability Engineers

Trending

  • Chat With Your Knowledge Base: A Hands-On Java and LangChain4j Guide
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • Efficient API Communication With Spring WebClient
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Scheduled Tasks With Hyperlambda and Retry

Scheduled tasks imply repeatedly executing some piece of code according to some interval. Adding retry, implies retrying execution until it succeeds.

By 
Thomas Hansen user avatar
Thomas Hansen
DZone Core CORE ·
Aug. 18, 22 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
3.8K Views

Join the DZone community and get the full member experience.

Join For Free

Scheduled tasks imply repeatedly executing some piece of code according to some interval. Adding retry implies retrying execution until it succeeds. Creating scheduled tasks with Hyperlambda is extremely easy. Making sure your tasks execute using retry logic is slightly more complex, but it's quite easy once you understand the flow of things, and you learn some basic Hyperlambda. To understand our code, we'll need to analyze some of the core Hyperlambda slots first.

Hyperlambda Is Async to the Bone

The above implies that Hyperlambda consumes tiny amounts of operating system resources while it's waiting for tasks. If you invoke [http.get] to retrieve some data from another server, the thread your Hyperlambda is scheduled to run on is actually suspended and released back to the operating system. This allows your operating system to "reuse" threads for different tasks, resulting in your app as a whole can accept a lot more throughput. Your app scales much better as a result.

All thread-related slots are also async to the bone. This implies that if you invoke [sleep], the thread is released while Hyperlambda waits.

The Hyperlambda Task Scheduler

Some additional theory about the Hyperlambda task scheduler might be needed now. This helps you understand its scalability traits. A lot of task schedulers will consume your server's resources until you've got nothing left. Hyperlambda's task scheduler does not operate like this. First of all, it's using a semaphore to make sure a maximum of 8 tasks is executed simultaneously. If task number 9 tries to execute, it needs to wait for one of the other tasks to finish before it's given CPU time. This prevents your operating system from being "flooded" with scheduled tasks executed in parallel.

In addition, it never re-schedules a repeated task before the task is finished executing. So it's literally impossible to create a task that repeats often enough by itself to flood your semaphore resulting in a stack overflow, or out-of-memory exception. It is still possible to create "bad code" that somehow exhausts your server, but it's actually very difficult, and almost requires a conscious choice from your side as a software developer.

The Code

Now that we understand the internals of Hyperlambda's task scheduler, it's time for some code.

Plain Text
.no:int:5
while
   mt
      get-value:x:@.no
      .:int:0
   .lambda
      try

         // Do stuff here ...
         // If exception, task will "retry" 5 times.

         // Task succeeded, "breaking" while loop!
         set-value:x:@.no
            .:int:0

      .catch

         // Task failed, retrying 5 times.
         log.error:Task failed ...
         sleep:1000
         math.increment:x:@.no

      // Task is finished, [while] loop will now abort.

The above code will execute the thing inside your [try] block, and if it's not succeeding, it will retry 5 times before giving up. Copy and paste the above code into scheduled tasks such as illustrated below.

Tasks

At this point, all we need to do is to schedule or task. This is done by clicking the clock icon in the bottom right corner of your task and choosing a repetition pattern.

Scheduled tasks

The above schedule your task to execute with a 5-day interval. There are repetition patterns for everything ranging from "1st of every month" to "every Tuesday and Thursday". Below are the basic building blocks for creating repeating tasks.

Unit Repetition

This repetition is in the form of n.unit. n is a number, and the unit can be any of the following values.

  • seconds
  • minutes
  • hours
  • days
  • weeks
  • months

Creating a pattern such as for instance 5.weeks implies your task will execute once and then wait for 5 weeks before executing again.

Weekday Repetition

This pattern resembles the following; ww.HH.mm.ss. The parts imply the following in order of appearance.

  • Weekdays
  • Hours
  • Minutes
  • Seconds

The weekday part can pipe multiple weekdays together, allowing you to use the following pattern Tuesday|Thursday.23.55.00 to execute your task every Tuesday and Thursday 5 minutes to midnight. Weekdays can have a double asterisk as their value, implying "every weekday". This allows you to declare a pattern such as follows **.05.00.00 to execute the task every day at 5 AM in the morning.

Monthly Repetition

This pattern is as follows MM.dd.HH.mm.ss and the entities imply in order of appearance the following.

  • Month
  • Day of month
  • Hours
  • Minutes
  • Seconds

To create a task that executes only on February the 5th at 7 AM, you could use values such as follows 02.05.07.00.00. A month can be piped just as weekdays above, allowing you to supply multiple months such as this; 01|02|03.05.07.00.00. The last pattern executes January, February, and March the 5th, at 7 AM in the morning.

The MM, dd, and WW values in both of the above repetition patterns can have ** as their value implies "all values".

Ask Us for Help

This article was created because one of our partners had a specific use case needing "HangFire functionality". Instead of explaining to him directly, I chose to write an article where I solved his use case. If you have some problem related to Hyperlambda or Magic, let us know, and we'll help you out :)

Task (computing)

Published at DZone with permission of Thomas Hansen. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Impact of AI Agents on Modern Workflows
  • A Developer’s Guide to Multithreading and Swift Concurrency
  • Mastering AI Agents: How Agentic Design Patterns Make Agents Smarter
  • Deep Work for Site Reliability Engineers

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!