I've made a PWA with ReactJS + NodeJS, running with a python backend that's being spawned as a child process by the Node server, I have to send and generate a document generated by python via email 48 hours after payment verification is called, how do I accomplish this? My current method includes :
def sendInTwoDays(recipient, filename):
time = round(random.uniform(0.8, 2.0), 2)
time = round(time * 24 * 3600, 2)
time /= 10000 #for testing
print("[python:sendInTwoDays()] > Sleep ({} seconds) : {} days".format(time, time/3600))
sleep(time)
sendNow(recipient, filename)
I hate it since it uses "sleep()" and sleep on a webserver just doesn't sit right with me.
Please suggest a better way to accomplish the same on heroku.
I would suggest a scheduling(cron) approach with Heroku Scheduler like to check due emails every fifteen minutes. Rather than letting the process sit for 48 hours, I would save the email data along with the emailing time in the database and let the scheduler can invoke an API/task to call the Python script to output and email documents which are due after 48 hours.
The emails will not be sent out after exact 48 hours, but should be close.
Related
I am using Discord.js Node V12 I am currently trying to find out how to say time elapsed in the status to show how long the bot has been playing game or any activity. But i cannot find anyone who has asked or answered any of these questions.
#client.event
async def on_connect():
await client.change_presence(status=discord.Status.dnd,activity = discord.Game(name = "VALORANT"))
I would like to break this answer into a few significant points:
• The sample code provided is from discord.py ( a discontinued python based library to interact with the API ) which is totally out of context of the whole question itself since you're asking it for discord.js
• There is no actual way to find the time elapsed since a particular activity as of now but you may resort to this:
var uptime = client.uptime; // returns uptime in milliseconds
var hours = uptime / 1000 / 60 / 60 // converts it to hours
/*
Then you may apply the following approach to change your status every hour passes
*/
setInterval(() => {
client.user.setActivity(`Valorant since ${hours} hour(s)`);
}, 3600000); // this would show up as Playing Valorant since 1 hour(s) and subsequently would change every hour if your bot isn't being restarted continuously.
I took a look at the discord.js documentation to examine setting activities and found no such information about timestamps. However, there was a section in the ActivityType that led me to the discord developers documentation and it indicates:
Bots are only able to send name, type, and optionally url.
So it doesn't seem as though you will be able to set the start or end timestamps for an activity as a bot.
I am not using bot API. I am using Telegram API to send messages. Messages are being sent easily but the problem occurs after 19 users. On the 20th user, I receive PeerFloodError. Even after, searching a lot, I didn't find any specific limits and using sleep is not working either. Please suggest a way to overcome this problem.
Code
def send_message(root2, client):
totalcount = 0
for user in users:
if totalcount >= len(users):
root2.destroy()
break
if totalcount % 15 == 0 and totalcount != 0:
print("Waiting for one minute...")
time.sleep(60)
if user not in users2 or user not in users3:
totalcount += 1
entity = client.get_entity(user)
client.send_message(entity, message_str)
time.sleep(8)
most of the Telegram APIs have strict limits for each of 30-seconds, 30-minutes, 24-hours periods. spread 19(or less API calls in 30 minutes and catch whether it throws an error or not, if it's doing fine after 30minutes: Great! otherwise, do this process for 24 hours.)
note that for a bulk usage of Telegram APIs, you may need to use several accounts in your project.
I've been using NodeJS running on Firebase hosting to monitor sensor values
(through dedicated hardware). I simply use function setinterval to check
the sensor values via API that I wrote. The system reads from sensor
successfully for every minutes (for just testing) but it stops reading
after 9 minutes (or 540 seconds according to google document here ??). The reading sensor function is working, only the loop in setinterval that stops working as I've mentioned. My simple code looks like this:
var intervalObj = setinterval(function(sensor_id){
var val = readSensorValue(sensor_id);
var d = new Date();
console.log("reading from sensor[" + sensor_id + "] => " + val + "#" + d);
}, 60000);
However, I have to monitor the sensor all day long. How can I fix this??
Or there's any other better approach??
Thank you for your time,
JJ
:D:D:D
I've decided not to use Firebase function hosting for now, so I run the project on the VM and everything's been working fine. Just a simple setInterval... even though I'm still wonder whether it is a design by Firebase for such limitation or there should be other approach for doing this on Firebase.
Thanks for all the comment.
:D:D:D
I am sending mail to the users using actionmailer through postmark. This is my code in controller:
#users = User.where(some condition)
#product = Product.find_by_name(some name).first
for user in #users
UserMailer.new_product_arrival(user, #product, home_url).deliver
end
and this my user_mailer.rb
def new_product_arrival(user,product,home_url)
#from = Settings.mailer_from_address
#recipients = user.login
#sent_on = Time.now
#user = user
#product = product
#content_type = "text/html"
#home_url = home_url
end
The problem is that if there are more than 10 users it takes a very long time because of the for loop. I need to know if we can handle this by using multi-threading or background job. I don't want to use background job, but can anyone tell me how to implement the above using multi-threading.
I am using ruby 1.8.7 and rails 3.0.7
There basically two ways to wrap your loop in order to get "multi-threading":
Spwan a thread for each delivery and join them back to the main thread
threads = []
for user in #users
threads << Thread.new do
UserMailer.new_product_arrival(user, #product, home_url).deliver
end
end
threads.each(&:join)
fork over the entire rails app ( pretty messy but the rails app serving the request will respond immediately ) and have the process detached:
process = fork do
for user in #users
UserMailer.new_product_arrival(user, #product, home_url).deliver
end
Process.kill("HUP")
#sends the kill signal to current Process, which is the Rails App sending your emails
end
Process.detach(process)
Hope that helps
our developer Artem recently made a major update to the Postmark gem
which allows you to send emails easily in batches, which should allow you to send emails faster. Check it out.
Try delayed_job gem. This is a database-based background job gem. We used it in an e-commerce website, for example, sending order confirmation emails to users.
These tasks can happen asynchronously in the background, because your Rails app doesn't need
them executed immediately.
um im a rails student from nairobi dev school kenya ..and i think you can give this a try ,..soo what you are having there is the delayed response due to the number of users ..you can try long polling an example
poll = function (){
s.ajax{
url:/'chat.json'
data: { last_time: get last_time () }
}}.done(function(data) {
// handle data
setTimeout(poll,1000);
});
}
try that in your ow way ..this is useful for a real time application..o you can use even action controller:: live ..i think youre farmiliar with threading with rails .also .the above exmples will hep you ,,hopefuly
\
I'm developing a website (with Rails 3.1) where limited set of 'writers' are able to write post. 'Moderators' should accept (or decline) the post and schedule the publishing. Until this moment is the development process pretty basic.
There are two publish moments each day. Accepted posts will be placed in some kind of queue. Each day at 10:00am and 4:00pm the oldest accepted post must be published. However, I need also to be able to ** manually set** a date and time when the post going live.
What's the best way to achieve the result? Cron? Background Jobs?
So...
1) have an accepted_at field, which you can also set manually; it's the 'time to go live'.
2)
class Post
scope :ready_to_be_published, lambda{ where(['accepted_at<? and not published', Time.zone.now]).order('accepted_at ASC') }
def accept!(time_to_go_live = nil)
update_attributes!(:accepted_at => time_to_go_live || Time.zone.now)
end
end
3) have a whenever job at 10am and 4pm to run a rake task
task :publish_a_post => :environment do
Post.ready_to_be_published.first.update_attributes!(:published => true)
end