DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Databases
  4. ReleaseCandidateTracker Records Deployments in RavenDB

ReleaseCandidateTracker Records Deployments in RavenDB

Oren Eini user avatar by
Oren Eini
·
May. 16, 12 · Interview
Like (0)
Save
Tweet
Share
4.38K Views

Join the DZone community and get the full member experience.

Join For Free

releasecandidatetracker is a new ravendb based application by szymon pobiega. i reviewed version 5f7e42e0fb1dea70e53bace63f3e18d95d2a62dd. at this point, i don’t know anything about this application, including what exactly it means, release tracking.

i downloaded the code and started vs, there is one project in the solution, which is already a good thing. i decided to randomize my review approach and go and check the models directory first.

here is how it looks:

image

this is interesting for several reasons. first, it looks like it is meant to keep a record of all deployments to multiple environments, and that you can lookup the history of each deployment both on the environment side and on the release candidate side.

note that we use rich models, which have collections in them. in fact, take a look at this method:

image

which calls to this method:

image

you know what the really fun part about this?

it ain’t relational model. there is no cost of actually making all of these calls!

next, we move to the infrastructure folder, where we have a couple of action results and the ravendb management stuff. here it how rct uses ravendb:

    public static class database
    {
        private static idocumentstore storeinstance;

        public static idocumentstore instance
        {
            get
            {
                if (storeinstance == null)
                {
                    throw new invalidoperationexception("document store has not been initialized.");
                }
                return storeinstance;
            }
        }

        public static void initialize()
        {
            var embeddabledocumentstore = new embeddabledocumentstore {datadirectory = @"~\app_data\database"};
            embeddabledocumentstore.initialize();
            storeinstance = embeddabledocumentstore;
        }
    }

it is using an embedded database to do that, which makes it very easy to use the app. just hit f5 and go. in fact, if we do, we see the fully functional website, which is quite awesome smile .

let us move to seeing how we are managing the sessions:

public class basecontroller : controller
{
    public idocumentsession documentsession { get; private set; }
    public candidateservice candidateservice { get; private set; }
    public scriptservice scriptservice { get; private set; }

    protected override void onactionexecuting(actionexecutingcontext filtercontext)
    {
        if (filtercontext.ischildaction)
        {
            return;
        }
        documentsession = database.instance.opensession();
        candidateservice = new candidateservice(documentsession);
        scriptservice = new scriptservice(documentsession);
        base.onactionexecuting(filtercontext);
    }
    
    protected override void onactionexecuted(actionexecutedcontext filtercontext)
    {
        if (filtercontext.ischildaction)
        {
            return;
        }
        if(documentsession != null)
        {
            if (filtercontext.exception == null)
            {
                documentsession.savechanges();
            }
            documentsession.dispose();
        }
        base.onactionexecuted(filtercontext);
    }
}

this is all handled inside the base controller, and it is very similar to how i am doing that in my own apps.

however, scriptservice and candidateservice seems strange, let us explore them a bit.

public class scriptservice
{
    private readonly idocumentsession documentsession;

    public scriptservice(idocumentsession documentsession)
    {
        this.documentsession = documentsession;
    }

    public void attachscript(string versionnumber, stream filecontents)
    {
        var metadata = new ravenjobject();
        documentsession.advanced.databasecommands.putattachment(versionnumber, null, filecontents, metadata);
    }

    public stream getscript(string versionnumber)
    {
        var attachment = documentsession.advanced.databasecommands.getattachment(versionnumber);
        return attachment != null 
            ? attachment.data() 
            : null;
    }
}

so this is using ravendb attachment to store stuff, i am not quite sure what yet, so let us track it down.

this is being used like this:

[httpget]
public actionresult getscript(string versionnumber)
{
    var candidate = candidateservice.findonebyversionnumber(versionnumber);
    var attachment = scriptservice.getscript(versionnumber);
    if (attachment != null)
    {
        var result = new filestreamresult(attachment, "text/plain");
        var version = candidate.versionnumber;
        var product = candidate.productname;
        result.filedownloadname = string.format("deploy-{0}-{1}.ps1", product, version);
        return result;
    }
    return new httpnotfoundresult("deployment script missing.");
}

so i am assuming that the scripts are deployment scripts for different versions, and that they get uploaded on every new release candidate.

but look at the candidateservice, it looks like a traditional service wrapping ravendb, and i have spoken against it multiple times.

in particular, i dislike this bit of code:

public releasecandidate findonebyversionnumber(string versionnumber)
{
    var result = documentsession.query<releasecandidate>()
        .where(x => x.versionnumber == versionnumber)
        .firstordefault();
    if(result == null)
    {
        throw new releasecandidatenotfoundexception(versionnumber);
    }
    return result;
}

public void store(releasecandidate candidate)
{
    var existing = documentsession.query<releasecandidate>()
        .where(x => x.versionnumber == candidate.versionnumber)
        .any();
    if (existing)
    {
        throw new releasecandidatealreadyexistsexception(candidate.versionnumber);
    }
    documentsession.store(candidate);
}

from looking at the code, it looks like the version number of the release candidate is the primary way to look it up. more than that, in the entire codebase, there is never a case where we load a document by id.

when i see a versionnumber, i think about things like “1.0.812.0”, but i think that in this case the version number is likely to include the product name as well, “ravendb-1.0.812.0”, otherwise you couldn’t have two products with the same version.

that said, the code above it wrong, because it doesn’t take into account ravendb’s indexes base nature. instead, the version number should actually be the releasecandidate id. this way, because ravendb’s document store is fully acid, we don’t have to worry about index update times, and we can load things very efficiently.

pretty much all of the rest of the code in the candidateservice is only used in a single location, and i don’t really see a value in it being there.

for example, let us look at this one:

[httppost]
public actionresult markasdeployed(string versionnumber, string environment, bool success)
{
    candidateservice.markasdeployed(versionnumber, environment, success);
    return new emptyresult();
}

image

as you can see, it is merely loading the appropriate release candidate, and calling the markasdeployed method on it.

instead of doing this needless, forwarding, and assuming that we have the versionnumber as the id, i would write:

[httppost]
public actionresult markasdeployed(string versionnumber, string environment, bool success)
{
    var cadnidate = documentsession.load<releasecandidate>(versionnumber);
    if (cadnidate == null)
        throw new releasecandidatenotfoundexception(versionnumber);
    var env = documentsession.load<deploymentenvironment>(environment);
    if (env == null)
        throw new invalidoperationexception(string.format("environment {0} not found", environment));

    cadnidate.markasdeployed(success, env);
    return new emptyresult();
}

finally, a word about the error handling, this is handled via:

protected override void onexception(exceptioncontext filtercontext)
{
    filtercontext.result = new errorresult(filtercontext.exception.message);
    filtercontext.exceptionhandled = true;
}

public class errorresult : actionresult
{
    private readonly string message;

    public errorresult(string message)
    {
        this.message = message;
    }

    public override void executeresult(controllercontext context)
    {
        context.httpcontext.response.write(message);
        context.httpcontext.response.statuscode = 500;
    }
}

the crazy part is that onexception is overridden only on some of the controllers, rather than in the base controller, and even worse. this sort of code leads to error details loss.

for example, let us say that i get a nullreferenceexception. this code will dutifully tell me all about it, but will not tell me where it happened .

this sort of thing make debugging extremely hard.

Database

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • mTLS Everywere
  • NoSQL vs SQL: What, Where, and How
  • Stop Using Spring Profiles Per Environment
  • Fargate vs. Lambda: The Battle of the Future

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: