-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsendfax.rb
71 lines (70 loc) · 2.1 KB
/
sendfax.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require "rubygems"
require 'drb'
require 'gui/config/environment.rb'
begin
# ==========================
# = Create DRB connection =
# ==========================
v_pos="creating DRB connection"
MYLOGGER.info v_pos
@adhearsion = DRbObject.new_with_uri "druby://localhost:9050"
# ==========================
# = Find all pending faxes =
# ==========================
v_pos="Looking for pending faxes"
MYLOGGER.info v_pos
Fax.to_process.each do |fax|
MYLOGGER.info "Processing fax"
MYLOGGER.debug fax.to_yaml
# Check if we have a free FaxChannel
done=false
retry_counter=0
v_pos="searching for free fax Channel"
while !done
# get the first idle channel
@channel=FaxChannel.find_by_state("idle")
MYLOGGER.info "Found free channel #{@channel.to_yaml}"
v_pos="Assigning channel to fax"
MYLOGGER.info v_pos
fax.update_attribute(:fax_channel_id, @channel.id)
#change the state
v_pos="Changing channel state"
MYLOGGER.info v_pos
@channel.sending!
done=true
else
MYLOGGER.info "No free channel found, sleeping (#{retry_counter})"
sleep 20
retry_counter+=1
if retry_counter > 5
v_pos="All fax channels busy, giving up"
MYLOGGER.info v_pos
raise "AllChannelsBusy"
end
end
end
# Todo: add some number formating here
v_pos="Originating Call to #{fax.dest}"
MYLOGGER.info v_pos
call_number="SIP/lcx/#{fax.dest}"
callerid="#{fax.localstationid}"
result = @adhearsion.originate({ :channel => "#{call_number}",
:context => "sendfax",
:extension => 's',
:priority => 1,
:callerid => "#{callerid}",
:async => false,
:variable => "fax_id=#{fax.id}" })
# not sure what to do with the result, just log it
puts "Done"
MYLOGGER.info "DONE"
# puts result.to_yaml
end
rescue Exception => e
MYLOGGER.info "*"*50
MYLOGGER.info "We crashed during #{v_pos} / #{e.message}"
MYLOGGER.info e.backtrace.inspect
MYLOGGER.info "*"*50
#raise
end