Resque not performing any methods - ruby-on-rails-3

I've got Resque set up in my Rails 3.2 app and have an after hook which successfully calls
Resque.enqueue(SomeJob, self.class.name, id)
I can see the job getting fired off, but no methods in my SomeJob class are getting executed. I've got a logger set up confirming the SomeJob gets executed but the log statement inside my self.perform block never gets called.
def self.perform
log.debug("working")
end
So far I've tried methods named self.work, work, self.perform, perform and nothing seems to get called. The Resque documentation seems to be geared towards a pending 2.0.0 release but I can't quite get this to work even with 1.24.1 or 1.22.
What is the magic method that gets called in Resque? Is there any way to explicitly call it in Resque.enqueue?

At first glance it looks like you're passing in two arguments (self.class.name and id), but the self.perform method isn't able to accept them, so it could be silently failing with an invalid argument error.
My suggestion would be to change the self.perform method to the following:
def self.perform(class_name, id)
log.debug("working: class_name=#{class_name} id=#{id}")
end

Related

Apollo - update() method getting called twice, both times with optimistic/fake data

I'm completely stuck on an Apollo problem, for which I've opened a GitHub issue and had zero response on.
I'm calling an Apollo mutation, using optimisticResponse. The way it's supposed to work, as I understand it, is that update() gets called twice: first with the optimistic data, then again with the actual data coming in from the network.
But for some reason, my code is not working like this. I'm getting two update() calls, both with the optimistic data.
Here's a repo that demonstrates this behavior: https://github.com/ffxsam/apollo-update-bug
yarn && yarn dev
Open in browser, open console
Enter some text and hit enter
Repeat above
Notice the error in the console about duplicate keys. This is happening because the temporary ID "??" is not being replaced with the real UUID
(optional) You can open Vue DevTools if available and inspect the data to see it's incorrect
I was doing some digging and I think I found the source of the problem.
Unfortunately, I don't have a solution.
In short, the problem might be with a network link called OfflineLink that is used by aws-appsync.
Explanation
aws-appsync has an ApolloLink called OfflineLink that intervenes with the request function.
What happens is something like this:
you call $apollo.mutate(...)
ApolloClient.QueryManager initializes the mutation that triggers your update the first time with the optimistic response. That is happening inside ApolloClient data store, markMutationInit calls markMutationResult that calls your update.
The graphql operation executes and reaches the OfflineLink in the network chain.
OfflineLink creates a new observer and dispatches the mutation info as an action.
The next line of OfflineLink calls the observer's next function with the optimisticResponse as if it was the execution result!
This triggers your update the second time with the result which is actually the optimisticResponse.
OfflineLink calls the observer's complete which resolves your promise.
console.log('done!'...
Meanwhile, OfflineLink prevents the original mutation from even sending the request, and a new mutation is generated and sent with the options you've given it.

If I mock an object and/or stub a method in my spec, will that mock be used even though it's only called indirectly?

I am new to mocking and stubbing, but I think I have a circumstance where their use would be ideal.
In my application, when a user saves a Product, an after_save callback fires that creates Publication instances which cause the product data to be sent to certain 3rd parties via API.
I have a request spec for Product that tests my CRUD operations.
If I stub either the API methods or mock the Publication model, will those mocks/stubs be used in my spec even though they are actually called in the Product after_save callback? I'm confused about this point.
Update
I figured I would just do it like this:
Publication.any_instance.stub(:publist).and_return(true)
And do that at the beginning of my test. That way whatever instance is created would be handled. Is that how it works?
Yes that stub will do what it says and the publist method on any instance of the publication class will always return true.
Instead of putting it "at the top" though do something like.
context 'when there is a publist' do
Publication.any_instance.stub(:publist).and_return(true)
it 'should ...' do
...
end
end
then if required you can do tests without the stub, or tests where publist returns false in other context blocks and be nice and clear in the spec.

Return result of asynchronous call to the method the called it

I'm writing a standard NSURLConnection class for the App I'm working on right now which I will hopefully be able to use subsequent apps as well.
The idea is to able to pass a URL and a parameters array to a method in this class that will start the connection and then have it return the result when it is done.
-(NSData*)go :(NSString*)url :(NSArray)params
Since I'm calling this from another class I'd like to be able to set the result to a variable in that calling class.
NSData *result = [[connect alloc]go:testurl :testparams];
The problem is that the result doesn't arrive right away so when I return the NSData I have set in the "go" method it is blank.
I've tried a few things like NSCondition and running a while loop on another thread in the go method to check if it was finished.
Unfortunately, and I did not know this beforehand, the asynchronous connections form NSURLConnections run on the same thread as my NSCondition in the "go" method ran on. Because of this when I locked up that method so it didn't return early, I also locked up my connection so it never reached it's completion callback.
How can I effectively pause my "go" method long enough for my connection to finish so I can return the data to anywhere in my app.
There's a chance I'm going about this the completely wrong way so please let me know if that is the case. It does need to work kind of like this though because multiple requests will be going out at the same time and I'd like different instances of this connection class to be able to control them all.

Method Interception, replace return value

We’re using Ninject.Extensions.Interception (LinFu if it matters) to do a few things and I want to know if its possible to return a value form the method being intercepted.
EG
A Call is made into one of our repository methods
Our Interceptor gets the BeforeInvoke event, we use this to look into the ASP.NET Cache to see if there is any relevant data
- Return the relevant data (this would cause the method to return immediately and NOT execute the body of the method
- Or Allow the method to run as per normal
Extra points if in the AfterInvoke method we take a peek at the data being returned and add it to the cache.
Has anybody done something similar before?
From your question I assume that you derive from SimpleInterceptor. This will not allow to return imediately. Instead you have to implement the Iinterceptor interface. You can decide to call the intercepted method by calling the Proceed method on the invocation or not.

NSubstitute Received() responding to multiple calls

I have an object that I've faked with NSubstitute that has a method on it that gets called twice. I'd like to verify that the method has actually been called twice (and only twice). I've poked around the docs and Google with no luck. Any help would be appreciated. Thanks.
This currently isn't supported in NSubstitute 1.2.1 (the feature is implemented in a branch, and will make it to next release).
An alternative for now is to use substitute.ReceivedCalls() which will return an enumerable you can query. Another option is to use When..Do to increment a counter whenever the method is called, and assert that the counter ends up at 2.
Update 2011-11-19: This is supported in NSubstitute 1.3.0, using Received(int). It is documented on the Checking received calls page.