Simple scheduler in Ruby

Quite often you may encounter a need to perform some particular tasks deferred in time or in a cyclic manner, especially when collecting data from all kinds of online information services, which content is a subject of dynamic changes.

cronjob_crontabUsers of Unix and MacOS can therefore take advantage of the system tools like for instance at or crontab and it might be quite sufficient in most situations. Then, the task execution time (or its cycle) is defined outside the script code. However, if we want to program it by ourselves, independently from systems’ schedulers or when we just don’t have, for some reason, access to them, Ruby scripting language and rufus-scheduler library comes with a little help.

The script whose code is attached to the body of this article shows some useful things. Firstly, the scheduler itself as a core functionality, whose capabilities have been briefly described in the comments to the script. Second, how to save web pages to PDF files using the PDFKit library. Of course, there are no obstacles to use other forms of processing of collected data, such as those described in my previous articles about ‚data scraping’. For example, in one of these articles I described how to automatically import the results of data scraping into a spreadsheet. There is, of course, the same possibility to do it in this case, and even – going slightly forward – this method can be used to perform some advanced data visualization. Thirdly, in the script the signal handling has been implemented to allow users a ‚clean’ termination of the program execution, through ctrl-c break for example.

Coming to what the script is doing in particular – it was written for periodic collection of Stock Quotes from Warsaw Stock Exchange online service.

I recommend you a further modification of the script, because the scope of its application is not limited to the data scraping only. After a few simple modifications, it can be successfully used in any situation where deferred execution of tasks is required, in particular, for example, to test the network response times, check the network services availability, to verify changes in routing, examining the network bandwidth saturation or infrastructure equipment resources utilization, etc.

Don’t forget to install the appropriate libraries before running the script:

require 'open-uri'
require 'rubygems'
require 'rufus-scheduler'
require 'pdfkit'

# ----- Function to make a snapshot of web page saved to PDF file
def web2file(url, counter)
   site_image = PDFKit.new(url)
   site_image.to_file("notowania_#{counter}.pdf")
   puts "File saved: notowania_#{counter}.pdf"
end

# Signal handling 
trap "SIGINT" do
  puts "Exiting"
  exit
end

trap "SIGTERM" do
  puts "Exiting"
  exit
end

counter = 1

header =<<END
########################################################
# Simple scheduler to get Stock Quotes on Warsaw GPW   #
# by Janusz Nawrat - 2014                              #
# Results are saved to PDF files                       #
# See the code to customize scheduler to your needs    #
########################################################
END

puts header
begin
   scheduler = Rufus::Scheduler.new
   scheduler.every "1m" do
      web2file("http://www.parkiet.com/temat/1122373.html", counter)
      counter += 1
   end
   scheduler.join
   # Various forms of scheduling are supported:
   #
   # scheduler.in '10d' do
   # # do something in 10 days
   # end
   #
   # scheduler.at '2015/12/12 22:30:00' do
   # # do something at a given point in time
   # end
   #
   # scheduler.every '3h' do
   # # do something every 3 hours
   # end
   #
   # scheduler.cron '5 0 * * *' do
   # # do something every day, five minutes after midnight
   # # (see "crontab" documentation)
   # end
rescue => e
   STDERR.puts "An error occured: #{e}"
end


Skomentuj

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Log Out / Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Log Out / Zmień )

Facebook photo

Komentujesz korzystając z konta Facebook. Log Out / Zmień )

Google+ photo

Komentujesz korzystając z konta Google+. Log Out / Zmień )

Connecting to %s

TOMASZ WEŁNA

artysta grafik | wykładowca

PRACOWNIA OKO

Szkoła Rysunku Malarstwa i Grafiki DR TOMASZA WEŁNY | KRAKÓW | Plac Matejki 10 | tel 691 81 75 74

Piękno neurobiologii

Blog Jerzego Vetulaniego

Teoria muzyki, zasady muzyki, podstawy muzyki

Teoria muzyki, zasady muzyki, podstawy muzyki - czyli to co każdy amator muzyki wiedzieć powinien :)

Personal Development & Inspirations

Przemyślenia i refleksje, którymi warto się podzielić (blog by Janusz Nawrat)

Business IT Cooperation Platform

Biznes i IT - dwa światy, które muszą współdziałać

%d bloggers like this: