How to set up Scheduler add-on at Heroku - ruby-on-rails-3

I am accustomed from PHP to set up CRON on the URL address, that I want to run automatically in a time interval.
Now I try to set up the Schedular add-on at Heroku and I have a little problem - I created the file lib/tasks/scheduler.rake and in admin section on Heroku set up everything what is possible, but:
I am a bit confused, how it all works - for example, these lines are in lib/tasks/scheduler.rake:
desc "This task is called by the Heroku scheduler add-on"
task :update_feed => :environment do
puts "Updating feed..."
NewsFeed.update
puts "done."
end
task :send_reminders => :environment do
User.send_reminders
end
What mean the task task :update_feed? In the set up hour will be run this action? But this is action in which controller? For example, what if I would need to run every day the action my_action in the controller home? I should set up there again only my_action instead update_feed?

With a cron to call an http action, such as using curl or wget, you are scheduling an http request, and the http request then results in the php action running, and in that action's code you have some work/logic that occurs.
With heroku scheduler, you are skipping all the http request stuff and action stuff, and can put the logic/work into the rake task directly (or put it in a regular ruby class or model and invoke that from the task body).
This works because the rake task is loading up the full rails environment (the :environment dependency part of the task definition does this), so inside the rake task body, you have access to your rails app models, required gems, application configuration, everything - just like inside a controller or model class in rails.
What's also nice, if you are on cedar, is that the scheduler invokes tasks in a one-off dynamo instance, so your app's main dynamo is not occupied by a task run by the scheduler, which is not the case when you use the cron -> http request -> controller action pattern.
If you tell me more about what you are trying to do, I can give more specific advice, but in general I usually have the task's logic defined in a plain ruby class in the lib directory, or as a class method on a model, and that is what would be called from the task body (as in the example code you cite above).

Related

Asp.Net Core - Start background task and ensure only one are running

I would like to start a long running task from an API controller and return 200 when the task is started.
I want only one task running and if another request are coming in, the controller should check if there is already a task running.
It the task is running just forget the request. If not start a new task.
Was thinking of using this code fire and forget in ASP.NET Core with dependency alive to start the task. Then I need some thread safe place to store a IsRunning variable.
Have you checked Hangfire
?
In can be run in cluster mode and also you can query to check if a specific task in running.

GET Mapreduce Job Progress after Job finished

I'm developing a application that can collect mpreduce job progress info to analyze.The first way is parse log file.but It's ugly。Is there any method like hook or plugin can do this
You can probably use the YARN application API to get most of the information. See this Yarn Application API
Here is an excerpt from the page:
... All query parameters for this api will filter on all applications. However the queue query parameter will only implicitly filter on unfinished applications that are currently in the given queue.
There are other YARN APIs too, that you can utilize to achieve your goal. It is certainly better than scanning log files.

Find out the return code of the privileged help run through SMJobSubmit

Is there a way to know the return code or process ID of the process which gets executed when the privileged helper tool is installed as a launchdaemon and launched via SMJobSubmit().
I have an application which to execute some tasks in privileged manner uses the SMJobSubmit API as mentioned here.
Now in order to know whether the tasks succeeded or not, I will have to do one of the following.
The best option is to get the return code of the executable that ran.
Another option would be if I could create a pipe between my application and the launchd.
If the above two are not possible, I will have to resort to some hack like writing a file in /tmp location and reading it from my app.
I guess SMJobSubmit internally submits the executable with a launchdaemon dictionary to the launchd which is then responsible for its execution. So is there a way I could query launchd to find out the return code for the executable run with the label "mylabel".
There is no way to do this directly.
SMJobSubmit is a simple wrapper around a complicated task. It also returns synchronously despite launching a task asynchronously. So, while it can give you an error if it fails to submit the job, if it successfully submits a job that fails to run, there is no way to find that out.
So, you will have to explicitly write some code to communicate from your helper to your app, to report that it's up and running.
If you've already built some communication mechanism (signals, files, Unix or TCP sockets, JSON-RPC over HTTP, whatever), just use that.
If you're designing something from scratch, XPC may be the best answer. You can't use XPC to launch your helper (since it's privileged), but you can manually create a connection by registering a Mach service and calling xpc_connection_create_mach_service.

Rails application http request

When I run my Rails application in Apache using Passenger and open two browser log the request with thread id using log4r.
I see both the request uses same thread id. How is it possible?
If I do sleep in one request until sleep expire another request is blocked.
Where can I configure use different thread for each request or maxThreadCount?
Is it the behavior for development environment or in production too? how to overcome with this?
config.threadsafe!
put it in your production.rb or development.rb.
I have same problem when calling a local webservice inside a controller action.
Puma also has better concurrency, but that threadsafe confgi make webrick multi-thread for me.

whenever + delayed_job with cron job in starting worker

I am learning cron job and delayed job, and I want to send emails using background job; for that I'm using the delayed_job gem. I don't want to start the worker manually by running the rake jobs:work command, but I want to set this rake in cron job so whenever an user login into the dashboard this command is fired and a mail is sent to his address. Following is my code:
Sending mail method
def dashboard
#user = User.find(params[:id])
UserMailer.delay.initial_email(#user)
end
UserMailer
def initial_email(user)
#user = user
mail(:to => user.email,:subject => "Welcome to my website!")
end
For the cron job I am using "whenever" Gem, so what should I write in my schedule.rb file so that when I login into the dashboard I get a mail without running worker manually?
DelayedJob is supposed to be running all the time in the background so it doesn't need to be fired up.
The worker agent checks the queue to see if any tasks need to be performed, and runs them.
It's pretty much like a 2nd instance of your application that runs in the background checking for tasks that need to run.
So you should start the worker agent with script/delayed_job start and let it run all the time. You can use a separate tool like monit or god to monitoring your worker agent to make sure it is always running.