Archive

Archive for November, 2022

Running a scheduled task on #AWS – A workaround for the 15 minute #Lambda limit

Let’s start with a bit of context. You want to run a process every day that could last 30 minutes, perhaps you’re importing data from an external source to a database. You can’t use Lambda, because the limit is 15 minutes, and it’s wasteful to use an EC2 instance because for 99% of the time the server will sit idle.

So, containers to the rescue. This is where you firstly write your code, and containerize it. Make sure it runs locally, as expected. Then you push the docker image to ECR. I followed most of the steps in this blog post:

https://towardsdatascience.com/deploying-a-docker-container-with-ecs-and-fargate-7b0cbc9cd608

The difference being, in my case, the application had a finite run time – i.e. do a task, then exit (not a web server).

Which means, that after I created my task definition, I then went to the Scheduled Task tab as shown below;

Where I could create a scheduled task that I can set to run every day, or minute, or whatever you need (the minium is 1 minute).

The container will run until it has finished it’s workload, so there is no 15 minute limit, like on AWS lambda.

Categories: Uncategorized

Format prices correctly in #MSSQL using currencies in #ISO4127 format.

If you want to specify a price including a currency, you can always write EUR 1,234 or USD 1,234, but it’s more concise to write $1,234 or €1,234, and users expect that format. You can always handle this at application layer, but here’s how to do it at Database layer.

Here, I’ve created a table of the currencies I needed, – it’s not complete, and I hope that someone can post a link to a complete table.

create table CurrencyLocales
( 
 id int identity(1,1),
 currency varchar(3),
 locale varchar(5)
)


insert into CurrencyLocales (currency,locale) values ('AUD','en-AU')
insert into CurrencyLocales (currency,locale) values ('BRL','pt-BR')
insert into CurrencyLocales (currency,locale) values ('CAD','en-CA')
insert into CurrencyLocales (currency,locale) values ('CZK','cs-CZ')
insert into CurrencyLocales (currency,locale) values ('DKK','da-DK')
insert into CurrencyLocales (currency,locale) values ('EUR','en-IE')
insert into CurrencyLocales (currency,locale) values ('GBP','en-GB')
insert into CurrencyLocales (currency,locale) values ('HUF','hu-HU')
insert into CurrencyLocales (currency,locale) values ('MXN','es-MX')
insert into CurrencyLocales (currency,locale) values ('MYR','en-MY')
insert into CurrencyLocales (currency,locale) values ('NOK','nb-NO')
insert into CurrencyLocales (currency,locale) values ('NZD','en-NZ')
insert into CurrencyLocales (currency,locale) values ('RUB','ru-RU')
insert into CurrencyLocales (currency,locale) values ('SEK','sv-SE')
insert into CurrencyLocales (currency,locale) values ('SGD','en-SG')
insert into CurrencyLocales (currency,locale) values ('USD','en-US')

create index idxCurrency on CurrencyLocales(currency)

-- example
select *, FORMAT(1234, 'C', locale) as formattedPrice  from CurrencyLocales
idCurrencyLocaleFormatted
1AUDen-AU$1,234.00
2BRLpt-BRR$1.234,00
3CADen-CA$1,234.00
4CZKcs-CZ1 234,00 Kč
5DKKda-DK1.234,00 kr.
6EURen-IE€1,234.00
7GBPen-GB£1,234.00
8HUFhu-HU1 234,00 Ft
9MXNes-MX$1,234.00
10MYRen-MYRM1,234.00
11NOKnb-NOkr 1 234,00
12NZDen-NZ$1,234.00
13RUBru-RU1 234,00 ₽
14SEKsv-SE1.234,00 kr
15SGDen-SG$1,234.00
16USDen-US$1,234.00
Categories: Uncategorized

Runing Hosted CapMonster Locally in C#

If you use CapMonster Cloud, then you may find that the per-request pricing does add up over high volumes, if you have spare computing power, then you can run CapMonster Hosted on your own server., or if you are running a batch-process on your own local PC, then this could be a big cost saver.

It’s not free, since you need to buy a license from CapMonster for the base sofware, I used the Lite version for $37, available here: https://zennolab.com/en/products/capmonster/#section_price – Then you also need the sitekey add on, which is $10 per month, available here; https://zennolab.com/wiki/en:addons:capmonster:sitekey – But this now allows you solve 100,000 captchas per day, instead of the $0.6 * 100 = $60 per day cost of the equivalent on CapMonster Cloud at the same volume.

So, After downloading the required software, it will start a server on 127.0.0.3 (port 80) by default that exposes the same API as capmonster.cloud – but obviously with a differnt endpoint (local vs remote).

The problem is that if you use the NuGet Package here; https://www.nuget.org/packages/CapMonsterCloud then you will notice that this does not offer the API endpoint to be configurable.

What I did was clone the repo from https://github.com/mboukhlouf/CapMonsterCloud and then set then change the ApiBaseUrl specified in Endpoints.cs – or make it a public property, and modify it in your client code.

So, this could be a good cost saving tip, for heavy users of CapMonster.

Categories: Uncategorized

Using AWS CloudFront as a proxy

Typically, CloudFront is used to serve your own website, so that it can leverage AWS’s CDN, and various edge servers in many countries, so that data is not transferred accross the world with every HTTP request.

But, you can also use it as a proxy, by pointing CloudFront at someone else’s website. This means that when you visit the CloudFront URL, the request to the third party website will come from AWS rather than from your local machine.

It’s not as anonymous as a good proxy, since there are various headers that would undoubtedly expose you, based on the request made to HTTPBIN below;

{
  "args": {}, 
  "headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", 
    "Accept-Encoding": "gzip, deflate, br", 
    "Accept-Language": "en-US,en;q=0.9", 
    "Dnt": "1", 
    "Host": "httpbin.org", 
    "Sec-Ch-Ua": "\"Google Chrome\";v=\"107\", \"Chromium\";v=\"107\", \"Not=A?Brand\";v=\"24\"", 
    "Sec-Ch-Ua-Mobile": "?0", 
    "Sec-Ch-Ua-Platform": "\"Windows\"", 
    "Sec-Fetch-Dest": "document", 
    "Sec-Fetch-Mode": "navigate", 
    "Sec-Fetch-Site": "none", 
    "Sec-Fetch-User": "?1", 
    "Upgrade-Insecure-Requests": "1", 
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36", 
    "X-Amzn-Trace-Id": "Root=1-636908d7-xxxxxxxx"
  }, 
  "origin": "xxx.xxx.xxx.xxx", 
  "url": "https://httpbin.org/get"
}

Here X-Amzn-Trace-Id could probably be tied to you.

Categories: Uncategorized