Action Mailer not executing Proc to generate TO field - ruby-on-rails-4

UP 04/08/2015 : Is it actually possible to use both .deliver_later and a proc ?
My problem :
My email doesn't execute the procedure to generate the TO field, thus sending a bad email on postfix
Postfix log
sudo tail -n 150 /var/log/mail.log
Jul 30 17:39:44 je postfix/pickup[3974]: 0DA531FC2AFA: uid=1030 from=<candidature#myorg.com>
Jul 30 17:39:44 je postfix/cleanup[8430]: 0DA531FC2AFA: message-id=<55ba453fc4fd1_20e02375a9c04882a#myorg.mail>
Jul 30 17:39:44 je postfix/qmgr[3506]: 0DA531FC2AFA: from=<candidature#telecom-etude.com>, size=18915, nrcpt=2 (queue active)
Jul 30 17:39:44 je postfix/error[8522]: 0DA531FC2AFA: to=<Proc:0xbbd2989c#/var/www/intranet_rails_production/releases/20150729170507/app/mailers/mailing_lists_mailer.rb:42>, relay=none, delay=0.41, delays=0.22/0.02/0/0.17, dsn=5.1.3, status=bounced (bad address syntax)
Jul 30 17:39:44 je postfix/error[8522]: 0DA531FC2AFA: to=<#<Proc:0xbbd2989c#/var/www/intranet_rails_production/releases/20150729170507/app/mailers/mailing_lists_mailer.rb:42>>, relay=none, delay=0.52, delays=0.22/0.02/0/0.28, dsn=5.1.3, status=bounced (bad address syntax)
Controller
...
if Rails.env.production? # Using :sendmail as delivery method
MailingListsMailer.action(#mail).deliver_later
else # Typically using :letter_opener or :file as delivery method
MailingListsMailer.action(#mail).deliver_now
...
Mailer
class MailingListsMailer < ActionMailer::Base
def action(message)
format_mail_params(message)
mail(
to: Proc.new {read_emails_file},
...
)
end
private
def read_emails_file
File.read('emails.txt').split('\n')
end
end
Config
config.action_mailer.delivery_method = :sendmail
config.action_mailer.smtp_settings = {
:address => "localhost",
:domain => "myorg.fr"
}
config.action_mailer.default_url_options = { host: 'myorg.fr', protocol: 'https' }
EDIT : I was using a proc as suggested in the ActionMailer Basics #2.3.3

You probably don't need the Proc wrapper at all, try this:
def action(message)
format_mail_params(message)
mail(
to: read_emails_file,
...
)
end
To debug it, do something like this:
def action(message)
format_mail_params(message)
puts "*"*50
emails = read_emails_file
puts emails
puts "*"*50
mail(
to: emails,
...
)
end
Then check the server logs for the message that looks something like:
*******************************************************************
[...]
*******************************************************************
Iterate the Emails
Because you don't want to divulge everyone's email, and avoid spam detection, you may want to iterate the emails array:
def action(message)
format_mail_params(message)
read_emails_file.each do |email|
mail(
to: email,
...
)
end
end

Related

How do I fix a program that runs properly on a local server, but does not run after deploying to AWS?

I am relatively knew to programming, especially in regards to the problem I am facing in regards to running post requests through amazon web services and use of API requests.
I currently have a program written below.
from chalice import Chalice
import requests, json
import alpaca_trade_api as tradeapi
app = Chalice(app_name='tradingview-webhook-alerts')
API_KEY = 'API'
SECRET_KEY = 'SECRET'
BASE_URL = "https://paper-api.alpaca.markets"
ORDERS_URL = "{}/v2/orders".format(BASE_URL)
HEADERS = {'APCA-API-KEY-ID': API_KEY, 'APCA-API-SECRET-KEY': SECRET_KEY}
#app.route('/GDX',methods=['POST'])
def GDX():
request = app.current_request
webhook_message = request.json_body
p = 1-(webhook_message['close'] / webhook_message['high'])
if p<.0175: #if the high price for the 15m candle is 3% higher than the close, thee excution will not occur
data = {
"symbol": webhook_message['ticker'], #want it to access whatever the payload message is, payload message I believe is what the alert from Trading_View will send
"qty": 8,
"side": "buy",
"type": "limit",
"limit_price": webhook_message['close'],
"time_in_force": "gtc",
"order_class": "bracket",
#need to find out the average max profit per trade
"take_profit": {
"limit_price": webhook_message['close'] * 1.0085 #take 0.6%% profit
},
"stop_loss": {
"stop_price": webhook_message['close'] * 0.95, #stop loss of 6%
"limit_price": webhook_message['close'] * 0.93
}
}
r = requests.post(ORDERS_URL, json=data, headers=HEADERS)
response = json.loads(r.content)
print(response)
print(p)
return {
'message': 'I bought the stock!',
'webhook_message': webhook_message
}
else:
return{
'message': 'stock not purchased',
'webhook_message': webhook_message
}
#app.route('/buy_SLV',methods=['POST'])
def buy_stock():
request = app.current_request
webhook_message = request.json_body
data = {
"symbol": webhook_message['ticker'], #want it to access whatever the payload message is, payload message I believe is what the alert from Trading_View will send
"qty": 4,
"side": "buy",
"type": "limit",
"limit_price": webhook_message['close'],
"time_in_force": "gtc",
"order_class": "bracket",
"take_profit": {
"limit_price": webhook_message['close'] * 1.008 #take 1% profit
},
"stop_loss": {
"stop_price": webhook_message['close'] * 0.95, #stop loss of 2%
"limit_price": webhook_message['close'] * 0.94
}
}
r = requests.post(ORDERS_URL, json=data, headers=HEADERS)
response = json.loads(r.content)
print(response)
print(response.keys())
return {
'message': 'I bought the stock!',
'webhook_message': webhook_message
}
#app.route('/GDX_UpperBB',methods=['POST'])
def GDX_UpperBB():
request = app.current_request
webhook_message = request.json_body
api = tradeapi.REST(API_KEY, SECRET_KEY, base_url=BASE_URL)
ids = []
orders = api.list_orders(
limit=100,
nested=True # show nested multi-leg orders
)
GDX_orders = [o for o in orders if o.symbol == 'GDX']
for i in GDX_orders:
ids.append(i.id)
if len(orders)>0:
print(ids)
for i in ids:
api.cancel_order(i)
else:
print('there are no orders')
return{
'message': 'I bought the stock!',
'webhook_message': webhook_message
}
the first two methods have been running fine (both through AWS and on local server). The third method (GDX_UpperBB) is what causes everything to stop working. When I run the program on a local server and make a call to the GDX_UpperBB method, it executes without issue. but when I deploy the program through the amazon web service API through chalice, I get a 502 BadGateway response with a "message": "Internal server error" throwback.
When I go into AWS and test the method this is the console response that I get (I removed about the first half of the response because it was long and everything said it ran successfully)
Mon Oct 26 00:46:51 UTC 2020 : Received response. Status: 200, Integration latency: 15 ms
Mon Oct 26 00:46:51 UTC 2020 : Endpoint response headers: {Date=Mon, 26 Oct 2020 00:46:51 GMT, Content-Type=application/json, Content-Length=127, Connection=keep-alive, x-amzn-RequestId=621b56f9-6bee-43af-8fe2-7f2cbeb7420e, X-Amz-Function-Error=Unhandled, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-5f961c7b-0a211c4e04be837554d0857f;sampled=0}
Mon Oct 26 00:46:51 UTC 2020 : Endpoint response body before transformations: {"errorMessage": "Unable to import module 'app': No module named 'alpaca_trade_api'", "errorType": "Runtime.ImportModuleError"}
Mon Oct 26 00:46:51 UTC 2020 : Lambda execution failed with status 200 due to customer function error: Unable to import module 'app': No module named 'alpaca_trade_api'. Lambda request id: 621b56f9-6bee-43af-8fe2-7f2cbeb7420e
Mon Oct 26 00:46:51 UTC 2020 : Method completed with status: 502
all help is appreciated.
You need to either add the dependency to your requirements.txt filer or add the code to a vendor directory off your main project directory

How to send a long url with get method to server

I have want to send a long url with GET, and I also need to add the url in the GET link,
like this(React):
let urlParam = {
apply_user: this.state.apply_user,
order_id: this.state.order_id,
begin_date: this.state.begin_date,
end_date: this.state.end_date,
status: this.state.status,
execute_mode: this.state.execute_mode,
page: page,
pageSize: this.state.pageSize,
otype:'all',
}
history.pushState(null, null, '?'+concatURLParams(urlParam))
then I can get all message from the url, like this :
http://cmdb.server.com/page/machine/list/?page=1&ips=172.17.10.3%20172.17.10.4%20172.17.10.9%20172.17.10.10
but in fact, the param is so long , there are lots of ip need to send.
And I use uwsgi to run my django project, I set buffer-size to 65536. official doc tell me the max size is 64k, but it got this, too.
invalid uwsgi request (current strsize: 21600). skip.
[pid: 15947|app: -1|req: -1/7] () {0 vars in 31 bytes} [Mon Jan 14 11:18:52 2019] => generated 0 bytes in 0 msecs ( 500) 0 headers in 0 bytes (0 switches on core 0)
this is my uwsgi.ini:
[uwsgi]
socket=127.0.0.1:9090
chdir=/home/ops/cmdb_futu/jumpserver
module=jumpserver.wsgi
master=true
buffer-size=65536
vacuum=true
processes=8
max-requests=2000
chmod-socket=664
vacuum=true
pidfile=uwsgi.pid
and I set nginx large_client_header_buffers and client_header_buffer_size to 64k, too.
But I don't make it for a long time. someone know why and help me.

groovyx.net.http.ResponseParseException while invoking web service from Spock groovy. Works fine from Postman or other clients

I want to consume a webservice and add assertions to its response. I get the following exception:
groovyx.net.http.ResponseParseException:
at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:495)
......
at Subscription.Order Products(Subscription.groovy:14)
Caused by: groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object
The current character read is 'I' with an int value of 73
Unable to determine the current character, it is not a string, number, array, or object
line number 1
index number 0
Invalid UTF-8 middle byte 0x52
My operation is as follows:
def setupSpec() {
client = new RESTClient("http://tsi-services-dev2.canaldigital.com:9080/test/webgw-dealer/v1/");
client.handler.failure = { resp, data -> return resp }
}
///// orderProductPayload is a variable which has an input
def "Order Products"() {
when:
def resp = client.post(path: "order/orderProduct", requestContentType: JSON, contentType: JSON, body: orderProductPayload) as HttpResponseDecorator
then:
println("response: " + resp.data)
resp.status == successResponseStatus
}
I have other such tests too which work fine and this particular test with the input payload entered works fine from Postman
Here is my response headers for this operation which fails in spock/groovy:
Content-Length →159
Content-Type →application/json
Date →Wed, 05 Apr 2017 06:19:16 GMT
X-Correlation-ID → xxxxx
Response header for another opertion which is working:
Connection →close
Content-Length →273
Content-Type →application/json
Date →Wed, 05 Apr 2017 06:16:41 GMT
X-Correlation-ID → xxxxx
Thanks alot for the help turns out there was a special character such as Ø in my request which the service didnt like and hence was responding in such an absurd way. Works fine now

TCL_REGEXP::How to grep 5 diifferent words from a variable using TCL regexp. And how to send greeped output to each column of excel sheet?

My TCL script:
set line {
Jul 24 21:06:40 2014: %AUTH-6-INFO: login[1765]: user 'admin' on 'pts/1' logged
Jul 24 21:05:15 2014: %DATAPLANE-5-: Unrecognized HTTP URL www.58.net. Flow: 0x2
Jul 24 21:04:39 2014: %DATAPLANE-5-: Unrecognized HTTP URL static.58.com. Flow:
Jul 24 21:04:38 2014: %DATAPLANE-5-: Unrecognized HTTP URL www.google-analytics.
com. Flow: 0x2265394048.
Jul 24 21:04:36 2014: %DATAPLANE-5-: Unrecognized HTTP URL track.58.co.in. Flow: 0
Jul 24 21:04:38 2014: %DATAPLANE-5-:Unrecognized HTTP URL www.google.co.in. Flow: 0x87078800
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Client Hello ServerName www.google.co.in. Flow: 0x87073880. len_analyzed: 183
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Server Hello ServerName test1. Flow: 0x87073880, len_analyzed 99
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Server Cert CommonName *.google.com. Flow: 0x87073880
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Searching rname(TYPE_A) cs50.wac.edgecastcdn.net in dns_hash_table
Jul 24 21:04:38 2014: %DATAPLANE-5-:Unrecognized HTTP URL www.facebook.com. Flow: 0x87078800
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Client Hello ServerName www.fb.com. Flow: 0x87073880. len_analyzed: 183
Jul 24 21:05:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Server Hello ServerName test. Flow: 0x87073880, len_analyzed 99
Jul 24 21:04:38 2014: %DATAPLANE-5-:CCB:44:Unrecognized Server Cert CommonName *.facebook.com. Flow: 0x87073880
Jul 24 21:05:39 2014: %DATAPLANE-5-:CCB:44:Searching rname(TYPE_A) cs50.wac.facebook.net in dns_hash_table
}
set urls [list]
foreach item [regexp -all -inline {URL\s+\S+} $line] {
lappend urls [lindex $item 1]
}
#puts $res
set s "*****************************************************"
set f {}
set f [open output.txt a]
if {$f ne {}} {
foreach url $urls {
chan puts $f $url
}
chan puts $f $s
chan close $f
}
My Requirement:
REQ 1. I need to grep the following things from $line variable.
URL www.58.net
Client Hello ServerName www.google.co.in.
Server Hello ServerName test1
Server Cert CommonName *.google.com.
rname(TYPE_A) cs50.wac.edgecastcdn.net
URL, Client Hello ServerName, Server Hello ServerName, Server Cert CommonName, rname are the common fields. Need to grep the words whatever appearing after that as shown above.
REQ 2. When I browse a URL, Iam getting the contents of $line. When I open a URL, my script should automatically grep the above things, and store in MS Excel file.There should be 5 columns in excel sheet each for one field. When "URL" found in a $line, it should Go and sit into column 1 of excel sheet. When "Client Hello ServerName" found, it should be moved to column 2 of excel sheet. Like this I want to upload all 5 datas to excel sheet.
Using my script provided above, I am able to grep URL's and able to upload into an .txt file.
Please guide me your ideas. Thanks a lot in advance.
Thanks,
Balu P.
Like most RE engines, Tcl's allows alternation through the use of the | operator. This lets you do:
# This is using expanded syntax
foreach {whole type payload} [regexp -all -inline {(?x)
\y ( URL
| (?: Client | Server)[ ]Hello[ ]ServerName
| Server[ ]Cert[ ]CommonName
| rname\(TYPE_A\) )
\s+ (\S+)
} $line] {
puts "type = $type"
puts "payload = [string trimright $payload .]"
}
(The tricky bits: \y means “word boundary”, and real spaces have to be written as [ ] because of expanded mode swallowing whitespace otherwise.)
When I try with your data, I get this output (two output lines per matched input line):
type = URL
payload = www.58.net
type = URL
payload = static.58.com
type = URL
payload = www.google-analytics
type = URL
payload = track.58.co.in
type = URL
payload = www.google.co.in
type = Client Hello ServerName
payload = www.google.co.in
type = Server Hello ServerName
payload = test1
type = Server Cert CommonName
payload = *.google.com
type = rname(TYPE_A)
payload = cs50.wac.edgecastcdn.net
type = URL
payload = www.facebook.com
type = Client Hello ServerName
payload = www.fb.com
type = Server Hello ServerName
payload = test
type = Server Cert CommonName
payload = *.facebook.com
type = rname(TYPE_A)
payload = cs50.wac.facebook.net
I don't know if this is exactly what you want, but it's very close.
For the second question, you need to either generate a CSV file (Tcl's got a package for that in the community library, tcllib) or to use COM to talk to Excel and manipulate things in there directly (the Tcom package is the generally recommended approach there). Which is best will depend on factors that you are not telling us; you should ask that as a separate question while explaining what the situation is (e.g., is there an existing spreadsheet or will the spreadsheet be created de novo.)
you can write a procedure & pass the type as filename & payload as data to be written. I have wrote one below.
proc type2file {filename payload} {
set name [string trim $filename].txt
set fp [open $name a]
puts $fp $payload
close $fp
}
call this inside for loop. Please let me know if it works for you.

Filter SIP Messages from log file

I work with VoIP solutions as a day to day job. I often get SIP messages debugs from devices I manage. Most of the times however, those logs contain more calls than I require to analyze, so it would be great if I could filter them out.
I want a tool that can, if I give it a log file and the Call-Id's I need, filter the log file to include only those SIP messages.
Unfortunately, SIP messages are more than one line, so my experience with grep is not sufficient to get it to work.
I started to program something in Perl for this, but any further than checking if I had the proper amount of parameters I didn't get. Is Perl the best language to go about this? I have included a part of the input here:
Jan 28 11:39:37.525 CET: //1393628/D5CC0586A87B/SIP/Msg/ccsipDisplayMsg:
Received:
SIP/2.0 200 OK
Via: SIP/2.0/UDP 10.218.16.2:5060;branch=z9hG4bKB22001ED5
From: "Frankeerapparaat Secretariaat" <sip:089653717#10.210.2.49>;tag=E7E0EF64-192F
To: <sip:022046187#10.210.2.49>;tag=25079324~19cc0abf-61d9-407f-a138-96eaffee1467-27521338
Date: Mon, 28 Jan 2013 10:39:32 GMT
Call-ID: D5CCA1AE-686D11E2-A881ED01-8DFA6D70#10.218.16.2
CSeq: 102 INVITE
Allow: INVITE, OPTIONS, INFO, BYE, CANCEL, ACK, PRACK, UPDATE, REFER, SUBSCRIBE, NOTIFY
Allow-Events: presence
Supported: replaces
Supported: X-cisco-srtp-fallback
Supported: Geolocation
Session-Expires: 1800;refresher=uas
Require: timer
P-Preferred-Identity: <sip:022046187#10.210.2.49>
Remote-Party-ID: <sip:022046187#10.210.2.49>;party=called;screen=no;privacy=off
Contact: <sip:022046187#10.210.2.49:5060>
Content-Type: application/sdp
Content-Length: 209
v=0
o=CiscoSystemsCCM-SIP 2000 1 IN IP4 10.210.2.49
s=SIP Call
c=IN IP4 10.210.2.1
t=0 0
m=audio 16844 RTP/AVP 8 101
a=rtpmap:8 PCMA/8000
a=ptime:20
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
Jan 28 11:39:37.529 CET: //1393628/D5CC0586A87B/SIP/Msg/ccsipDisplayMsg:
Sent:
ACK sip:022046187#10.210.2.49:5060 SIP/2.0
Via: SIP/2.0/UDP 10.218.16.2:5060;branch=z9hG4bKB2247150A
From: "Frankeerapparaat Secretariaat" <sip:089653717#10.210.2.49>;tag=E7E0EF64-192F
To: <sip:022046187#10.210.2.49>;tag=25079324~19cc0abf-61d9-407f-a138-96eaffee1467-27521338
Date: Mon, 28 Jan 2013 10:39:36 GMT
Call-ID: D5CCA1AE-686D11E2-A881ED01-8DFA6D70#10.218.16.2
Max-Forwards: 70
CSeq: 102 ACK
Authorization: Digest username="Genk_AC_1",realm="infraxnet.be",uri="sip:022046187#10.210.2.49:5060",response="9546733290a96d1470cfe29a7500c488",nonce="5V/Jt8FHd5I8uaoahshiaUud8O6UujJJ",algorithm=MD5
Allow-Events: telephone-event
Content-Length: 0
Jan 28 11:39:37.529 CET: //1393627/D5CC0586A87B/SIP/Msg/ccsipDisplayMsg:
Sent:
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.8.11:5060;branch=z9hG4bK24ecaaaa6dbd3
From: "Frankeerapparaat Secretariaat" <sip:3717#192.168.8.11>;tag=e206cc93-1791-457a-aaac-1541296cf17c-29093746
To: <sip:022046187#192.168.8.28>;tag=E7E0F8A4-EA3
Date: Mon, 28 Jan 2013 10:39:32 GMT
Call-ID: fedc8f80-10615564-45df0-b08a8c0#192.168.8.11
CSeq: 101 INVITE
Allow: INVITE, OPTIONS, BYE, CANCEL, ACK, PRACK, UPDATE, REFER, SUBSCRIBE, NOTIFY, INFO, REGISTER
Allow-Events: telephone-event
Remote-Party-ID: <sip:022046187#192.168.8.28>;party=called;screen=no;privacy=off
Contact: <sip:022046187#192.168.8.28:5060>
Supported: replaces
Supported: sdp-anat
Server: Cisco-SIPGateway/IOS-15.3.1.T
Session-Expires: 1800;refresher=uas
Require: timer
Supported: timer
Content-Type: application/sdp
Content-Disposition: session;handling=required
Content-Length: 247
v=0
o=CiscoSystemsSIP-GW-UserAgent 7276 9141 IN IP4 192.168.8.28
s=SIP Call
c=IN IP4 192.168.8.28
t=0 0
m=audio 30134 RTP/AVP 8 101
c=IN IP4 192.168.8.28
a=rtpmap:8 PCMA/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
a=ptime:20
The program I envision would take 2 or more arguments: the log file and then any amount of Call-ID's of calls I am interested in. It would then filter out only the relevant messages and print it to stdout.
Note that a single SIP message may include a blank line. The next message starts only when a new timestamp is shown.
Perhaps the following will be helpful:
use strict;
use warnings;
my %callIDs = map { $_ => 1 } splice #ARGV, 1, #ARGV - 1;
my $recordPrinted;
local $/ = '';
while (<>) {
if ( /Call-ID:\s+(.+)/ and $callIDs{$1} ) {
$recordPrinted = 1;
print;
next;
}
print if $recordPrinted and /\brtpmap\b/;
$recordPrinted = 0;
}
Usage: perl scriptName.pl logFile callID [callID]
When you send a script parameters, they end up in #ARGV. The splice takes (and removes) the elements from 1 on to build a hash's keys. The zeroth element is the log file name.
local $/ = ''; sets paragraph mode, i.e., a whole chunk of text separated by blank lines is read at a time. The regex captures the Call-ID and if a key exists for that ID, the line is printed. Also, a flag is set to indicate that a 'record' has been printed, since there may be another 'chunk' to that record.
If a record's been printed and a field is found that occurs in a second part of the record (rtpmap), that chunk is printed.
Try this
if ($subject =~ m/(?im)(Call-ID: (.+))$/) {
$result = $2;
} else {
$result = "";
}