source: subversion/sites/tile.openstreetmap.org/cat_tile.rb-no-instant @ 5396

Last change on this file since 5396 was 5386, checked in by jonb, 13 years ago

update tile cat_tile.rb script and provide some variants which are used if the machine is under heavy load (e.g. turning off instant rendering or access limit counting).

  • Property svn:executable set to *
File size: 3.1 KB
Line 
1require 'cgi'
2
3cgi = CGI.new
4
5module Foo
6  require 'mysql'
7  require 'date'
8  require 'time'
9  require 'singleton'
10
11  class Bar
12    def call_local_sql
13      dbh = nil
14      begin
15        dbh = Mysql.real_connect('localhost', 'tile', 'tile', 'tile')
16        sql = yield
17        res = dbh.query(sql)
18        return res #        if res.nil? then return true else return res end
19      rescue MysqlError =>ex
20        puts ex
21      ensure
22        dbh.close unless dbh.nil?
23      end
24      nil
25    end
26  end
27end
28
29#first check number of accesses
30
31include Apache
32ip = Apache.request.connection.remote_ip
33fb = Foo::Bar.new
34
35res = fb.call_local_sql { "select hits from access where ip='#{ip}'" }
36if res.nil?
37  exit
38end
39
40if res.num_rows == 0
41  fb.call_local_sql { "insert into access (ip,hits) values('#{ip}',1) ON DUPLICATE KEY UPDATE hits=hits+1;" }
42else
43  hits = 0
44  res.each_hash do |row|
45    hits = row['hits'].to_i
46  end
47  hits += 1
48
49  # User gets an error once they hit upper limit (and not even counted any more)
50  if hits > 75_000
51    sleep 3
52    cgi.header("status"=>"403 Your IP has downloaded too many tiles today. Please try again tomorrow or use planet.osm and render them yourself!")
53   exit
54  end
55
56  fb.call_local_sql { "update access set hits=#{hits} where ip='#{ip}'" }
57  if hits > 50_000
58    sleep 1
59    print IO.read("/home/www/tile/images/limit.png")
60    exit
61  end
62end
63
64#now send the tile
65x = cgi['x'].to_i
66y = cgi['y'].to_i
67z = cgi['z'].to_i
68
69if z and (z > 18 or z < 0)
70  exit
71end
72
73# valid x/y for tiles are 0 ... 2^zoom-1
74limit = (2 ** z) - 1
75
76if x and (x < 0 or x > limit)
77  print IO.read("/home/www/tile/images/blank-000000.png")
78  exit
79end
80
81if y and (y < 0 or y > limit)
82  print IO.read("/home/www/tile/images/blank-000000.png")
83  exit
84end
85
86
87#res = fb.call_local_sql { "select data, dirty_t, created_at from tiles where x = #{x} and y=#{y} and z=#{z} limit 1" }
88#if res.nil?
89#  exit
90#end
91#
92#if res.num_rows == 0
93#  fb.call_local_sql { "insert into tiles (x,y,z,dirty_t, created_at) values (#{x},#{y},#{z},'true',NOW())" }
94#
95#  res = fb.call_local_sql { "select count(dirty_t) as dirty from tiles where dirty_t='true'" }
96#  if res.nil?
97#    exit
98#  end
99#  res.each_hash do |row|
100#    if row['dirty'].to_i < 128
101#      render = IO.popen("/home/jburgess/live/render_from_list.py > /dev/null", "w+")
102#      render.puts "#{x} #{y} #{z}"
103#      render.close
104#    else
105#      exit
106#    end
107#  end
108#end
109
110res = fb.call_local_sql { "select data, dirty_t, created_at from tiles where x = #{x} and y=#{y} and z=#{z} limit 1" }
111if res.nil?
112  exit
113end
114if res.num_rows == 0
115  fb.call_local_sql { "insert into tiles (x,y,z,dirty_t, created_at) values (#{x},#{y},#{z},'true',NOW())" }
116  exit
117else
118  res.each_hash do |row|
119    created_at = Time.parse(row['created_at'])
120    expires_at = created_at + (60*60*24*3)
121    if row['dirty_t'] == 'true' or expires_at < Time.now
122      cgi.header("expires" => Time.now + (60*30))
123    else
124      cgi.header("expires" => expires_at)
125    end
126    cgi.header("image/png")
127    print row['data']
128    if row['dirty_t'] == 'false' and expires_at < Time.now
129      fb.call_local_sql { "update tiles set dirty_t = 'true' where x = #{x} and y=#{y} and z=#{z}" }
130    end
131  end
132end
133
Note: See TracBrowser for help on using the repository browser.