I have a rakefile with several tasks and some of them take a lot of time (hours or days). I usually run some tasks, and left the rest to be executed in another day. How can I list the status of all the tasks? I am imaging something like rake --status
that shows the pending and up-to-date tasks? I do not want to write a separate to-do list to record the status of tasks. Is there a solution for this?
Asked
Active
Viewed 185 times
1

Daniel Hernández
- 1,279
- 8
- 15
-
1Rake is the Ruby version of Makefile. So it nothing more than a simple Ruby script that executes when asked to. You will need to keep track of which tasks where run and which ones weren't. A better approach would be to keep track of the state of the application/data and then execute the required task depending on your business logic – TheGeorgeous Nov 03 '20 at 06:22
-
So, you propose, for instance, creating a database table associating each task to its status, and after each task execution update the status of the task? This implies that there is no off-the-shell solution for this? – Daniel Hernández Nov 03 '20 at 07:05
-
It could help if we put tasks before and after each task execution. This is explained in this question: https://stackoverflow.com/questions/15707940/rake-before-task-hook. Also, an extension for these hooks is described here: https://github.com/guillermo/rake-hooks. – Daniel Hernández Nov 03 '20 at 07:19
-
I was researching about how to solve this issue and I found two alternative solutions. 1) If I access the instance of the class `Application` that is created when running rake then I can access all tasks using the attribute `tasks`. For each task I can use the method `needed?` to check the task status. 2) Create a class `MyTask` that inherits from `Task` but also records the status of the task in a database, to be queried. Also, redefine the method `needed?` to query the database. – Daniel Hernández Nov 03 '20 at 08:14
-
1Looks like `sidekiq` and `sidekiq-scheduler` or `sidekiq-cron` fit your needs. If you move your code from rake tasks to sidekiq jobs, you can track their current state. There is a web UI also https://github.com/mperham/sidekiq/wiki/Monitoring – Yakov Nov 03 '20 at 08:36
-
Thanks, I think that the solution I posted below answer my problem. However, I will check `sidekiq` to see if it fits my needs. – Daniel Hernández Nov 03 '20 at 08:55
1 Answers
1
I can run the Rakefile
as any other ruby script. Then I can access the tasks by using the following code:
Rake.application.tasks.each do |tsk|
puts "#{tsk.name}: #{tsk.needed?}"
end
This shows the status of each task.
Also, the class Task
can be extended to record the status of tasks in a database, and to provide more information about the execution of tasks.

Daniel Hernández
- 1,279
- 8
- 15
-
Your tasks might be run from different processes concurrently (it becomes even more likely in cases like yours - when the task execution takes many hours) - in this case, this solution won't work reliably (Rake::Application will know nothing about its "twins" running in parallel). I'd say if you need precise control you need an external (for Ruby process) mechanism (db/redis/you name it...) – Konstantin Strukov Nov 03 '20 at 08:52