unable to get the item added in the queue from rest service  Topic is solved

Discussions relating to the REST API of Jiwa 7.

unable to get the item added in the queue from rest service

Postby symimake » Tue Aug 30, 2022 5:45 pm

Hi,

I created an endpoint to get inventory from 3rd party app in the custom REST route. I created one concurrent queue in ProcessInventory class and from endpoint I am adding items in it. On the other hand in ProcessInventory I created a while loop to process the inventory when ever its get item in the queue.

The problem is that the items are adding in the queue but when I try to dequeue item from queue in the while loop the queue does not contain that item in it and shows as empty. I putted the gethashcode of queue when enqueue and dequeue and got different hash code. I am not getting why its showing different hash code for the same queue object.

I putted the plugin code as well in the attachments
Attachments
Plugin TestListner.xml
Plugin
(34.22 KiB) Downloaded 121 times
symimake
Occasional Contributor
Occasional Contributor
 
Posts: 16
Joined: Thu Jul 28, 2022 3:38 pm

Re: unable to get the item added in the queue from rest serv  Topic is solved

Postby Mike.Sheen » Tue Aug 30, 2022 6:08 pm

You're dequeing in the ScheduledExecutionPlugin class which is used by the JiwaPluginScheduler service which is a different process to which the REST API Self Hosted service runs in. So, that means your static InventoryProcess class and CustomInventory property are different instances - you can't communicate across process boundaries using statics.

You'll need to devise another way for the JiwaPluginScheduler service to be able to access the queued items added by the REST API, or you could dequeue inside the REST API service instead.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: unable to get the item added in the queue from rest serv

Postby symimake » Tue Aug 30, 2022 6:15 pm

So how can I achieve that?

if put it into sheduler service then it run periodically and I want to run it continuously without interrupting Jiwa main thread. Can you suggest anything to tackle this kind of scenario?
symimake
Occasional Contributor
Occasional Contributor
 
Posts: 16
Joined: Thu Jul 28, 2022 3:38 pm

Re: unable to get the item added in the queue from rest serv

Postby Mike.Sheen » Tue Aug 30, 2022 6:24 pm

symimake wrote:So how can I achieve that?

if put it into sheduler service then it run periodically and I want to run it continuously without interrupting Jiwa main thread. Can you suggest anything to tackle this kind of scenario?


Design of systems is beyond the scope of what these forums are for - if you want to engage our services we have a lot of experience in designing and building systems, particularly integrations with Jiwa. You can do that by lodging a ticket at https://helpdesk.jiwa.com.au

But, to throw you a bone - the simplest way forward for what you have in place already is:

1. Keep the ScheduledExecutionPlugin as the thing which runs on a periodic schedule (just move your code out of the OnServiceStart method and put it into the Execute method - and that code will change - see point 3)
2. Add a route to the API to dequeue - you can make this internal as it's not supposed publicly accessible - easiest way is in the service handling the request to dequeue, check for a secret in the request header
3. In the Execute of the ScheduledExecutionPlugin, call the internal route defined in #2 (set secret key header if that's how you plan internal use only enforcement) - just like you'd call any external route - I recommend using the HttpUtils string extensions - so it's really as easy as "http://localhost/internal/inventory/dequeue".GetBytesFromUrl() - you can GetStringFromUrl or PostToUrl as well - see the ServiceStack docs on HttpUtils.

This is a back-of-a-napkin design - I don't know what your environment constraints or anything like that are, so take my suggestion without warranty.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756

Re: unable to get the item added in the queue from rest serv

Postby symimake » Tue Aug 30, 2022 6:40 pm

You said to put the onservice start code in the scheduler Execute method. The execute method also doing some other scheduled tasks so can I run it for once?

By creating another scheduler and create one flag for that. If it is already true don't run thread again?

or is there another way to do that without adding another scheduler task?
symimake
Occasional Contributor
Occasional Contributor
 
Posts: 16
Joined: Thu Jul 28, 2022 3:38 pm

Re: unable to get the item added in the queue from rest serv

Postby SBarnes » Tue Aug 30, 2022 8:38 pm

If I am understanding where you are eventually going with this you are trying to catch a web hook from shopify and then do something with it inventory wise, some things you might need to consider in your "napkin design"

1. You should make sure you are returning a 200 OK
2. I would suggest you might be better persisting the data to a database for later processing as with your method if the process goes down or there is a server reboot there is the danger of losing data.

My advice like Mike's is you might be better getting a quote from Jiwa on this as they have extensive experience with this sort of thing.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: unable to get the item added in the queue from rest serv

Postby Mike.Sheen » Wed Aug 31, 2022 1:06 am

symimake wrote:You said to put the onservice start code in the scheduler Execute method. The execute method also doing some other scheduled tasks so can I run it for once?


The OnServiceStart method is invoked once, at start of the service. The Execute method is run as per the defined schedules of the plugin. I assumed you put your processing of your queue in OnServiceStart because you didn't understand how the service worked - but now after looking again I see you use it to bootstrap a loop which continually dequeues items from your queue.

Keep that strategy if you like, but have the loop simply call an internal route on the API which does the dequeue.

symimake wrote:By creating another scheduler and create one flag for that. If it is already true don't run thread again?

Not sure what you're asking here.

symimake wrote:or is there another way to do that without adding another scheduler task?

Yes, probably - but I don't know what it is you are trying to do. By looking at your code and what you've named things I can guess a little, but I don't have the full story. I've worked out you want a webhook receiver for a Shopify store, and you're going to great lengths to have that receiver simply enqueue items and not perform processing inside the service handling the request - so I'm guessing that Shopify is sensitive to a but of latency in the webhooks and you need to accept and respond to those webhooks as soon as possible. Or maybe there are other reasons for your current design.
Mike Sheen
Chief Software Engineer
Jiwa Financials

If I do answer your question to your satisfaction, please mark it as the post solving the topic so others with the same issue can readily identify the solution
User avatar
Mike.Sheen
Overflow Error
Overflow Error
 
Posts: 2444
Joined: Tue Feb 12, 2008 11:12 am
Location: Perth, Republic of Western Australia
Topics Solved: 756


Return to REST API

Who is online

Users browsing this forum: No registered users and 4 guests